Search in sources :

Example 1 with UserRole

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

the class SampleFacadeEjb method deleteSamples.

public List<String> deleteSamples(List<String> sampleUuids) {
    User user = userService.getCurrentUser();
    if (!userRoleConfigFacade.getEffectiveUserRights(user.getUserRoles().toArray(new UserRole[user.getUserRoles().size()])).contains(UserRight.SAMPLE_DELETE)) {
        throw new UnsupportedOperationException("User " + user.getUuid() + " is not allowed to delete samples.");
    }
    List<String> deletedSampleUuids = new ArrayList<>();
    List<Sample> samplesToBeDeleted = sampleService.getByUuids(sampleUuids);
    if (samplesToBeDeleted != null) {
        samplesToBeDeleted.forEach(sampleToBeDeleted -> {
            if (!sampleToBeDeleted.isDeleted()) {
                sampleService.delete(sampleToBeDeleted);
                deletedSampleUuids.add(sampleToBeDeleted.getUuid());
            }
        });
    }
    return deletedSampleUuids;
}
Also used : User(de.symeda.sormas.backend.user.User) UserRole(de.symeda.sormas.api.user.UserRole) ArrayList(java.util.ArrayList)

Example 2 with UserRole

use of de.symeda.sormas.api.user.UserRole 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 3 with UserRole

use of de.symeda.sormas.api.user.UserRole 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 UserRole

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

the class UserService method getAllByRegionsAndUserRoles.

/**
 * @see #getReferenceList(List, List, List, boolean, boolean, boolean, List) This method is partly a duplication for getReferenceList,
 *      but it's still in use for WeeklyReports and messageRecipients where more information of the user is needed
 *      and method signatures rely on {@link User}.
 */
private List<User> getAllByRegionsAndUserRoles(List<Region> regions, Collection<UserRole> userRoles, BiFunction<CriteriaBuilder, Root<User>, Predicate> createExtraFilters) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> cq = cb.createQuery(getElementClass());
    Root<User> from = cq.from(getElementClass());
    Predicate filter = createDefaultFilter(cb, from);
    if (regions != null) {
        filter = from.get(User.REGION).in(regions);
    }
    if (!userRoles.isEmpty()) {
        Join<User, UserRole> joinRoles = from.join(User.USER_ROLES, JoinType.LEFT);
        Predicate rolesFilter = joinRoles.in(userRoles);
        filter = CriteriaBuilderHelper.and(cb, filter, rolesFilter);
    }
    if (createExtraFilters != null) {
        filter = CriteriaBuilderHelper.and(cb, filter, createExtraFilters.apply(cb, from));
    }
    if (filter != null) {
        cq.where(filter);
    }
    cq.distinct(true).orderBy(cb.asc(from.get(AbstractDomainObject.ID)));
    return em.createQuery(cq).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) UserRole(de.symeda.sormas.api.user.UserRole) Predicate(javax.persistence.criteria.Predicate)

Example 5 with UserRole

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

the class UserService method buildCriteriaFilter.

public Predicate buildCriteriaFilter(UserCriteria userCriteria, CriteriaBuilder cb, Root<User> from) {
    Predicate filter = null;
    if (userCriteria.getActive() != null) {
        filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(from.get(User.ACTIVE), userCriteria.getActive()));
    }
    if (userCriteria.getUserRole() != null) {
        Join<User, UserRole> joinRoles = from.join(User.USER_ROLES, JoinType.LEFT);
        filter = CriteriaBuilderHelper.and(cb, filter, joinRoles.in(Collections.singletonList(userCriteria.getUserRole())));
    }
    if (userCriteria.getRegion() != null) {
        filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(from.join(Case.REGION, JoinType.LEFT).get(AbstractDomainObject.UUID), userCriteria.getRegion().getUuid()));
    }
    if (userCriteria.getDistrict() != null) {
        filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(from.join(Case.DISTRICT, JoinType.LEFT).get(AbstractDomainObject.UUID), userCriteria.getDistrict().getUuid()));
    }
    if (userCriteria.getFreeText() != null) {
        String[] textFilters = userCriteria.getFreeText().split("\\s+");
        for (String textFilter : textFilters) {
            if (DataHelper.isNullOrEmpty(textFilter)) {
                continue;
            }
            Predicate likeFilters = cb.or(CriteriaBuilderHelper.unaccentedIlike(cb, from.get(User.FIRST_NAME), textFilter), CriteriaBuilderHelper.unaccentedIlike(cb, from.get(User.LAST_NAME), textFilter), CriteriaBuilderHelper.unaccentedIlike(cb, from.get(User.USER_NAME), textFilter), CriteriaBuilderHelper.unaccentedIlike(cb, from.get(User.USER_EMAIL), textFilter), CriteriaBuilderHelper.ilike(cb, from.get(User.PHONE), textFilter), CriteriaBuilderHelper.ilike(cb, from.get(User.UUID), textFilter));
            filter = CriteriaBuilderHelper.and(cb, filter, likeFilters);
        }
    }
    return filter;
}
Also used : UserRole(de.symeda.sormas.api.user.UserRole) Predicate(javax.persistence.criteria.Predicate)

Aggregations

UserRole (de.symeda.sormas.api.user.UserRole)29 Test (org.junit.Test)7 HashSet (java.util.HashSet)6 Case (de.symeda.sormas.app.backend.caze.Case)5 User (de.symeda.sormas.backend.user.User)5 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)5 UserDto (de.symeda.sormas.api.user.UserDto)4 User (de.symeda.sormas.app.backend.user.User)4 Predicate (javax.persistence.criteria.Predicate)4 CaseDao (de.symeda.sormas.app.backend.caze.CaseDao)3 District (de.symeda.sormas.app.backend.region.District)3 Task (de.symeda.sormas.app.backend.task.Task)3 TaskDao (de.symeda.sormas.app.backend.task.TaskDao)3 Set (java.util.Set)3 AuthProvider (de.symeda.sormas.api.AuthProvider)2 JurisdictionLevel (de.symeda.sormas.api.user.JurisdictionLevel)2 UserRight (de.symeda.sormas.api.user.UserRight)2 Community (de.symeda.sormas.app.backend.region.Community)2 AbstractBeanTest (de.symeda.sormas.backend.AbstractBeanTest)2 RDCF (de.symeda.sormas.backend.TestDataCreator.RDCF)2