Search in sources :

Example 1 with UserRight

use of de.symeda.sormas.api.user.UserRight in project SORMAS-Project by hzi-braunschweig.

the class UserRightsFacadeEjb method generateUserRightsDocument.

private void generateUserRightsDocument(Map<UserRole, Set<UserRight>> userRoleRights, OutputStream outStream) throws IOException {
    XSSFWorkbook workbook = new XSSFWorkbook();
    // Create User Rights sheet
    String safeName = WorkbookUtil.createSafeSheetName(I18nProperties.getCaption(Captions.userRights));
    XSSFSheet sheet = workbook.createSheet(safeName);
    // Define colors
    final XSSFColor green = XssfHelper.createColor(0, 153, 0);
    final XSSFColor red = XssfHelper.createColor(255, 0, 0);
    final XSSFColor black = XssfHelper.createColor(0, 0, 0);
    // Initialize cell styles
    // Authorized style
    XSSFCellStyle authorizedStyle = workbook.createCellStyle();
    authorizedStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    authorizedStyle.setFillForegroundColor(green);
    authorizedStyle.setBorderBottom(BorderStyle.THIN);
    authorizedStyle.setBorderLeft(BorderStyle.THIN);
    authorizedStyle.setBorderTop(BorderStyle.THIN);
    authorizedStyle.setBorderRight(BorderStyle.THIN);
    authorizedStyle.setBorderColor(BorderSide.BOTTOM, black);
    authorizedStyle.setBorderColor(BorderSide.LEFT, black);
    authorizedStyle.setBorderColor(BorderSide.TOP, black);
    authorizedStyle.setBorderColor(BorderSide.RIGHT, black);
    // Unauthorized style
    XSSFCellStyle unauthorizedStyle = workbook.createCellStyle();
    unauthorizedStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    unauthorizedStyle.setFillForegroundColor(red);
    unauthorizedStyle.setBorderBottom(BorderStyle.THIN);
    unauthorizedStyle.setBorderLeft(BorderStyle.THIN);
    unauthorizedStyle.setBorderTop(BorderStyle.THIN);
    unauthorizedStyle.setBorderRight(BorderStyle.THIN);
    unauthorizedStyle.setBorderColor(BorderSide.BOTTOM, black);
    unauthorizedStyle.setBorderColor(BorderSide.LEFT, black);
    unauthorizedStyle.setBorderColor(BorderSide.TOP, black);
    unauthorizedStyle.setBorderColor(BorderSide.RIGHT, black);
    // Bold style
    XSSFFont boldFont = workbook.createFont();
    boldFont.setBold(true);
    XSSFCellStyle boldStyle = workbook.createCellStyle();
    boldStyle.setFont(boldFont);
    int rowCounter = 0;
    // Header
    Row headerRow = sheet.createRow(rowCounter++);
    Cell userRightHeadlineCell = headerRow.createCell(0);
    userRightHeadlineCell.setCellValue(I18nProperties.getCaption(Captions.userRight));
    userRightHeadlineCell.setCellStyle(boldStyle);
    Cell descHeadlineCell = headerRow.createCell(1);
    descHeadlineCell.setCellValue(I18nProperties.getCaption(Captions.UserRight_description));
    descHeadlineCell.setCellStyle(boldStyle);
    sheet.setColumnWidth(0, 256 * 35);
    sheet.setColumnWidth(1, 256 * 50);
    for (UserRole userRole : UserRole.values()) {
        String columnCaption = userRole.toString();
        Cell headerCell = headerRow.createCell(userRole.ordinal() + 2);
        headerCell.setCellValue(columnCaption);
        headerCell.setCellStyle(boldStyle);
        sheet.setColumnWidth(userRole.ordinal() + 2, 256 * 14);
    }
    // Jurisdiction row (header)
    final Row jurisdictionRow = sheet.createRow(rowCounter++);
    final Cell jurisdictionHeadlineCell = jurisdictionRow.createCell(0);
    jurisdictionHeadlineCell.setCellValue(I18nProperties.getCaption(Captions.UserRight_jurisdiction));
    jurisdictionHeadlineCell.setCellStyle(boldStyle);
    final Cell jurDescHeadlineCell = jurisdictionRow.createCell(1);
    jurDescHeadlineCell.setCellValue(I18nProperties.getCaption(Captions.UserRight_jurisdictionOfRole));
    jurDescHeadlineCell.setCellStyle(boldStyle);
    for (UserRole userRole : UserRole.values()) {
        final String columnCaption = userRole.getJurisdictionLevel().toString();
        final Cell headerCell = jurisdictionRow.createCell(userRole.ordinal() + 2);
        headerCell.setCellValue(columnCaption);
        headerCell.setCellStyle(boldStyle);
    }
    // User right rows
    for (UserRight userRight : UserRight.values()) {
        Row row = sheet.createRow(rowCounter++);
        // User right name
        Cell nameCell = row.createCell(0);
        nameCell.setCellValue(userRight.name());
        nameCell.setCellStyle(boldStyle);
        // User right description
        Cell descCell = row.createCell(1);
        descCell.setCellValue(userRight.toString());
        // Add styled cells for all user roles
        for (UserRole userRole : UserRole.values()) {
            Cell roleRightCell = row.createCell(userRole.ordinal() + 2);
            if (userRoleRights.containsKey(userRole) && userRoleRights.get(userRole).contains(userRight) || userRole.hasDefaultRight(userRight)) {
                roleRightCell.setCellStyle(authorizedStyle);
                roleRightCell.setCellValue(I18nProperties.getString(Strings.yes));
            } else {
                roleRightCell.setCellStyle(unauthorizedStyle);
                roleRightCell.setCellValue(I18nProperties.getString(Strings.no));
            }
        }
    }
    XssfHelper.addAboutSheet(workbook);
    workbook.write(outStream);
    workbook.close();
}
Also used : XSSFColor(org.apache.poi.xssf.usermodel.XSSFColor) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFCellStyle(org.apache.poi.xssf.usermodel.XSSFCellStyle) UserRight(de.symeda.sormas.api.user.UserRight) UserRole(de.symeda.sormas.api.user.UserRole) XSSFFont(org.apache.poi.xssf.usermodel.XSSFFont) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Row(org.apache.poi.ss.usermodel.Row) Cell(org.apache.poi.ss.usermodel.Cell)

Example 2 with UserRight

use of de.symeda.sormas.api.user.UserRight in project SORMAS-Project by hzi-braunschweig.

the class UserRoleConfigFacadeEjb method toDto.

public static UserRoleConfigDto toDto(UserRoleConfig source) {
    if (source == null) {
        return null;
    }
    UserRoleConfigDto target = new UserRoleConfigDto();
    DtoHelper.fillDto(target, source);
    target.setUserRole(source.getUserRole());
    target.setUserRights(new HashSet<UserRight>(source.getUserRights()));
    return target;
}
Also used : UserRight(de.symeda.sormas.api.user.UserRight) UserRoleConfigDto(de.symeda.sormas.api.user.UserRoleConfigDto)

Example 3 with UserRight

use of de.symeda.sormas.api.user.UserRight in project SORMAS-Project by hzi-braunschweig.

the class UserRoleConfigFacadeEjb method getEffectiveUserRights.

@Override
public Set<UserRight> getEffectiveUserRights(UserRole... userRoles) {
    Map<UserRole, Set<UserRight>> userRoleRights = getUserRoleRightsCached();
    Set<UserRight> userRights = EnumSet.noneOf(UserRight.class);
    for (UserRole userRole : userRoles) {
        userRights.addAll(userRoleRights.get(userRole));
    }
    return userRights;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) EnumSet(java.util.EnumSet) UserRight(de.symeda.sormas.api.user.UserRight) UserRole(de.symeda.sormas.api.user.UserRole)

Example 4 with UserRight

use of de.symeda.sormas.api.user.UserRight in project SORMAS-Project by hzi-braunschweig.

the class UserRoleConfigFacadeEjbTest method testGetEffectiveUserRights.

@Test
public void testGetEffectiveUserRights() {
    // 1. no role configured -> use defaults
    Set<UserRight> supervisorRights = getUserRoleConfigFacade().getEffectiveUserRights(UserRole.SURVEILLANCE_SUPERVISOR);
    assertThat(supervisorRights, is(UserRole.SURVEILLANCE_SUPERVISOR.getDefaultUserRights()));
    UserRoleConfigDto userRoleConfig = UserRoleConfigDto.build(UserRole.SURVEILLANCE_SUPERVISOR);
    userRoleConfig = getUserRoleConfigFacade().saveUserRoleConfig(userRoleConfig);
    // 2. role configured with no rights
    supervisorRights = getUserRoleConfigFacade().getEffectiveUserRights(UserRole.SURVEILLANCE_SUPERVISOR);
    assertThat(supervisorRights, is(IsEmptyCollection.empty()));
    // 3. role configured with a few rights
    userRoleConfig.getUserRights().add(UserRight.CASE_CREATE);
    userRoleConfig.getUserRights().add(UserRight.CASE_EDIT);
    userRoleConfig = getUserRoleConfigFacade().saveUserRoleConfig(userRoleConfig);
    supervisorRights = getUserRoleConfigFacade().getEffectiveUserRights(UserRole.SURVEILLANCE_SUPERVISOR);
    assertThat(supervisorRights, is(new HashSet<UserRight>(Arrays.asList(UserRight.CASE_CREATE, UserRight.CASE_EDIT))));
    // 4. combine configured and default rights
    Set<UserRight> mixedUserRights = getUserRoleConfigFacade().getEffectiveUserRights(UserRole.SURVEILLANCE_SUPERVISOR, UserRole.NATIONAL_OBSERVER);
    Set<UserRight> expectedUserRights = new HashSet<UserRight>(Arrays.asList(UserRight.CASE_CREATE, UserRight.CASE_EDIT));
    expectedUserRights.addAll(UserRole.NATIONAL_OBSERVER.getDefaultUserRights());
    assertThat(mixedUserRights, is(expectedUserRights));
}
Also used : UserRight(de.symeda.sormas.api.user.UserRight) UserRoleConfigDto(de.symeda.sormas.api.user.UserRoleConfigDto) HashSet(java.util.HashSet) Test(org.junit.Test) AbstractBeanTest(de.symeda.sormas.backend.AbstractBeanTest)

Aggregations

UserRight (de.symeda.sormas.api.user.UserRight)4 UserRole (de.symeda.sormas.api.user.UserRole)2 UserRoleConfigDto (de.symeda.sormas.api.user.UserRoleConfigDto)2 HashSet (java.util.HashSet)2 AbstractBeanTest (de.symeda.sormas.backend.AbstractBeanTest)1 EnumSet (java.util.EnumSet)1 Set (java.util.Set)1 Cell (org.apache.poi.ss.usermodel.Cell)1 Row (org.apache.poi.ss.usermodel.Row)1 XSSFCellStyle (org.apache.poi.xssf.usermodel.XSSFCellStyle)1 XSSFColor (org.apache.poi.xssf.usermodel.XSSFColor)1 XSSFFont (org.apache.poi.xssf.usermodel.XSSFFont)1 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)1 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)1 Test (org.junit.Test)1