Merge remote-tracking branch 'origin/master' into v2.x
authorMathieu Baudier <mbaudier@argeo.org>
Sun, 7 Feb 2021 09:23:00 +0000 (10:23 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Sun, 7 Feb 2021 09:23:00 +0000 (10:23 +0100)
core/org.argeo.suite.core/src/org/argeo/suite/SuiteUtils.java
core/org.argeo.suite.ui/src/org/argeo/suite/ui/DefaultHeader.java
core/org.argeo.suite.ui/src/org/argeo/suite/ui/DefaultLeadPane.java
core/org.argeo.suite.ui/src/org/argeo/suite/ui/SuiteUiUtils.java

index e63b5152c0b00430aad95bd83d5b12e2d392b7b5..93f3b148c085d3ba1ba0c3dbe54e29d64aa597d4 100644 (file)
@@ -1,5 +1,8 @@
 package org.argeo.suite;
 
+import java.util.HashSet;
+import java.util.Set;
+
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
@@ -96,4 +99,24 @@ public class SuiteUtils {
 
        }
 
+       public static Set<String> extractRoles(String[] semiColArr) {
+               Set<String> res = new HashSet<>();
+               // TODO factorize and make it more robust
+               final String rolesPrefix = "roles:=\"";
+               // first one is layer id
+               for (int i = 1; i < semiColArr.length; i++) {
+                       if (semiColArr[i].startsWith(rolesPrefix)) {
+                               String rolesStr = semiColArr[i].substring(rolesPrefix.length());
+                               // remove last "
+                               rolesStr = rolesStr.substring(0, rolesStr.lastIndexOf('\"'));
+                               // TODO support AND (&) as well
+                               String[] roles = rolesStr.split("\\|");// OR (|)
+                               for (String role : roles) {
+                                       res.add(role.trim());
+                               }
+                       }
+               }
+               return res;
+       }
+
 }
index a251e14c78ffa0de572f661a56c524e106e11e9a..692a23badf2c4c1f3c5d2446420a5404ddb676d5 100644 (file)
@@ -26,6 +26,7 @@ import org.eclipse.swt.widgets.Label;
 import org.osgi.service.cm.ConfigurationException;
 import org.osgi.service.cm.ManagedService;
 
+/** HEader of a standard Argeo Suite applicaiton. */
 public class DefaultHeader implements CmsUiProvider, ManagedService {
        public final static String TITLE_PROPERTY = "argeo.suite.ui.header.title";
        private Map<String, String> properties;
index a207e7a7453da85272bd20a88efedb183256d111..9a5248e7db8d85d2cc1f2fd1dd22806ea633f846 100644 (file)
@@ -2,7 +2,10 @@ package org.argeo.suite.ui;
 
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.TreeMap;
 
 import javax.jcr.Node;
@@ -10,16 +13,23 @@ import javax.jcr.RepositoryException;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.argeo.api.NodeConstants;
+import org.argeo.cms.LocaleUtils;
 import org.argeo.cms.Localized;
 import org.argeo.cms.auth.CurrentUser;
+import org.argeo.cms.ui.CmsTheme;
 import org.argeo.cms.ui.CmsUiProvider;
 import org.argeo.cms.ui.CmsView;
+import org.argeo.cms.ui.util.CmsIcon;
+import org.argeo.cms.ui.util.CmsUiUtils;
 import org.argeo.suite.RankedObject;
+import org.argeo.suite.SuiteUtils;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Label;
 import org.osgi.framework.Constants;
 
 /** Side pane listing various perspectives. */
@@ -31,64 +41,100 @@ public class DefaultLeadPane implements CmsUiProvider {
        }
 
        private Map<String, RankedObject<SuiteLayer>> layers = Collections.synchronizedSortedMap(new TreeMap<>());
-       private String[] defaultLayers;
-       private String[] adminLayers;
+       private List<String> defaultLayers;
+       private List<String> adminLayers;
 
        @Override
        public Control createUi(Composite parent, Node node) throws RepositoryException {
                CmsView cmsView = CmsView.getCmsView(parent);
+               parent.setLayout(CmsUiUtils.noSpaceGridLayout());
+               Composite appLayersC = new Composite(parent, SWT.NONE);
+               CmsUiUtils.style(appLayersC, SuiteStyle.leadPane);
                GridLayout layout = new GridLayout();
                layout.verticalSpacing = 10;
                layout.marginTop = 10;
                layout.marginLeft = 10;
                layout.marginRight = 10;
-               parent.setLayout(layout);
-
+               appLayersC.setLayout(layout);
+               appLayersC.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false));
+
+               Composite adminLayersC = new Composite(parent, SWT.NONE);
+               CmsUiUtils.style(adminLayersC, SuiteStyle.leadPane);
+               GridLayout adminLayout = new GridLayout();
+               adminLayout.verticalSpacing = 10;
+               adminLayout.marginBottom = 10;
+               adminLayout.marginLeft = 10;
+               adminLayout.marginRight = 10;
+               adminLayersC.setLayout(adminLayout);
+               adminLayersC.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, false, true));
+
+//             boolean isAdmin = cmsView.doAs(() -> CurrentUser.isInRole(NodeConstants.ROLE_USER_ADMIN));
+               Set<String> userRoles = cmsView.doAs(() -> CurrentUser.roles());
                Button first = null;
-               for (String layerId : defaultLayers) {
+               layers: for (String layerDef : defaultLayers) {
+                       layerDef = layerDef.trim();
+                       if ("".equals(layerDef))
+                               continue layers;// skip empty lines
+                       String[] semiColArr = layerDef.split(";");
+                       String layerId = semiColArr[0];
+                       Set<String> layerRoles = SuiteUtils.extractRoles(semiColArr);
                        if (layers.containsKey(layerId)) {
+                               if (!layerRoles.isEmpty()) {
+                                       Set<String> intersection = new HashSet<String>(layerRoles);
+                                       intersection.retainAll(userRoles);
+                                       if (intersection.isEmpty())
+                                               continue layers;// skip unauthorized layer
+                               }
                                RankedObject<SuiteLayer> layerObj = layers.get(layerId);
 
                                // TODO deal with i10n
                                String titleStr = (String) layerObj.getProperties().get(SuiteLayer.Property.title.name());
                                Localized title = null;
-                               if (titleStr != null)
-                                       title = new Localized.Untranslated(titleStr);
+                               if (titleStr != null) {
+                                       if (titleStr.startsWith("%")) {
+                                               // LocaleUtils.local(titleStr, getClass().getClassLoader());
+                                               title = () -> titleStr;
+                                       } else {
+                                               title = new Localized.Untranslated(titleStr);
+                                       }
+                               }
 
                                String iconName = (String) layerObj.getProperties().get(SuiteLayer.Property.icon.name());
                                SuiteIcon icon = null;
                                if (iconName != null)
                                        icon = SuiteIcon.valueOf(iconName);
 
-                               Button b = SuiteUiUtils.createLayerButton(parent, layerId, title, icon);
+                               Composite buttonParent;
+                               if (adminLayers.contains(layerId))
+                                       buttonParent = adminLayersC;
+                               else
+                                       buttonParent = appLayersC;
+                               Button b = createLayerButton(buttonParent, layerId, title, icon);
                                if (first == null)
                                        first = b;
                        }
                }
 
-               // TODO factorise
-               boolean isAdmin = cmsView.doAs(() -> CurrentUser.isInRole(NodeConstants.ROLE_USER_ADMIN));
-               if (isAdmin && adminLayers != null)
-                       for (String layerId : adminLayers) {
-                               if (layers.containsKey(layerId)) {
-                                       RankedObject<SuiteLayer> layerObj = layers.get(layerId);
-
-                                       // TODO deal with i10n
-                                       String titleStr = (String) layerObj.getProperties().get(SuiteLayer.Property.title.name());
-                                       Localized title = null;
-                                       if (titleStr != null)
-                                               title = new Localized.Untranslated(titleStr);
-
-                                       String iconName = (String) layerObj.getProperties().get(SuiteLayer.Property.icon.name());
-                                       SuiteIcon icon = null;
-                                       if (iconName != null)
-                                               icon = SuiteIcon.valueOf(iconName);
-
-                                       Button b = SuiteUiUtils.createLayerButton(parent, layerId, title, icon);
-                                       if (first == null)
-                                               first = b;
-                               }
-                       }
+//             if (isAdmin && adminLayers != null)
+//                     for (String layerId : adminLayers) {
+//                             if (layers.containsKey(layerId)) {
+//                                     RankedObject<SuiteLayer> layerObj = layers.get(layerId);
+//
+//                                     String titleStr = (String) layerObj.getProperties().get(SuiteLayer.Property.title.name());
+//                                     Localized title = null;
+//                                     if (titleStr != null)
+//                                             title = new Localized.Untranslated(titleStr);
+//
+//                                     String iconName = (String) layerObj.getProperties().get(SuiteLayer.Property.icon.name());
+//                                     SuiteIcon icon = null;
+//                                     if (iconName != null)
+//                                             icon = SuiteIcon.valueOf(iconName);
+//
+//                                     Button b = SuiteUiUtils.createLayerButton(parent, layerId, title, icon);
+//                                     if (first == null)
+//                                             first = b;
+//                             }
+//                     }
 
 //             Button dashboardB = createButton(parent, SuiteMsg.dashboard.name(), SuiteMsg.dashboard, SuiteIcon.dashboard);
                if (!cmsView.isAnonymous()) {
@@ -99,15 +145,23 @@ public class DefaultLeadPane implements CmsUiProvider {
                return first;
        }
 
+       protected void processLayer(String layerDef) {
+
+       }
+
        public void init(Map<String, Object> properties) {
-               defaultLayers = (String[]) properties.get(Property.defaultLayers.toString());
+               String[] defaultLayers = (String[]) properties.get(Property.defaultLayers.toString());
                if (defaultLayers == null)
                        throw new IllegalArgumentException("Default layers must be set.");
+               this.defaultLayers = Arrays.asList(defaultLayers);
                if (log.isDebugEnabled())
                        log.debug("Default layers: " + Arrays.asList(defaultLayers));
-               adminLayers = (String[]) properties.get(Property.adminLayers.toString());
-               if (log.isDebugEnabled() && adminLayers != null)
-                       log.debug("Admin layers: " + Arrays.asList(adminLayers));
+               String[] adminLayers = (String[]) properties.get(Property.adminLayers.toString());
+               if (adminLayers != null) {
+                       this.adminLayers = Arrays.asList(adminLayers);
+                       if (log.isDebugEnabled())
+                               log.debug("Admin layers: " + Arrays.asList(adminLayers));
+               }
        }
 
        public void addLayer(SuiteLayer layer, Map<String, Object> properties) {
@@ -127,4 +181,26 @@ public class DefaultLeadPane implements CmsUiProvider {
                        }
                }
        }
+
+       protected Button createLayerButton(Composite parent, String layer, Localized msg, CmsIcon icon) {
+               CmsTheme theme = CmsTheme.getCmsTheme(parent);
+               Button button = new Button(parent, SWT.PUSH);
+               CmsUiUtils.style(button, SuiteStyle.leadPane);
+               if (icon != null)
+                       button.setImage(icon.getBigIcon(theme));
+               button.setLayoutData(new GridData(SWT.CENTER, SWT.BOTTOM, true, false));
+               // button.setToolTipText(msg.lead());
+               if (msg != null) {
+                       Label lbl = new Label(parent, SWT.CENTER);
+                       CmsUiUtils.style(lbl, SuiteStyle.leadPane);
+                       //CmsUiUtils.markup(lbl);
+                       ClassLoader l10nClassLoader = getClass().getClassLoader();
+                       String txt = LocaleUtils.lead(msg, l10nClassLoader);
+//                     String txt = msg.lead();
+                       lbl.setText(txt);
+                       lbl.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, true, false));
+               }
+               CmsUiUtils.sendEventOnSelect(button, SuiteEvent.switchLayer.topic(), SuiteEvent.LAYER, layer);
+               return button;
+       }
 }
index 8e9a9d5d641968693a6364f6cba86696ef0d256b..3c47b96a6053a187abb123cbdc95af5bfc1c4305 100644 (file)
@@ -8,16 +8,17 @@ import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
 import org.argeo.cms.Localized;
+import org.argeo.cms.auth.CurrentUser;
 import org.argeo.cms.ui.CmsEditable;
-import org.argeo.cms.ui.CmsTheme;
+import org.argeo.cms.ui.CmsView;
 import org.argeo.cms.ui.dialogs.LightweightDialog;
-import org.argeo.cms.ui.util.CmsIcon;
 import org.argeo.cms.ui.util.CmsUiUtils;
 import org.argeo.eclipse.ui.EclipseUiUtils;
 import org.argeo.entity.EntityNames;
 import org.argeo.entity.EntityType;
 import org.argeo.jcr.Jcr;
 import org.argeo.jcr.JcrUtils;
+import org.argeo.suite.SuiteRole;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.custom.ScrolledComposite;
 import org.eclipse.swt.events.MouseEvent;
@@ -312,22 +313,9 @@ public class SuiteUiUtils {
                return img;
        }
 
-       public static Button createLayerButton(Composite parent, String layer, Localized msg, CmsIcon icon) {
-               CmsTheme theme = CmsTheme.getCmsTheme(parent);
-               Button button = new Button(parent, SWT.PUSH);
-               CmsUiUtils.style(button, SuiteStyle.leadPane);
-               if (icon != null)
-                       button.setImage(icon.getBigIcon(theme));
-               button.setLayoutData(new GridData(SWT.CENTER, SWT.BOTTOM, true, false));
-               // button.setToolTipText(msg.lead());
-               if (msg != null) {
-                       Label lbl = new Label(parent, SWT.NONE);
-                       CmsUiUtils.style(lbl, SuiteStyle.leadPane);
-                       lbl.setText(msg.lead());
-                       lbl.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, true, false));
-               }
-               CmsUiUtils.sendEventOnSelect(button, SuiteEvent.switchLayer.topic(), SuiteEvent.LAYER, layer);
-               return button;
+       public static boolean isCoworker(CmsView cmsView) {
+               boolean coworker = cmsView.doAs(() -> CurrentUser.isInRole(SuiteRole.coworker.dn()));
+               return coworker;
        }
 
 //     public static String createAndConfigureEntity(Shell shell, Session referenceSession, String mainMixin,