Skip to content

Commit

Permalink
Merge pull request #8371 from hzi-braunschweig/change-2804-Replace_di…
Browse files Browse the repository at this point in the history
…rect_usage_of_user_roles

#2804: Replace direct usage of user roles
  • Loading branch information
MartinWahnschaffe committed Mar 17, 2022
2 parents 34d07e3 + a19cfdf commit 287f64b
Show file tree
Hide file tree
Showing 91 changed files with 1,049 additions and 827 deletions.
Expand Up @@ -487,7 +487,7 @@ public interface Captions {
String caseFilterWithDifferentRegion = "caseFilterWithDifferentRegion";
String caseFilterWithExtendedQuarantine = "caseFilterWithExtendedQuarantine";
String caseFilterWithoutGeo = "caseFilterWithoutGeo";
String caseFilterWithoutResponsibleOfficer = "caseFilterWithoutResponsibleOfficer";
String caseFilterWithoutResponsibleUser = "caseFilterWithoutResponsibleUser";
String caseFilterWithReducedQuarantine = "caseFilterWithReducedQuarantine";
String caseFollowupVisitsView = "caseFollowupVisitsView";
String CaseHospitalization = "CaseHospitalization";
Expand Down
Expand Up @@ -42,7 +42,7 @@ public interface Descriptions {
String descCaseFilterRelatedToEvent = "descCaseFilterRelatedToEvent";
String descCaseFilterWithExtendedQuarantine = "descCaseFilterWithExtendedQuarantine";
String descCaseFilterWithoutGeo = "descCaseFilterWithoutGeo";
String descCaseFilterWithoutResponsibleOfficer = "descCaseFilterWithoutResponsibleOfficer";
String descCaseFilterWithoutResponsibleUser = "descCaseFilterWithoutResponsibleUser";
String descCaseFilterWithReducedQuarantine = "descCaseFilterWithReducedQuarantine";
String descCommunityFilter = "descCommunityFilter";
String descContactIncludeContactsFromOtherJurisdictions = "descContactIncludeContactsFromOtherJurisdictions";
Expand Down
@@ -0,0 +1,6 @@
package de.symeda.sormas.api.user;

public enum NotificationProtocol {
EMAIL,
SMS;
}
18 changes: 6 additions & 12 deletions sormas-api/src/main/java/de/symeda/sormas/api/user/UserFacade.java
Expand Up @@ -50,9 +50,7 @@ public interface UserFacade {

UserDto getByUserName(String userName);

List<UserReferenceDto> getUsersByRegionAndRoles(RegionReferenceDto regionRef, UserRole... assignableRoles);

List<UserReferenceDto> getUsersByRegionsAndRoles(List<RegionReferenceDto> regionRefs, UserRole... assignableRoles);
List<UserReferenceDto> getUsersByRegionAndRights(RegionReferenceDto regionRef, UserRight... userRights);

List<UserReferenceDto> getUsersWithSuperiorJurisdiction(UserDto user);

Expand All @@ -64,19 +62,17 @@ public interface UserFacade {

/**
* @param district
* @param includeSupervisors
* independent from the district
* @param userRoles
* roles of the users by district
* @param userRights
* rights of the users by district
* @return
*/
List<UserReferenceDto> getUserRefsByDistrict(DistrictReferenceDto district, boolean includeSupervisors, UserRole... userRoles);
List<UserReferenceDto> getUserRefsByDistrict(DistrictReferenceDto district, UserRight... userRights);

List<UserReferenceDto> getUserRefsByDistricts(List<DistrictReferenceDto> districts, boolean includeSupervisors, UserRole... userRoles);
List<UserReferenceDto> getUserRefsByDistricts(List<DistrictReferenceDto> districts, UserRight... userRights);

List<UserReferenceDto> getAllUserRefs(boolean includeInactive);

List<UserDto> getUsersByAssociatedOfficer(UserReferenceDto associatedOfficer, UserRole... userRoles);
List<UserDto> getUsersByAssociatedOfficer(UserReferenceDto associatedOfficer, UserRight... userRights);

List<String> getAllUuids();

Expand Down Expand Up @@ -107,6 +103,4 @@ public interface UserFacade {
List<UserReferenceDto> getUsersHavingTravelEntryInJurisdiction(TravelEntryReferenceDto travelEntryReferenceDto);

List<UserReferenceWithTaskNumbersDto> getAssignableUsersWithTaskNumbers(@NotNull TaskContextIndex taskContextIndex);

List<UserReferenceDto> getUsersByRegionAndRight(RegionReferenceDto region, UserRight userRight);
}
22 changes: 20 additions & 2 deletions sormas-api/src/main/java/de/symeda/sormas/api/user/UserRight.java
Expand Up @@ -40,7 +40,6 @@
import static de.symeda.sormas.api.user.UserRole.POE_SUPERVISOR;
import static de.symeda.sormas.api.user.UserRole.REST_EXTERNAL_VISITS_USER;
import static de.symeda.sormas.api.user.UserRole.REST_USER;
import static de.symeda.sormas.api.user.UserRole.SORMAS_TO_SORMAS_CLIENT;
import static de.symeda.sormas.api.user.UserRole.STATE_OBSERVER;
import static de.symeda.sormas.api.user.UserRole.SURVEILLANCE_OFFICER;
import static de.symeda.sormas.api.user.UserRole.SURVEILLANCE_SUPERVISOR;
Expand Down Expand Up @@ -231,6 +230,11 @@ public enum UserRight {
ADMIN,
ADMIN_SUPERVISOR
),
CASE_RESPONSIBLE(
SURVEILLANCE_SUPERVISOR,
ADMIN_SUPERVISOR,
SURVEILLANCE_OFFICER
),
IMMUNIZATION_VIEW(
ADMIN,
NATIONAL_USER,
Expand Down Expand Up @@ -592,6 +596,9 @@ public enum UserRight {
ADMIN,
ADMIN_SUPERVISOR
),
CONTACT_RESPONSIBLE(
CONTACT_OFFICER
),
MANAGE_EXTERNAL_SYMPTOM_JOURNAL(
NATIONAL_USER,
CONTACT_SUPERVISOR,
Expand Down Expand Up @@ -816,6 +823,10 @@ public enum UserRight {
NATIONAL_USER,
ADMIN_SUPERVISOR
),
EVENT_RESPONSIBLE(
SURVEILLANCE_SUPERVISOR,
SURVEILLANCE_OFFICER
),
EVENTPARTICIPANT_ARCHIVE(
ADMIN
),
Expand Down Expand Up @@ -1017,6 +1028,9 @@ public enum UserRight {
ADMIN,
ADMIN_SUPERVISOR
),
PERFORM_BULK_OPERATIONS_PSEUDONYM(
ADMIN
),
INFRASTRUCTURE_CREATE(
ADMIN
),
Expand Down Expand Up @@ -1568,6 +1582,9 @@ public enum UserRight {
),
EXTERNAL_VISITS(
REST_EXTERNAL_VISITS_USER
),
DEV_MODE(
ADMIN
);
//@formatter:on

Expand Down Expand Up @@ -1672,6 +1689,8 @@ public enum UserRight {
public static final String _PERFORM_BULK_OPERATIONS_EVENT = "PERFORM_BULK_OPERATIONS_EVENT";
public static final String _MANAGE_PUBLIC_EXPORT_CONFIGURATION = "MANAGE_PUBLIC_EXPORT_CONFIGURATION";
public static final String _PERFORM_BULK_OPERATIONS_CASE_SAMPLES = "PERFORM_BULK_OPERATIONS_CASE_SAMPLES";
public static final String _PERFORM_BULK_OPERATIONS_LAB_MESSAGES = "PERFORM_BULK_OPERATIONS_LAB_MESSAGES";
public static final String _PERFORM_BULK_OPERATIONS_PSEUDONYM = "PERFORM_BULK_OPERATIONS_PSEUDONYM";
public static final String _INFRASTRUCTURE_CREATE = "INFRASTRUCTURE_CREATE";
public static final String _INFRASTRUCTURE_EDIT = "INFRASTRUCTURE_EDIT";
public static final String _INFRASTRUCTURE_VIEW = "INFRASTRUCTURE_VIEW";
Expand Down Expand Up @@ -1720,7 +1739,6 @@ public enum UserRight {
public static final String _BAG_EXPORT = "BAG_EXPORT";
public static final String _SORMAS_TO_SORMAS_SHARE = "SORMAS_TO_SORMAS_SHARE";
public static final String _LAB_MESSAGES = "LAB_MESSAGES";
public static final String _PERFORM_BULK_OPERATIONS_LAB_MESSAGES = "PERFORM_BULK_OPERATIONS_LAB_MESSAGES";
public static final String _TRAVEL_ENTRY_MANAGEMENT_ACCESS = "TRAVEL_ENTRY_MANAGEMENT_ACCESS";
public static final String _TRAVEL_ENTRY_VIEW = "TRAVEL_ENTRY_VIEW";
public static final String _TRAVEL_ENTRY_CREATE = "TRAVEL_ENTRY_CREATE";
Expand Down
48 changes: 18 additions & 30 deletions sormas-api/src/main/java/de/symeda/sormas/api/user/UserRole.java
Expand Up @@ -34,7 +34,10 @@

/**
* These are also used as user groups in the server realm
*
* @Deprected until this is moved to DefaultUserRole in #4461
*/
@Deprecated
public enum UserRole
implements
StatisticsGroupingKey {
Expand Down Expand Up @@ -209,7 +212,7 @@ public enum UserRole
POE_NATIONAL_USER(false, false, false, true, JurisdictionLevel.NATION, Collections.emptyList(), Collections.emptyList()),
IMPORT_USER(false, false, false, false, JurisdictionLevel.NONE, Collections.emptyList(), Collections.emptyList()),
REST_EXTERNAL_VISITS_USER(false, false, false, false, JurisdictionLevel.NATION, Collections.emptyList(), Collections.emptyList()),
REST_USER(false, false, false, false, JurisdictionLevel.NONE, Collections.emptyList(), Collections.emptyList()),
REST_USER(false, false, false, false, JurisdictionLevel.NATION, Collections.emptyList(), Collections.emptyList()),
SORMAS_TO_SORMAS_CLIENT(false, false, false, false, JurisdictionLevel.NATION, Collections.emptyList(), Collections.emptyList()),
BAG_USER(false, false, false, false, JurisdictionLevel.NONE, Collections.emptyList(), Collections.emptyList());

Expand Down Expand Up @@ -282,14 +285,17 @@ public String toShortString() {
return I18nProperties.getEnumCaptionShort(this);
}

@Deprecated
public boolean isSupervisor() {
return supervisor;
}

@Deprecated
public boolean hasAssociatedOfficer() {
return hasAssociatedOfficer;
}

@Deprecated
public boolean isPortHealthUser() {
return portHealthUser;
}
Expand Down Expand Up @@ -427,16 +433,6 @@ public static JurisdictionLevel getJurisdictionLevel(Collection<UserRole> roles)
return laboratoryJurisdictionPresent ? JurisdictionLevel.LABORATORY : JurisdictionLevel.NONE;
}

public static boolean isSupervisor(Collection<UserRole> roles) {

for (UserRole role : roles) {
if (role.isSupervisor()) {
return true;
}
}
return false;
}

public static boolean hasAssociatedOfficer(Collection<UserRole> roles) {

for (UserRole role : roles) {
Expand Down Expand Up @@ -467,10 +463,6 @@ public static boolean isPortHealthUser(Collection<UserRole> roles) {
return false;
}

public static boolean isLabUser(Collection<UserRole> roles) {
return roles.contains(UserRole.LAB_USER) || roles.contains(UserRole.EXTERNAL_LAB_USER);
}

public static void validate(Collection<UserRole> roles) throws UserRoleValidationException {
UserRole previousCheckedRole = null;
for (UserRole userRole : roles) {
Expand Down Expand Up @@ -532,24 +524,20 @@ public static List<UserRole> getWithJurisdictionLevels(JurisdictionLevel... juri
return ret;
}

public static UserRole[] getWithEmailNotificationTypes(Set<NotificationType> notificationTypes) {
public static UserRole[] getWithNotificationTypes(NotificationProtocol notificationProtocol, Collection<NotificationType> notificationTypes) {
List<UserRole> ret = new ArrayList<>();

for (UserRole role : UserRole.values()) {
if (role.emailNotifications.stream().anyMatch(notificationTypes::contains)) {
ret.add(role);
if (notificationProtocol == NotificationProtocol.EMAIL) {
for (UserRole role : UserRole.values()) {
if (role.emailNotifications.stream().anyMatch(notificationTypes::contains)) {
ret.add(role);
}
}
}

return ret.toArray(new UserRole[] {});
}

public static UserRole[] getWithSmsNotificationTypes(Set<NotificationType> notificationTypes) {
List<UserRole> ret = new ArrayList<>();

for (UserRole role : UserRole.values()) {
if (role.emailNotifications.stream().anyMatch(notificationTypes::contains)) {
ret.add(role);
} else if (notificationProtocol == NotificationProtocol.SMS) {
for (UserRole role : UserRole.values()) {
if (role.smsNotifications.stream().anyMatch(notificationTypes::contains)) {
ret.add(role);
}
}
}

Expand Down
Expand Up @@ -17,6 +17,7 @@
*******************************************************************************/
package de.symeda.sormas.api.user;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -47,7 +48,26 @@ public interface UserRoleConfigFacade {
*/
Set<UserRight> getEffectiveUserRights(UserRole... userRoles);

/**
* Will fallback to default user rights for each role that has no configuration defined
*/
Set<UserRight> getEffectiveUserRights(Collection<UserRole> userRoles);

/**
* Will fallback to default user rights for each role that has no configuration defined
*/
Set<UserRole> getEffectiveUserRoles(UserRight... userRights);

boolean hasUserRight(Collection<UserRole> userRoles, UserRight userRight);

boolean hasAnyUserRight(Collection<UserRole> userRoles, Collection<UserRight> userRights);

/**
* Will fallback to default user rights for each role that has no configuration defined
*/
Set<UserRole> getEffectiveUserRoles(Collection<UserRight> userRights);

Set<UserRole> getEnabledUserRoles();

Map<UserRole, Set<UserRight>> getAllAsMap();
Map<UserRole, Set<UserRight>> getUserRoleRights();
}
2 changes: 1 addition & 1 deletion sormas-api/src/main/resources/captions.properties
Expand Up @@ -330,7 +330,7 @@ caseFilterPortHealthWithoutFacility=Only port health cases without a facility
caseFilterCasesWithCaseManagementData=Only cases with case management data
caseFilterWithDifferentRegion=Show duplicates with differing regions
caseFilterExcludeSharedCases=Exclude cases shared from other jurisdictions
caseFilterWithoutResponsibleOfficer=Only cases without responsible officer
caseFilterWithoutResponsibleUser=Only cases without responsible user
caseFilterWithExtendedQuarantine=Only cases with extended quarantine
caseFilterWithReducedQuarantine=Only cases with reduced quarantine
caseFilterOnlyQuarantineHelpNeeded=Help needed in quarantine
Expand Down
2 changes: 1 addition & 1 deletion sormas-api/src/main/resources/descriptions.properties
Expand Up @@ -67,7 +67,7 @@ descCaseFilterWithoutGeo = Only list cases that don't have address or report geo
descCaseFilterPortHealthWithoutFacility = Only list cases that origin from a point of entry and have not yet been referred to a facility
descCaseFilterCasesWithCaseManagementData = Only list cases that have at least one prescription, treatment or clinical visit
descCaseFilterExcludeSharedCases = Exclude all cases that do not originally belong to your jurisdiction, but have been shared with you by other users
descCaseFilterWithoutResponsibleOfficer = Only list cases that don't have responsible surveillance officer
descCaseFilterWithoutResponsibleUser = Only list cases that don't have responsible surveillance user
descCaseFilterWithExtendedQuarantine = Only list cases whose quarantine period has been extended
descContactOnlyWithExtendedQuarantine = Only list contacts whose quarantine period has been extended
descCaseFilterWithReducedQuarantine = Only list cases whose quarantine period has been reduced
Expand Down
3 changes: 3 additions & 0 deletions sormas-api/src/main/resources/enum.properties
Expand Up @@ -1272,6 +1272,7 @@ UserRight.CASE_INVESTIGATE = Edit case investigation status
UserRight.CASE_SEE_ARCHIVED = View archived cases
UserRight.CASE_TRANSFER = Transfer cases to another region/district/facility
UserRight.CASE_REFER_FROM_POE = Refer case from point of entry
UserRight.CASE_RESPONSIBLE = Can be responsible for a case
UserRight.CASE_VIEW = View existing cases
UserRight.CONTACT_ASSIGN = Assign contacts to officers
UserRight.CONTACT_CLASSIFY = Edit contact classification
Expand All @@ -1281,6 +1282,7 @@ UserRight.CONTACT_IMPORT = Import contacts
UserRight.CONTACT_DELETE = Delete contacts from the system
UserRight.CONTACT_EDIT = Edit existing contacts
UserRight.CONTACT_EXPORT = Export contacts from SORMAS
UserRight.CONTACT_RESPONSIBLE = Can be responsible for a contact
UserRight.CONTACT_SEE_ARCHIVED = View archived contacts
UserRight.CONTACT_VIEW = View existing contacts
UserRight.CONTACT_ARCHIVE = Archive contacts
Expand All @@ -1291,6 +1293,7 @@ UserRight.EVENT_ARCHIVE = Archive events
UserRight.EVENT_CREATE = Create new events
UserRight.EVENT_EDIT = Edit existing events
UserRight.EVENT_EXPORT = Export events from SORMAS
UserRight.EVENT_RESPONSIBLE = Can be responsible for an event
UserRight.EVENT_SEE_ARCHIVED = View archived events
UserRight.EVENT_VIEW = View existing events
UserRight.EVENTPARTICIPANT_CREATE = Create new event participants
Expand Down
Expand Up @@ -44,21 +44,13 @@ public void testUserRolesCombinationValidity() {
UserRole.POE_NATIONAL_USER,
UserRole.REST_EXTERNAL_VISITS_USER);

assertValidRolesCombination(
UserRole.NATIONAL_USER,
UserRole.LAB_USER);
assertValidRolesCombination(UserRole.NATIONAL_USER, UserRole.LAB_USER);

assertValidRolesCombination(
UserRole.ADMIN,
UserRole.LAB_USER);
assertValidRolesCombination(UserRole.ADMIN, UserRole.LAB_USER);

assertInvalidRolesCombination(
UserRole.LAB_USER,
UserRole.EXTERNAL_LAB_USER);
assertInvalidRolesCombination(UserRole.LAB_USER, UserRole.EXTERNAL_LAB_USER);

assertInvalidRolesCombination(
UserRole.NATIONAL_USER,
UserRole.EXTERNAL_LAB_USER);
assertInvalidRolesCombination(UserRole.NATIONAL_USER, UserRole.EXTERNAL_LAB_USER);

assertValidRolesCombination(
UserRole.SURVEILLANCE_SUPERVISOR,
Expand All @@ -68,12 +60,8 @@ public void testUserRolesCombinationValidity() {
UserRole.STATE_OBSERVER,
UserRole.POE_SUPERVISOR);

assertValidRolesCombination(
UserRole.SURVEILLANCE_OFFICER,
UserRole.CASE_OFFICER,
UserRole.CONTACT_OFFICER,
UserRole.DISTRICT_OBSERVER);

assertValidRolesCombination(UserRole.SURVEILLANCE_OFFICER, UserRole.CASE_OFFICER, UserRole.CONTACT_OFFICER, UserRole.DISTRICT_OBSERVER);

assertValidRolesCombination(UserRole.HOSPITAL_INFORMANT);

assertValidRolesCombination(UserRole.COMMUNITY_INFORMANT, UserRole.IMPORT_USER);
Expand All @@ -84,18 +72,17 @@ public void testUserRolesCombinationValidity() {
}

@Test
public void testUserRolesJurisdiction(){
public void testUserRolesJurisdiction() {

assertJurisdictionForRoles(JurisdictionLevel.NONE, UserRole.ADMIN, UserRole.REST_USER);
assertJurisdictionForRoles(JurisdictionLevel.NONE, UserRole.ADMIN);
assertJurisdictionForRoles(JurisdictionLevel.NATION, UserRole.ADMIN, UserRole.REST_USER, UserRole.NATIONAL_CLINICIAN);
assertJurisdictionForRoles(JurisdictionLevel.DISTRICT, UserRole.ADMIN, UserRole.REST_USER, UserRole.DISTRICT_OBSERVER);
assertJurisdictionForRoles(JurisdictionLevel.NATION, UserRole.NATIONAL_USER, UserRole.LAB_USER);
assertJurisdictionForRoles(JurisdictionLevel.DISTRICT, UserRole.ADMIN, UserRole.DISTRICT_OBSERVER);
assertJurisdictionForRoles(JurisdictionLevel.NATION, UserRole.NATIONAL_USER, UserRole.LAB_USER, UserRole.REST_USER, UserRole.ADMIN);
assertJurisdictionForRoles(JurisdictionLevel.REGION, UserRole.CASE_SUPERVISOR);
assertJurisdictionForRoles(JurisdictionLevel.LABORATORY, UserRole.LAB_USER);
assertJurisdictionForRoles(JurisdictionLevel.LABORATORY, UserRole.ADMIN, UserRole.LAB_USER);
}


private void assertJurisdictionForRoles(final JurisdictionLevel jurisdictionLevel, final UserRole... userRoles) {
Assert.assertEquals(jurisdictionLevel, UserRole.getJurisdictionLevel(Arrays.asList(userRoles)));
}
Expand Down

0 comments on commit 287f64b

Please sign in to comment.