Skip to content

Commit

Permalink
Merge pull request #13026 from SORMAS-Foundation/#4659-Reduce_userPro…
Browse files Browse the repository at this point in the history
…vider_getCurrent_warning

#4659 - Use the methods from UiUtil.java
  • Loading branch information
leventegal-she authored Mar 13, 2024
2 parents 327a4d3 + 37d5051 commit 31ee727
Show file tree
Hide file tree
Showing 242 changed files with 1,271 additions and 1,515 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import de.symeda.sormas.api.utils.LocationHelper;
import de.symeda.sormas.api.utils.fieldaccess.UiFieldAccessCheckers;
import de.symeda.sormas.api.utils.fieldvisibility.FieldVisibilityCheckers;
import de.symeda.sormas.ui.UserProvider;
import de.symeda.sormas.ui.UiUtil;
import de.symeda.sormas.ui.caze.AbstractTableField;
import de.symeda.sormas.ui.utils.CommitDiscardWrapperComponent;
import de.symeda.sormas.ui.utils.DateFormatHelper;
Expand Down Expand Up @@ -164,7 +164,7 @@ protected void editEntry(ActivityAsCaseDto entry, boolean create, Consumer<Activ

final CommitDiscardWrapperComponent<ActivityAsCaseForm> component = new CommitDiscardWrapperComponent<>(
activityAsCaseForm,
UserProvider.getCurrent().hasUserRight(UserRight.CASE_EDIT) && isEditAllowed,
UiUtil.permitted(isEditAllowed, UserRight.CASE_EDIT),
activityAsCaseForm.getFieldGroup());
component.getCommitButton().setCaption(I18nProperties.getString(Strings.done));

Expand Down Expand Up @@ -193,7 +193,7 @@ protected void editEntry(ActivityAsCaseDto entry, boolean create, Consumer<Activ

@Override
protected ActivityAsCaseDto createEntry() {
UserDto user = UserProvider.getCurrent().getUser();
UserDto user = UiUtil.getUser();
ActivityAsCaseDto activityAsCase = ActivityAsCaseDto.build(null);
activityAsCase.getLocation().setRegion(user.getRegion());
activityAsCase.getLocation().setDistrict(user.getDistrict());
Expand Down
10 changes: 5 additions & 5 deletions sormas-ui/src/main/java/de/symeda/sormas/ui/MainScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public View getView(String viewName) {
VaadinIcons.AIRPLANE);
}

if (FacadeProvider.getFeatureConfigurationFacade().isFeatureEnabled(FeatureType.SORMAS_TO_SORMAS_ACCEPT_REJECT)
if (UiUtil.enabled(FeatureType.SORMAS_TO_SORMAS_ACCEPT_REJECT)
&& FacadeProvider.getSormasToSormasFacade().isProcessingShareEnabledForUser()) {
ControllerProvider.getSormasToSormasController().registerViews(navigator);
menu.addView(
Expand Down Expand Up @@ -336,13 +336,13 @@ public View getView(String viewName) {
VaadinIcons.BAR_CHART);
}

if (UserProvider.getCurrent().hasUserAccess()) {
if (UiUtil.hasUserAccess()) {
AbstractUserView.registerViews(navigator);

menu.addView(UsersView.class, AbstractUserView.ROOT_VIEW_NAME, I18nProperties.getCaption(Captions.mainMenuUsers), VaadinIcons.USERS);
}

if (UserProvider.getCurrent().hasConfigurationAccess()) {
if (UiUtil.hasConfigurationAccess()) {
Class<? extends AbstractConfigurationView> firstAccessibleView = AbstractConfigurationView.registerViews(navigator);
menu.addView(
firstAccessibleView,
Expand All @@ -356,8 +356,8 @@ public View getView(String viewName) {

// Add GDPR window
// possible to desactivate it with check
UserDto user = UserProvider.getCurrent().getUser();
if (FacadeProvider.getFeatureConfigurationFacade().isFeatureEnabled(FeatureType.GDPR_CONSENT_POPUP) && !user.isHasConsentedToGdpr()) {
UserDto user = UiUtil.getUser();
if (UiUtil.enabled(FeatureType.GDPR_CONSENT_POPUP) && !user.isHasConsentedToGdpr()) {
Window subWindowGdpR = new Window(I18nProperties.getPrefixCaption(UserDto.I18N_PREFIX, UserDto.HAS_CONSENTED_TO_GDPR));
VerticalLayout subContentGdpr = new VerticalLayout();
subWindowGdpR.setContent(subContentGdpr);
Expand Down
18 changes: 9 additions & 9 deletions sormas-ui/src/main/java/de/symeda/sormas/ui/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
*******************************************************************************/
package de.symeda.sormas.ui;

import com.vaadin.event.ContextClickEvent;
import com.vaadin.event.MouseEvents;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.vaadin.icons.VaadinIcons;
import com.vaadin.navigator.Navigator;
import com.vaadin.navigator.View;
Expand All @@ -36,6 +41,7 @@
import com.vaadin.ui.UI;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.ValoTheme;

import de.symeda.sormas.api.FacadeProvider;
import de.symeda.sormas.api.i18n.Captions;
import de.symeda.sormas.api.i18n.I18nProperties;
Expand All @@ -47,12 +53,6 @@
import de.symeda.sormas.ui.utils.CommitDiscardWrapperComponent;
import de.symeda.sormas.ui.utils.CssStyles;
import de.symeda.sormas.ui.utils.VaadinUiUtil;
import org.apache.commons.lang3.StringUtils;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

/**
* Responsive navigation menu presenting a list of available views to the user.
Expand Down Expand Up @@ -126,7 +126,7 @@ public Menu(Navigator navigator) {
MenuBar logoutMenu = new MenuBar();
logoutMenu.setId(Captions.actionLogout);
logoutMenu.addItem(
I18nProperties.getCaption(Captions.actionLogout) + " (" + UserProvider.getCurrent().getUserName() + ")",
I18nProperties.getCaption(Captions.actionLogout) + " (" + UiUtil.getUserName() + ")",
VaadinIcons.SIGN_OUT,
(Command) selectedItem -> LoginHelper.logout());

Expand Down
95 changes: 93 additions & 2 deletions sormas-ui/src/main/java/de/symeda/sormas/ui/UiUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,94 @@
import java.util.Objects;
import java.util.Set;

import de.symeda.sormas.api.EditPermissionType;
import de.symeda.sormas.api.FacadeProvider;
import de.symeda.sormas.api.common.DeletableEntityType;
import de.symeda.sormas.api.feature.FeatureType;
import de.symeda.sormas.api.user.JurisdictionLevel;
import de.symeda.sormas.api.user.PseudonymizableDataAccessLevel;
import de.symeda.sormas.api.user.UserDto;
import de.symeda.sormas.api.user.UserReferenceDto;
import de.symeda.sormas.api.user.UserRight;
import de.symeda.sormas.api.user.UserRoleDto;

public class UiUtil {

private UiUtil() {
}

public static UserDto getUser() {
return Objects.nonNull(getCurrentUserProvider()) ? getCurrentUserProvider().getUser() : null;
}

public static UserReferenceDto getUserReference() {
return Objects.nonNull(getCurrentUserProvider()) ? getCurrentUserProvider().getUserReference() : null;
}

public static JurisdictionLevel getJurisdictionLevel() {
return Objects.nonNull(getCurrentUserProvider()) ? getCurrentUserProvider().getJurisdictionLevel() : null;
}

public static String getUserName() {
return Objects.nonNull(getCurrentUserProvider()) ? getCurrentUserProvider().getUserName() : null;
}

public static String getUserUuid() {
return Objects.nonNull(getCurrentUserProvider()) ? getCurrentUserProvider().getUuid() : null;
}

public static Set<UserRoleDto> getUserRoles() {
return Objects.nonNull(getCurrentUserProvider()) ? getCurrentUserProvider().getUserRoles() : null;
}

public static Set<UserRight> getUserRights() {
return Objects.nonNull(getCurrentUserProvider()) ? getCurrentUserProvider().getUserRights() : null;
}

public static PseudonymizableDataAccessLevel getPseudonymizableDataAccessLevel(boolean inJurisdiction) {
return Objects.nonNull(getCurrentUserProvider()) ? getCurrentUserProvider().getPseudonymizableDataAccessLevel(inJurisdiction) : null;
}

public static boolean hasUserAccess() {
return Objects.nonNull(getCurrentUserProvider()) && getCurrentUserProvider().hasUserAccess();
}

public static boolean hasOptionalHealthFacility() {
return Objects.nonNull(getCurrentUserProvider()) && getCurrentUserProvider().hasOptionalHealthFacility();
}

public static boolean hasConfigurationAccess() {
return Objects.nonNull(getCurrentUserProvider()) && getCurrentUserProvider().hasConfigurationAccess();
}

public static boolean hasRegionJurisdictionLevel() {
return Objects.nonNull(getCurrentUserProvider()) && getCurrentUserProvider().hasRegionJurisdictionLevel();
}

public static boolean hasNationJurisdictionLevel() {
return Objects.nonNull(getCurrentUserProvider()) && getCurrentUserProvider().hasNationJurisdictionLevel();
}

public static boolean hasNoneJurisdictionLevel() {
return Objects.nonNull(getCurrentUserProvider()) && getCurrentUserProvider().hasNoneJurisdictionLevel();
}

public static boolean hasLaboratoryOrExternalLaboratoryJurisdictionLevel() {
return Objects.nonNull(getCurrentUserProvider()) && getCurrentUserProvider().hasLaboratoryOrExternalLaboratoryJurisdictionLevel();
}

public static boolean isPortHealthUser() {
return Objects.nonNull(getCurrentUserProvider()) && getCurrentUserProvider().isPortHealthUser();
}

public static boolean isAdmin() {
return Objects.nonNull(getCurrentUserProvider()) && getCurrentUserProvider().isAdmin();
}

public static UserProvider getCurrentUserProvider() {
return UserProvider.getCurrent();
}

public static boolean permitted(FeatureType feature, UserRight userRight) {
return (feature == null || enabled(feature)) && (userRight == null || permitted(userRight));
}
Expand All @@ -26,11 +104,24 @@ public static boolean permitted(FeatureType feature, UserRight... userRights) {
}

public static boolean permitted(UserRight userRight) {
return Objects.nonNull(UserProvider.getCurrent()) && UserProvider.getCurrent().hasUserRight(userRight);
return Objects.nonNull(getCurrentUserProvider()) && getCurrentUserProvider().hasUserRight(userRight);
}

public static boolean permitted(UserRight... userRights) {
return Objects.nonNull(UserProvider.getCurrent()) && UserProvider.getCurrent().hasAllUserRights(userRights);
return Objects.nonNull(getCurrentUserProvider()) && getCurrentUserProvider().hasAllUserRights(userRights);
}

public static boolean permitted(boolean isActionAllowed, UserRight userRight) {
return Objects.nonNull(getCurrentUserProvider()) && getCurrentUserProvider().hasUserRightWithAllowedFlag(isActionAllowed, userRight);
}

public static boolean permitted(boolean isActionAllowed, UserRight... userRights) {
return Objects.nonNull(getCurrentUserProvider()) && getCurrentUserProvider().hasAllUserRightsWithAllowedFlag(isActionAllowed, userRights);
}

public static boolean permitted(EditPermissionType editPermissionType, UserRight userRight) {
return Objects.nonNull(getCurrentUserProvider())
&& getCurrentUserProvider().hasUserRightWithEditPermissionType(editPermissionType, userRight);
}

public static boolean enabled(FeatureType featureType) {
Expand Down
19 changes: 12 additions & 7 deletions sormas-ui/src/main/java/de/symeda/sormas/ui/UserProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import com.vaadin.ui.UI;

import de.symeda.sormas.api.EditPermissionType;
import de.symeda.sormas.api.FacadeProvider;
import de.symeda.sormas.api.i18n.I18nProperties;
import de.symeda.sormas.api.infrastructure.region.RegionReferenceDto;
Expand Down Expand Up @@ -94,16 +95,20 @@ public boolean hasUserRight(UserRight userRight) {
return getUserRights().contains(userRight);
}

public boolean hasAnyRight(Set<UserRight> userRight) {
return getUserRights().stream().anyMatch(userRight::contains);
}

public boolean hasAllUserRights(UserRight... userRights) {
return getUserRights().containsAll(Arrays.asList(userRights));
}

public boolean hasAllUserRightsWithEditAllowedFlag(boolean isEditAllowed, UserRight... userRights) {
return isEditAllowed && getUserRights().containsAll(Arrays.asList(userRights));
public boolean hasUserRightWithAllowedFlag(boolean isActionAllowed, UserRight userRight) {
return isActionAllowed && hasUserRight(userRight);
}

public boolean hasAllUserRightsWithAllowedFlag(boolean isActionAllowed, UserRight... userRights) {
return isActionAllowed && getUserRights().containsAll(Arrays.asList(userRights));
}

public boolean hasUserRightWithEditPermissionType(EditPermissionType editPermissionType, UserRight userRight) {
return (editPermissionType == null || editPermissionType == EditPermissionType.ALLOWED) && hasUserRight(userRight);
}

public boolean hasNationJurisdictionLevel() {
Expand Down Expand Up @@ -132,7 +137,7 @@ public boolean hasExternalLaboratoryJurisdictionLevel() {
}

public boolean hasRegion(RegionReferenceDto regionReference) {
RegionReferenceDto userRegionReference = getCurrent().getUser().getRegion();
RegionReferenceDto userRegionReference = UiUtil.getUser().getRegion();
return Objects.equals(userRegionReference, regionReference);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import de.symeda.sormas.api.i18n.I18nProperties;
import de.symeda.sormas.api.i18n.Strings;
import de.symeda.sormas.api.user.UserRight;
import de.symeda.sormas.ui.UserProvider;
import de.symeda.sormas.ui.UiUtil;
import de.symeda.sormas.ui.utils.CommitDiscardWrapperComponent;
import de.symeda.sormas.ui.utils.VaadinUiUtil;

Expand All @@ -50,10 +50,8 @@ public void create(ActionContext context, ReferenceDto entityRef, Runnable callb

ActionEditForm createForm = new ActionEditForm(true);
createForm.setValue(createNewAction(context, entityRef));
final CommitDiscardWrapperComponent<ActionEditForm> editView = new CommitDiscardWrapperComponent<>(
createForm,
UserProvider.getCurrent().hasUserRight(UserRight.ACTION_CREATE),
createForm.getFieldGroup());
final CommitDiscardWrapperComponent<ActionEditForm> editView =
new CommitDiscardWrapperComponent<>(createForm, UiUtil.permitted(UserRight.ACTION_CREATE), createForm.getFieldGroup());

editView.addCommitListener(() -> {
if (!createForm.getFieldGroup().isModified()) {
Expand Down Expand Up @@ -96,7 +94,7 @@ public void edit(ActionDto dto, Runnable callback) {
});

// Add delete button if user has role
if (UserProvider.getCurrent().hasUserRight(UserRight.ACTION_DELETE)) {
if (UiUtil.permitted(UserRight.ACTION_DELETE)) {
editView.addDeleteListener(() -> {
FacadeProvider.getActionFacade().deleteAction(newDto);
UI.getCurrent().removeWindow(popupWindow);
Expand All @@ -116,7 +114,7 @@ public void edit(ActionDto dto, Runnable callback) {

private ActionDto createNewAction(ActionContext context, ReferenceDto entityRef) {
ActionDto action = ActionDto.build(context, entityRef);
action.setCreatorUser(UserProvider.getCurrent().getUserReference());
action.setCreatorUser(UiUtil.getUserReference());
return action;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import de.symeda.sormas.api.i18n.Captions;
import de.symeda.sormas.api.i18n.I18nProperties;
import de.symeda.sormas.api.user.UserDto;
import de.symeda.sormas.ui.UserProvider;
import de.symeda.sormas.ui.UiUtil;
import de.symeda.sormas.ui.utils.AbstractEditForm;
import de.symeda.sormas.ui.utils.CssStyles;
import de.symeda.sormas.ui.utils.DateFormatHelper;
Expand Down Expand Up @@ -149,7 +149,7 @@ private void updateByCreating() {
if (value != null) {
boolean creating = value.getCreationDate() == null;

UserDto user = UserProvider.getCurrent().getUser();
UserDto user = UiUtil.getUser();
boolean creator = user.equals(value.getCreatorUser());

setVisible(!creating, ActionDto.REPLY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.ValoTheme;

import de.symeda.sormas.api.FacadeProvider;
import de.symeda.sormas.api.action.ActionDto;
import de.symeda.sormas.api.action.ActionPriority;
import de.symeda.sormas.api.action.ActionStatus;
Expand All @@ -39,7 +38,7 @@
import de.symeda.sormas.api.user.UserRight;
import de.symeda.sormas.api.utils.DataHelper;
import de.symeda.sormas.api.utils.HtmlHelper;
import de.symeda.sormas.ui.UserProvider;
import de.symeda.sormas.ui.UiUtil;
import de.symeda.sormas.ui.document.DocumentListComponent;
import de.symeda.sormas.ui.utils.ButtonHelper;
import de.symeda.sormas.ui.utils.CssStyles;
Expand Down Expand Up @@ -179,8 +178,7 @@ public ActionListEntry(ActionDto action) {
priorityLabel.addStyleName(statusStyle);
}

if (FacadeProvider.getFeatureConfigurationFacade().isFeatureEnabled(FeatureType.DOCUMENTS)
&& UserProvider.getCurrent().hasUserRight(UserRight.DOCUMENT_VIEW)) {
if (UiUtil.permitted(FeatureType.DOCUMENTS, UserRight.DOCUMENT_VIEW)) {
VerticalLayout documentLayout = new VerticalLayout();
documentLayout.setMargin(false);
documentLayout.setSpacing(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import de.symeda.sormas.api.user.UserRight;
import de.symeda.sormas.api.utils.DataHelper;
import de.symeda.sormas.ui.ControllerProvider;
import de.symeda.sormas.ui.UserProvider;
import de.symeda.sormas.ui.UiUtil;
import de.symeda.sormas.ui.utils.ButtonHelper;
import de.symeda.sormas.ui.utils.CssStyles;

Expand Down Expand Up @@ -76,7 +76,7 @@ public ActionStatsComponent(ActionContext context, EventReferenceDto entityRef)
actionsHeader.addStyleName(CssStyles.H3);
componentHeader.addComponent(actionsHeader);

if (UserProvider.getCurrent().hasUserRight(UserRight.ACTION_CREATE)) {
if (UiUtil.permitted(UserRight.ACTION_CREATE)) {
createButton = ButtonHelper.createIconButton(
Captions.actionNewAction,
VaadinIcons.PLUS_CIRCLE,
Expand Down
Loading

0 comments on commit 31ee727

Please sign in to comment.