Search in sources :

Example 6 with UserRole

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

the class UserService method getInformantsOfFacility.

public List<User> getInformantsOfFacility(Facility facility) {
    if (facility == null || !FacilityType.HOSPITAL.equals(facility.getType())) {
        throw new IllegalArgumentException("Facility needs to be a hospital");
    }
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> cq = cb.createQuery(getElementClass());
    Root<User> from = cq.from(getElementClass());
    Join<User, UserRole> joinRoles = from.join(User.USER_ROLES, JoinType.LEFT);
    Predicate filter = cb.and(createDefaultFilter(cb, from), cb.equal(from.get(User.HEALTH_FACILITY), facility), joinRoles.in(Collections.singletonList(UserRole.HOSPITAL_INFORMANT)));
    cq.where(filter).distinct(true);
    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 7 with UserRole

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

the class KeycloakService method ensureRoles.

private void ensureRoles(Keycloak keycloak, String userRepresentationId, Set<UserRole> userRoles) {
    RealmResource realm = keycloak.realm(REALM_NAME);
    Map<String, RoleRepresentation> keycloakRoles = getRealmRoles(keycloak);
    UserResource userResource = realm.users().get(userRepresentationId);
    Set<String> sormasRoles = Arrays.stream(UserRole.values()).map(Enum::name).collect(Collectors.toSet());
    List<RoleRepresentation> oldUserRoles = userResource.roles().realmLevel().listAll().stream().filter(role -> sormasRoles.contains(role.getName())).collect(Collectors.toList());
    List<RoleRepresentation> newUserRoles = userRoles.stream().map(userRole -> keycloakRoles.get(userRole.name())).filter(Objects::nonNull).collect(Collectors.toList());
    if (keycloakRoles.containsKey(DEFAULT_USER_ROLE)) {
        newUserRoles.add(keycloakRoles.get(DEFAULT_USER_ROLE));
    }
    if (CollectionUtils.isNotEmpty(oldUserRoles)) {
        userResource.roles().realmLevel().remove(oldUserRoles);
    }
    userResource.roles().realmLevel().add(newUserRoles);
}
Also used : RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) AuthProvider(de.symeda.sormas.api.AuthProvider) Arrays(java.util.Arrays) LOCALE(org.keycloak.representations.IDToken.LOCALE) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Function(java.util.function.Function) StringUtils(org.apache.commons.lang3.StringUtils) ObservesAsync(javax.enterprise.event.ObservesAsync) Collections.singletonList(java.util.Collections.singletonList) CollectionUtils(org.apache.commons.collections.CollectionUtils) Map(java.util.Map) Observes(javax.enterprise.event.Observes) UserCreateEvent(de.symeda.sormas.backend.user.event.UserCreateEvent) LocalBean(javax.ejb.LocalBean) Json(javax.json.Json) CredentialRepresentation(org.keycloak.representations.idm.CredentialRepresentation) UserRole(de.symeda.sormas.api.user.UserRole) UserResource(org.keycloak.admin.client.resource.UserResource) RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) ConfigFacadeEjbLocal(de.symeda.sormas.backend.common.ConfigFacadeEjb.ConfigFacadeEjbLocal) EJB(javax.ejb.EJB) Stateless(javax.ejb.Stateless) UserUpdateEvent(de.symeda.sormas.backend.user.event.UserUpdateEvent) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Logger(org.slf4j.Logger) RealmResource(org.keycloak.admin.client.resource.RealmResource) Set(java.util.Set) JsonPath(com.jayway.jsonpath.JsonPath) Collectors(java.util.stream.Collectors) Language(de.symeda.sormas.api.Language) Keycloak(org.keycloak.admin.client.Keycloak) Objects(java.util.Objects) Base64(java.util.Base64) List(java.util.List) Response(javax.ws.rs.core.Response) ConfigProvider(org.eclipse.microprofile.config.ConfigProvider) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) PasswordResetEvent(de.symeda.sormas.backend.user.event.PasswordResetEvent) KeycloakBuilder(org.keycloak.admin.client.KeycloakBuilder) Collections(java.util.Collections) JsonObjectBuilder(javax.json.JsonObjectBuilder) OAuth2Constants(org.keycloak.OAuth2Constants) RealmResource(org.keycloak.admin.client.resource.RealmResource) UserResource(org.keycloak.admin.client.resource.UserResource)

Example 8 with UserRole

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

the class UserService method getReferenceList.

/**
 * Loads users filtered by combinable filter conditions.<br />
 * Condition combination if parameter is set:<br />
 * {@code ((regionUuids & districtUuids & communityUuids & filterByJurisdiction & userRoles) | includeSupervisors) & activeOnly}
 *
 * @see #createJurisdictionFilter(CriteriaBuilder, From)
 * @param regionUuids
 * @param districtUuids
 * @param communityUuids
 * @param includeSupervisors
 *            If set to {@code true}, all supervisors are returned independent of other filters.
 * @param filterByJurisdiction
 * @param activeOnly
 * @param userRoles
 */
public List<UserReference> getReferenceList(List<String> regionUuids, List<String> districtUuids, List<String> communityUuids, boolean includeSupervisors, boolean filterByJurisdiction, boolean activeOnly, List<UserRole> userRoles) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<UserReference> cq = cb.createQuery(UserReference.class);
    Root<UserReference> root = cq.from(UserReference.class);
    Join<UserReference, UserRole> rolesJoin = root.join(User.USER_ROLES, JoinType.LEFT);
    Root<User> userRoot = cq.from(User.class);
    cq.select(root);
    // WHERE inner AND
    Predicate filter = null;
    boolean userEntityJoinUsed = false;
    if (CollectionUtils.isNotEmpty(regionUuids)) {
        Join<User, Region> regionJoin = userRoot.join(User.REGION, JoinType.LEFT);
        filter = CriteriaBuilderHelper.and(cb, filter, cb.in(regionJoin.get(AbstractDomainObject.UUID)).value(regionUuids));
        userEntityJoinUsed = true;
    }
    if (CollectionUtils.isNotEmpty(districtUuids)) {
        Join<User, District> districtJoin = userRoot.join(User.DISTRICT, JoinType.LEFT);
        filter = CriteriaBuilderHelper.and(cb, filter, cb.in(districtJoin.get(AbstractDomainObject.UUID)).value(districtUuids));
        userEntityJoinUsed = true;
    }
    if (filterByJurisdiction) {
        filter = CriteriaBuilderHelper.and(cb, filter, createJurisdictionFilter(cb, userRoot));
        userEntityJoinUsed = true;
    }
    if (CollectionUtils.isNotEmpty(userRoles)) {
        filter = CriteriaBuilderHelper.and(cb, filter, rolesJoin.in(userRoles));
    }
    if (userEntityJoinUsed) {
        filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(root.get(UserReference.ID), userRoot.get(AbstractDomainObject.ID)));
    }
    // WHERE OR
    if (includeSupervisors) {
        Predicate supervisorFilter = rolesJoin.in(Arrays.asList(UserRole.CASE_SUPERVISOR, UserRole.CONTACT_SUPERVISOR, UserRole.SURVEILLANCE_SUPERVISOR, UserRole.ADMIN_SUPERVISOR));
        filter = CriteriaBuilderHelper.or(cb, filter, supervisorFilter);
    }
    // WHERE outer AND
    if (activeOnly) {
        filter = CriteriaBuilderHelper.and(cb, filter, createDefaultFilter(cb, root));
    }
    if (CollectionUtils.isNotEmpty(communityUuids)) {
        Join<User, Community> communityJoin = userRoot.join(User.COMMUNITY, JoinType.LEFT);
        filter = CriteriaBuilderHelper.and(cb, filter, cb.in(communityJoin.get(AbstractDomainObject.UUID)).value(communityUuids));
    }
    if (filter != null) {
        cq.where(filter);
    }
    cq.distinct(true);
    cq.orderBy(cb.asc(root.get(AbstractDomainObject.ID)));
    return em.createQuery(cq).setHint(ModelConstants.HINT_HIBERNATE_READ_ONLY, true).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Predicate(javax.persistence.criteria.Predicate) UserRole(de.symeda.sormas.api.user.UserRole) Region(de.symeda.sormas.backend.infrastructure.region.Region) District(de.symeda.sormas.backend.infrastructure.district.District) Community(de.symeda.sormas.backend.infrastructure.community.Community)

Example 9 with UserRole

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

the class CaseBackendTest method testTaskReassignmentAfterChangedCaseDistrict.

@Test
public void testTaskReassignmentAfterChangedCaseDistrict() throws DaoException {
    CaseDao caseDao = DatabaseHelper.getCaseDao();
    Case caze = TestEntityCreator.createCase();
    caze.setRegion(caze.getResponsibleRegion());
    caze.setDistrict(caze.getResponsibleDistrict());
    caze.setCommunity(caze.getResponsibleCommunity());
    caseDao.saveAndSnapshot(caze);
    User user = ConfigProvider.getUser();
    UserRole userRole = UserRole.SURVEILLANCE_OFFICER;
    Set<UserRole> userRoles = new HashSet<>();
    userRoles.add(userRole);
    user.setUserRoles(userRoles);
    DatabaseHelper.getUserDao().saveAndSnapshot(user);
    TaskDao taskDao = DatabaseHelper.getTaskDao();
    Task task = TestEntityCreator.createCaseTask(caze, TaskStatus.PENDING, user);
    assertEquals(caze.getResponsibleRegion().getUuid(), TestHelper.REGION_UUID);
    assertEquals(caze.getResponsibleDistrict().getUuid(), TestHelper.DISTRICT_UUID);
    assertEquals(caze.getResponsibleCommunity().getUuid(), TestHelper.COMMUNITY_UUID);
    assertEquals(caze.getRegion().getUuid(), TestHelper.REGION_UUID);
    assertEquals(caze.getDistrict().getUuid(), TestHelper.DISTRICT_UUID);
    assertEquals(caze.getCommunity().getUuid(), TestHelper.COMMUNITY_UUID);
    assertEquals(caze.getHealthFacility().getUuid(), TestHelper.FACILITY_UUID);
    task = taskDao.queryUuid(task.getUuid());
    assertEquals(TestHelper.USER_UUID, task.getAssigneeUser().getUuid());
    // ResponsibleDistrict changed, but District still in user's jurisdiction
    District secondDistrict = DatabaseHelper.getDistrictDao().queryUuid(TestHelper.SECOND_DISTRICT_UUID);
    Community secondCommunity = DatabaseHelper.getCommunityDao().queryUuid(TestHelper.SECOND_COMMUNITY_UUID);
    caze.setResponsibleDistrict(secondDistrict);
    caze.setResponsibleCommunity(secondCommunity);
    caseDao.saveAndSnapshot(caze);
    task = taskDao.queryUuid(task.getUuid());
    assertEquals(TestHelper.USER_UUID, task.getAssigneeUser().getUuid());
    // Case not in user's jurisdiction anymore
    caze.setDistrict(secondDistrict);
    caze.setCommunity(null);
    caseDao.saveAndSnapshot(caze);
    task = taskDao.queryUuid(task.getUuid());
    assertEquals(TestHelper.SECOND_USER_UUID, task.getAssigneeUser().getUuid());
}
Also used : Task(de.symeda.sormas.app.backend.task.Task) User(de.symeda.sormas.app.backend.user.User) UserRole(de.symeda.sormas.api.user.UserRole) CaseDao(de.symeda.sormas.app.backend.caze.CaseDao) TaskDao(de.symeda.sormas.app.backend.task.TaskDao) District(de.symeda.sormas.app.backend.region.District) Community(de.symeda.sormas.app.backend.region.Community) Case(de.symeda.sormas.app.backend.caze.Case) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with UserRole

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

the class UserFacadeEjbTest method testGetValidLoginRoles.

@Test
public void testGetValidLoginRoles() {
    AuthProvider authProvider = mock(AuthProvider.class);
    MockedStatic<AuthProvider> mockAuthProvider = mockStatic(AuthProvider.class);
    Mockito.when(AuthProvider.getProvider(any())).thenReturn(authProvider);
    RDCF rdcf = creator.createRDCF();
    UserDto user = creator.createUser(rdcf, SURVEILLANCE_SUPERVISOR);
    String password = getUserFacade().resetPassword(user.getUuid());
    Set<UserRole> validLoginRoles = getUserFacade().getValidLoginRoles(user.getUserName(), password);
    assertThat(validLoginRoles, containsInAnyOrder(SURVEILLANCE_SUPERVISOR));
    user.setActive(false);
    getUserFacade().saveUser(user);
    validLoginRoles = getUserFacade().getValidLoginRoles(user.getUserName(), password);
    assertThat(validLoginRoles, nullValue());
    // Important: release static mock.
    mockAuthProvider.closeOnDemand();
}
Also used : RDCF(de.symeda.sormas.backend.TestDataCreator.RDCF) UserRole(de.symeda.sormas.api.user.UserRole) UserDto(de.symeda.sormas.api.user.UserDto) AuthProvider(de.symeda.sormas.api.AuthProvider) AbstractBeanTest(de.symeda.sormas.backend.AbstractBeanTest) Test(org.junit.Test)

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