[PATCH 2 of 2] Allow to filter datacage entries by module-roles of current user

Wald Commits scm-commit at wald.intevation.org
Mon Oct 1 16:21:17 CEST 2018


# HG changeset patch
# User gernotbelger
# Date 1538403680 -7200
# Node ID b188255f08b317f0aee7e89cd893820eb8c16fea
# Parent  ec2ebbc49fbcd27fd175341979d55e3bb6a7e129
Allow to filter datacage entries by module-roles of current user.
Hide Sinfo, Uinfo and Minfo branches if user does not have those modules.

diff -r ec2ebbc49fbc -r b188255f08b3 artifacts/doc/conf/meta-data.xml
--- a/artifacts/doc/conf/meta-data.xml	Mon Oct 01 16:21:16 2018 +0200
+++ b/artifacts/doc/conf/meta-data.xml	Mon Oct 01 16:21:20 2018 +0200
@@ -290,14 +290,14 @@
                     <dc:call-macro name="bed-heights-single-KL_TW"/>
                   </dc:when>
                   <dc:when test="$out = 'sinfo_flowdepth_waterlevels'">
-                    <minfo>
+                    <minfo module="minfo">
                       <fixanalysis>
                         <dc:call-macro name="basedata_7_waterlevels"/>
                       </fixanalysis>
                     </minfo>
                     <dc:call-macro name="basedata_3_officials"/>
                     <dc:call-macro name="basedata_2_fixations"/>
-                    <sinfo>
+                    <sinfo module="sinfo">
                       <sinfo_additional_ls>
                         <dc:call-macro name="basedata_1_additionals-sinfo-with-q"/>
                         <dc:call-macro name="basedata_1_additionals-sinfo-without-q"/>
@@ -1355,7 +1355,7 @@
 
     <!-- Default Vegetation Zones -->
     <dc:macro name="predefined_vegetation_zones">
-      <uinfo>
+      <uinfo module="uinfo">
         <vegetation_zones>
           <dc:variable name="vegzonedata" expr="dc:defaultvegetationzone($river)"/>
 
@@ -1688,7 +1688,7 @@
       <dc:call-macro name="basedata_3_officials_wq"/>
       <dc:call-macro name="basedata_4_heightmarks-points_wq"/>
       <dc:call-macro name="basedata_5_flood-protections_wq"/>
-      <minfo>
+      <minfo module="minfo">
         <dc:call-macro name="sqrelations"/>
         <dc:call-macro name="basedata_7_waterlevels_wq"/>
       </minfo>
@@ -1701,7 +1701,7 @@
       <dc:call-macro name="fix-wq-curve"/>
       <!-- TODO: own macro for Vollmer-curve (extract from fix-wq-curve and waterlevels-user) -->
       <dc:call-macro name="extreme-wq-curve"/>
-      <minfo>
+      <minfo module="minfo">
         <dc:call-macro name="sqrelations_user"/>
       </minfo>
     </dc:macro>
@@ -1714,7 +1714,7 @@
       <dc:call-macro name="basedata_4_heightmarks-points"/>
       <dc:call-macro name="basedata_5_flood-protections"/>
       <dc:call-macro name="annotations_per_type"/>
-      <minfo>
+      <minfo module="minfo">
         <fixanalysis>
           <dc:call-macro name="basedata_6_delta_w"/>
           <dc:call-macro name="basedata_7_waterlevels"/>
@@ -1731,7 +1731,7 @@
         </flow_velocities>
       </minfo>
 
-      <sinfo>
+      <sinfo module="sinfo">
         <sinfo_predefined_flowdepths>
           <dc:call-macro name="sinfo_predefined_flowdepth-m"/>
         </sinfo_predefined_flowdepths>
diff -r ec2ebbc49fbc -r b188255f08b3 gwt-client/src/main/java/org/dive4elements/river/client/server/MetaDataServiceImpl.java
--- a/gwt-client/src/main/java/org/dive4elements/river/client/server/MetaDataServiceImpl.java	Mon Oct 01 16:21:16 2018 +0200
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/server/MetaDataServiceImpl.java	Mon Oct 01 16:21:20 2018 +0200
@@ -8,105 +8,148 @@
 
 package org.dive4elements.river.client.server;
 
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
+import java.util.Iterator;
+import java.util.List;
 
 import org.apache.log4j.Logger;
-
-import com.google.gwt.user.server.rpc.RemoteServiceServlet;
-
 import org.dive4elements.artifacts.common.ArtifactNamespaceContext;
-
 import org.dive4elements.artifacts.common.utils.XMLUtils;
-
-import org.dive4elements.river.client.shared.exceptions.ServerException;
-
-import org.dive4elements.river.client.client.services.MetaDataService;
-
 import org.dive4elements.artifacts.httpclient.exceptions.ConnectionException;
-
 import org.dive4elements.artifacts.httpclient.http.HttpClient;
 import org.dive4elements.artifacts.httpclient.http.HttpClientImpl;
-
+import org.dive4elements.river.client.client.services.MetaDataService;
+import org.dive4elements.river.client.server.auth.User;
+import org.dive4elements.river.client.server.meta.Converter;
+import org.dive4elements.river.client.shared.exceptions.ServerException;
 import org.dive4elements.river.client.shared.model.AttrList;
 import org.dive4elements.river.client.shared.model.DataCageNode;
 import org.dive4elements.river.client.shared.model.DataCageTree;
-
-import org.dive4elements.river.client.server.meta.Converter;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
 
 /**
  * Service that returns certain meta-data from the backends data, polished to
  * inclusion into current project.
  */
-public class MetaDataServiceImpl
-extends      RemoteServiceServlet
-implements   MetaDataService, DataCageTree.Visitor
-{
+public class MetaDataServiceImpl extends RemoteServiceServlet implements MetaDataService {
+
+    private static final long serialVersionUID = 1L;
+
     /** Our very own log. */
-    private static final Logger log =
-        Logger.getLogger(MetaDataServiceImpl.class);
+    private static final Logger log = Logger.getLogger(MetaDataServiceImpl.class);
 
-    public static final String ERROR_NO_META_DATA_FOUND =
-        "error_no_meta_data_found";
+    private static final String ERROR_NO_META_DATA_FOUND = "error_no_meta_data_found";
 
+    private static final class Prune {
+        private final User user;
 
-    @Override
-    public boolean accept(DataCageNode node) {
-        AttrList al = node.getAttributes();
-        return al != null && al.hasAttribute("factory");
+        public Prune(final User user) {
+            this.user = user;
+        }
+
+        void execute(final DataCageTree tree) {
+
+            final DataCageNode root = tree.getRoot();
+
+            if (root == null || !root.hasChildren())
+                return;
+
+            recursivePrune(root);
+        }
+
+        private boolean recursivePrune(final DataCageNode node) {
+
+            /* filter by user - features */
+            if (!acceptUserFeatures(node))
+                return false;
+
+            /* recursively check all leafs, we only accepts real ones that have a factory */
+            if (!node.hasChildren())
+                return acceptLeaf(node);
+
+            final List<DataCageNode> children = node.getChildren();
+
+            for (final Iterator<DataCageNode> iterator = children.iterator(); iterator.hasNext();) {
+                final DataCageNode dataCageNode = iterator.next();
+
+                if (!recursivePrune(dataCageNode))
+                    iterator.remove();
+            }
+
+            return !children.isEmpty();
+        }
+
+        private boolean acceptUserFeatures(final DataCageNode node) {
+        final AttrList al = node.getAttributes();
+            if (al == null)
+                return true;
+
+            final String module = al.getValue("module");
+            if (module == null)
+                return true;
+
+            if (this.user == null || this.user.canUseFeature("module:" + module))
+                return true;
+
+            return false;
+    }
+
+        private boolean acceptLeaf(final DataCageNode node) {
+            final AttrList al = node.getAttributes();
+            if (al == null)
+                return false;
+
+            return al.hasAttribute("factory");
+        }
     }
 
     /**
-     * @param locale needed for i18n.
-     * @param artifactId ID of masterartifact (can be null)
-     * @param userId can be null
-     * @param outs can be null
-     * @param parameters can be null or parameters like
-     * "load-system:true;key:value"
+     * @param locale
+     *            needed for i18n.
+     * @param artifactId
+     *            ID of masterartifact (can be null)
+     * @param userId
+     *            can be null
+     * @param outs
+     *            can be null
+     * @param parameters
+     *            can be null or parameters like
+     *            "load-system:true;key:value"
      */
     @Override
-    public DataCageTree getMetaData(
-        String locale,
-        String artifactId,
-        String userId,
-        String outs,
-        String parameters
-    ) throws ServerException
-    {
+    public DataCageTree getMetaData(final String locale, final String artifactId, final String userId, final String outs, final String parameters)
+            throws ServerException {
         log.info("MetaDataService.getMetaData");
 
         // Create the query document.
-        String url = getServletContext().getInitParameter("server-url");
+        final String url = getServletContext().getInitParameter("server-url");
 
-        Document doc = XMLUtils.newDocument();
+        final Document doc = XMLUtils.newDocument();
 
-        XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
-            doc,
-            ArtifactNamespaceContext.NAMESPACE_URI,
-            ArtifactNamespaceContext.NAMESPACE_PREFIX);
+        final XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(doc, ArtifactNamespaceContext.NAMESPACE_URI, ArtifactNamespaceContext.NAMESPACE_PREFIX);
 
-        Element meta = ec.create("meta");
+        final Element meta = ec.create("meta");
 
         if (artifactId != null) {
-            Element artifactEl = ec.create("artifact-id");
+            final Element artifactEl = ec.create("artifact-id");
             artifactEl.setAttribute("value", artifactId);
             meta.appendChild(artifactEl);
         }
 
         if (userId != null) {
-            Element userEl = ec.create("user-id");
+            final Element userEl = ec.create("user-id");
             userEl.setAttribute("value", userId);
             meta.appendChild(userEl);
         }
 
         if (outs != null) {
-            Element outsEl = ec.create("outs");
+            final Element outsEl = ec.create("outs");
             outsEl.setAttribute("value", outs);
             meta.appendChild(outsEl);
         }
 
         if (parameters != null) {
-            Element paramsEl = ec.create("parameters");
+            final Element paramsEl = ec.create("parameters");
             paramsEl.setAttribute("value", parameters);
             meta.appendChild(paramsEl);
         }
@@ -114,20 +157,22 @@
         doc.appendChild(meta);
 
         // Fire.
-        HttpClient client = new HttpClientImpl(url, locale);
+        final HttpClient client = new HttpClientImpl(url, locale);
 
         try {
-            Converter converter = new Converter();
-            DataCageTree tree = converter.convert(
-                client.callService(url, "metadata", doc));
-            tree.prune(this);
+            final User user = getUser();
+
+            final Converter converter = new Converter();
+            final DataCageTree tree = converter.convert(client.callService(url, "metadata", doc));
+
+            new Prune(user).execute(tree);
+
             return tree;
         }
-        catch (ConnectionException ce) {
+        catch (final ConnectionException ce) {
             ce.printStackTrace();
         }
 
         throw new ServerException(ERROR_NO_META_DATA_FOUND);
     }
-}
-// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
+}
\ No newline at end of file
diff -r ec2ebbc49fbc -r b188255f08b3 gwt-client/src/main/java/org/dive4elements/river/client/shared/model/DataCageTree.java
--- a/gwt-client/src/main/java/org/dive4elements/river/client/shared/model/DataCageTree.java	Mon Oct 01 16:21:16 2018 +0200
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/shared/model/DataCageTree.java	Mon Oct 01 16:21:20 2018 +0200
@@ -9,53 +9,29 @@
 package org.dive4elements.river.client.shared.model;
 
 import java.io.Serializable;
-import java.util.List;
 
-public class DataCageTree implements Serializable
-{
+public class DataCageTree implements Serializable {
+
+    private static final long serialVersionUID = 1L;
 
     public interface Visitor {
         boolean accept(DataCageNode node);
-    } // interface
+    }
 
-    protected DataCageNode root;
+    private DataCageNode root;
 
     public DataCageTree() {
     }
 
-    public DataCageTree(DataCageNode root) {
+    public DataCageTree(final DataCageNode root) {
         this.root = root;
     }
 
-    public void setRoot(DataCageNode root) {
+    public void setRoot(final DataCageNode root) {
         this.root = root;
     }
 
     public DataCageNode getRoot() {
-        return root;
+        return this.root;
     }
-
-
-    protected boolean recursivePrune(DataCageNode node, Visitor visitor) {
-        if (!node.hasChildren()) {
-            return visitor.accept(node);
-        }
-
-        List<DataCageNode> children = node.getChildren();
-
-        for (int i = children.size()-1; i >= 0; --i) {
-            if (!recursivePrune(children.get(i), visitor)) {
-                children.remove(i);
-            }
-        }
-
-        return !children.isEmpty();
-    }
-
-    public boolean prune(Visitor visitor) {
-        return root == null || !root.hasChildren()
-            ? true
-            : recursivePrune(root, visitor);
-    }
-}
-// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
+}
\ No newline at end of file


More information about the Dive4Elements-commits mailing list