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();
}
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);
}
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();
}
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());
}
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();
}
Aggregations