X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms.e4%2Fsrc%2Forg%2Fargeo%2Fcms%2Fe4%2Faddons%2FAuthAddon.java;fp=org.argeo.cms.e4%2Fsrc%2Forg%2Fargeo%2Fcms%2Fe4%2Faddons%2FAuthAddon.java;h=6f03139ca4d6aaa96c8359ee86d5660c39a6254c;hb=54fb8849152a3d91f7ba52aeeffb236f5e70fbdf;hp=0000000000000000000000000000000000000000;hpb=9eaf85002b003a68eea81fca417465ba52f14e4e;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms.e4/src/org/argeo/cms/e4/addons/AuthAddon.java b/org.argeo.cms.e4/src/org/argeo/cms/e4/addons/AuthAddon.java new file mode 100644 index 000000000..6f03139ca --- /dev/null +++ b/org.argeo.cms.e4/src/org/argeo/cms/e4/addons/AuthAddon.java @@ -0,0 +1,77 @@ +package org.argeo.cms.e4.addons; + +import java.util.Iterator; + +import javax.annotation.PostConstruct; + +import org.argeo.cms.auth.CurrentUser; +import org.eclipse.e4.ui.model.application.MApplication; +import org.eclipse.e4.ui.model.application.ui.MElementContainer; +import org.eclipse.e4.ui.model.application.ui.MUIElement; +import org.eclipse.e4.ui.model.application.ui.basic.MTrimBar; +import org.eclipse.e4.ui.model.application.ui.basic.MTrimmedWindow; +import org.eclipse.e4.ui.model.application.ui.basic.MWindow; + +public class AuthAddon { + public final static String AUTH = "auth."; + + @PostConstruct + void init(MApplication application) { + Iterator windows = application.getChildren().iterator(); + windows: while (windows.hasNext()) { + MWindow window = windows.next(); + // main window + boolean windowVisible = process(window); + if (!windowVisible) { + windows.remove(); + continue windows; + } + // trim bars + if (window instanceof MTrimmedWindow) { + Iterator trimBars = ((MTrimmedWindow) window).getTrimBars().iterator(); + while (trimBars.hasNext()) { + MTrimBar trimBar = trimBars.next(); + if (!process(trimBar)) { + trimBars.remove(); + } + } + } + } + } + + protected boolean process(MUIElement element) { + for (String tag : element.getTags()) { + if (tag.startsWith(AUTH)) { + String role = tag.substring(AUTH.length(), tag.length()); + if (!CurrentUser.isInRole(role)) { + element.setVisible(false); + element.setToBeRendered(false); + return false; + } + } + } + + // children + if (element instanceof MElementContainer) { + @SuppressWarnings("unchecked") + MElementContainer container = (MElementContainer) element; + Iterator children = container.getChildren().iterator(); + while (children.hasNext()) { + MUIElement child = children.next(); + boolean visible = process(child); + if (!visible) + children.remove(); + } + + for (Object child : container.getChildren()) { + if (child instanceof MUIElement) { + boolean visible = process((MUIElement) child); + if (!visible) + container.getChildren().remove(child); + } + } + } + + return true; + } +}