Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[26928] hide preferencePage based on user roles #791

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import ch.elexis.core.ui.UiDesk;
import ch.elexis.core.ui.actions.GlobalActions;
import ch.elexis.core.ui.constants.UiResourceConstants;
import ch.elexis.core.ui.e4.util.CoreUiUtil;
import ch.rgw.tools.ExHandler;

/**
Expand All @@ -54,6 +55,7 @@ public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(final IWorkbenchWindo
*/
@Override
public void initialize(final IWorkbenchConfigurer configurer) {
CoreUiUtil.hidePreferencePage();
Hub.pin.initializeDisplayPreferences(UiDesk.getDisplay());
configurer.setSaveAndRestore(true);
super.initialize(configurer);
Expand Down
3 changes: 2 additions & 1 deletion bundles/ch.elexis.core.ui.e4/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ Require-Bundle: javax.annotation;bundle-version="1.3.5";visibility:=reexport,
org.eclipse.core.databinding.beans;visibility:=reexport,
org.eclipse.core.databinding.property;visibility:=reexport,
org.eclipse.jface.databinding,
com.ibm.icu
com.ibm.icu,
org.eclipse.ui.workbench
Bundle-RequiredExecutionEnvironment: JavaSE-21
Service-Component: OSGI-INF/ch.elexis.core.ui.e4.util.CoreUiUtil.xml
Bundle-ActivationPolicy: lazy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
Expand All @@ -14,6 +15,8 @@
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.workbench.UIEvents;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.preference.IPreferenceNode;
import org.eclipse.jface.preference.PreferenceManager;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.StructuredViewer;
Expand All @@ -32,6 +35,7 @@
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.PlatformUI;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.service.component.annotations.Component;
Expand All @@ -42,7 +46,12 @@
import org.slf4j.LoggerFactory;

import ch.elexis.core.model.IImage;
import ch.elexis.core.model.IRole;
import ch.elexis.core.model.ISticker;
import ch.elexis.core.model.IUser;
import ch.elexis.core.model.RoleConstants;
import ch.elexis.core.services.IContextService;
import ch.elexis.core.utils.OsgiServiceUtil;

@Component(property = EventConstants.EVENT_TOPIC + "=" + UIEvents.UILifeCycle.APP_STARTUP_COMPLETE)
public class CoreUiUtil implements EventHandler {
Expand All @@ -57,6 +66,10 @@ public class CoreUiUtil implements EventHandler {

private static IEclipseContext serviceContext;

private static final List<IPreferenceNode> hiddenNodes = new ArrayList<>();

private static final String ALLOWED_PREFERENCE_PAGE_ID = "ch.elexis.preferences.UserPreferences";

@Override
public void handleEvent(Event event) {
logger.info("APPLICATION STARTUP COMPLETE"); //$NON-NLS-1$
Expand Down Expand Up @@ -424,4 +437,39 @@ public static int compareNullSafe(Object o1, Object o2) {
return o1 == null ? (o2 == null ? 0 : -1) : (o2 == null ? 1 : Integer.MAX_VALUE);

}

/**
* Shows or hides preference pages based on the active user's roles.
* {@link RoleConstants}
*/
public static void hidePreferencePage() {
Set<String> authorizedRoles = Set.of(RoleConstants.ACCESSCONTROLE_ROLE_ICT_ADMINISTRATOR,
RoleConstants.ACCESSCONTROLE_ROLE_POWERUSER, RoleConstants.ACCESSCONTROLE_ROLE_MPK,
RoleConstants.ACCESSCONTROLE_ROLE_MANDATOR);

IContextService contextService = OsgiServiceUtil.getService(IContextService.class)
.orElseThrow(() -> new IllegalStateException());
IUser user = contextService.getActiveUser().orElseThrow(() -> new IllegalStateException("No active user"));

boolean hidePreferencePage = user.getRoles().stream().map(IRole::getId).noneMatch(authorizedRoles::contains);
if (!hidePreferencePage && hiddenNodes.isEmpty()) {
return;
}

PreferenceManager pm = PlatformUI.getWorkbench().getPreferenceManager();
IPreferenceNode[] nodes = pm.getRootSubNodes();
PlatformUI.getWorkbench().getDisplay().asyncExec(() -> {
if (hidePreferencePage) {
for (IPreferenceNode node : nodes) {
if (!node.getId().equals(ALLOWED_PREFERENCE_PAGE_ID)) {
hiddenNodes.add(node);
pm.remove(node);
}
}
} else {
hiddenNodes.forEach(pm::addToRoot);
hiddenNodes.clear();
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
import ch.elexis.core.ui.dialogs.DateSelectorDialog;
import ch.elexis.core.ui.dialogs.NeuerFallDialog;
import ch.elexis.core.ui.dialogs.SelectFallDialog;
import ch.elexis.core.ui.e4.util.CoreUiUtil;
import ch.elexis.core.ui.icons.Images;
import ch.elexis.core.ui.locks.LockedAction;
import ch.elexis.core.ui.locks.LockedRestrictedAction;
Expand Down Expand Up @@ -370,6 +371,7 @@ public void doRun() {
// reset after login
if (loginUserName != null) {
System.setProperty(ElexisSystemPropertyConstants.LOGIN_USERNAME, loginUserName);
CoreUiUtil.hidePreferencePage();
}
if (!performLogin) {
exitAction.run();
Expand Down