You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tiles.apache.org by ap...@apache.org on 2007/12/20 16:33:48 UTC

svn commit: r605967 - in /tiles/framework/trunk: tiles-api/src/main/java/org/apache/tiles/ tiles-core/src/main/java/org/apache/tiles/definition/ tiles-core/src/main/java/org/apache/tiles/impl/ tiles-core/src/main/java/org/apache/tiles/impl/mgmt/

Author: apetrelli
Date: Thu Dec 20 07:33:45 2007
New Revision: 605967

URL: http://svn.apache.org/viewvc?rev=605967&view=rev
Log:
TILES-197
Added the "roles" property in Attribute and Definition, and modified the rest of the code accordingly.

Modified:
    tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/Attribute.java
    tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/Definition.java
    tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/DefinitionsImpl.java
    tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java
    tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/mgmt/DefinitionManager.java

Modified: tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/Attribute.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/Attribute.java?rev=605967&r1=605966&r2=605967&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/Attribute.java (original)
+++ tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/Attribute.java Thu Dec 20 07:33:45 2007
@@ -23,7 +23,10 @@
 
 import java.io.Serializable;
 import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
 import java.util.Map;
+import java.util.Set;
 
 /**
  * Common implementation of attribute definition.
@@ -108,9 +111,9 @@
     };
 
     /**
-     * Role associated to this attribute.
+     * The roles that can render this attribute.
      */
-    protected String role = null;
+    protected Set<String> roles = null;
 
     /**
      * The value of the attribute.
@@ -152,7 +155,7 @@
      */
     public Attribute(Attribute attribute) {
         this.name = attribute.name;
-        this.role = attribute.role;
+        this.roles = attribute.roles;
         this.type = attribute.type;
         this.value = attribute.getValue();
     }
@@ -176,7 +179,7 @@
      */
     public Attribute(Object value, String role) {
         this.value = value;
-        this.role = role;
+        setRole(role);
     }
 
     /**
@@ -188,8 +191,8 @@
      */
     public Attribute(Object value, String role, AttributeType type) {
         this.value = value;
-        this.role = role;
         this.type = type;
+        setRole(role);
     }
 
     /**
@@ -204,8 +207,8 @@
             AttributeType type) {
         this.name = name;
         this.value = value;
-        this.role = role;
         this.type = type;
+        setRole(role);
     }
 
     /**
@@ -213,7 +216,31 @@
      * @return the name of the required role(s)
      */
     public String getRole() {
-        return role;
+        String retValue = null;
+
+        if (roles != null && !roles.isEmpty()) {
+            StringBuilder builder = new StringBuilder();
+            Iterator<String> roleIt = roles.iterator();
+            if (roleIt.hasNext()) {
+                builder.append(roleIt.next());
+                while (roleIt.hasNext()) {
+                    builder.append(",");
+                    builder.append(roleIt.next());
+                }
+                retValue = builder.toString();
+            }
+        }
+
+        return retValue;
+    }
+
+    /**
+     * Returns the roles that can render this attribute.
+     *
+     * @return The enabled roles.
+     */
+    public Set<String> getRoles() {
+        return roles;
     }
 
     /**
@@ -222,7 +249,24 @@
      * @param role Associated role.
      */
     public void setRole(String role) {
-        this.role = role;
+        if (role != null && role.trim().length() > 0) {
+            String[] rolesStrings = role.split("\\s*,\\s*");
+            roles = new HashSet<String>();
+            for (int i = 0; i < rolesStrings.length; i++) {
+                roles.add(rolesStrings[i]);
+            }
+        } else {
+            roles = null;
+        }
+    }
+
+    /**
+     * Sets the roles that can render this attribute.
+     *
+     * @param roles The enabled roles.
+     */
+    public void setRoles(Set<String> roles) {
+        this.roles = roles;
     }
 
     /**

Modified: tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/Definition.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/Definition.java?rev=605967&r1=605966&r2=605967&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/Definition.java (original)
+++ tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/Definition.java Thu Dec 20 07:33:45 2007
@@ -21,8 +21,11 @@
 package org.apache.tiles;
 
 
+import java.util.HashSet;
+import java.util.Iterator;
 import java.util.Map;
 import java.util.HashMap;
+import java.util.Set;
 
 import org.apache.tiles.Attribute.AttributeType;
 
@@ -53,9 +56,9 @@
      */
     protected Map<String, Attribute> attributes = null;
     /**
-     * Role associated to definition.
+     * The roles that can render this definition.
      */
-    protected String role = null;
+    protected Set<String> roles = null;
     /**
      * Associated ViewPreparer URL or classname, if defined.
      */
@@ -80,11 +83,11 @@
     public Definition(Definition definition) {
         attributes = new HashMap<String, Attribute>(
             definition.getAttributes());
-        this.name = definition.getName();
-        this.template = definition.getTemplate();
-        this.role = definition.getRole();
-        this.preparer = definition.getPreparer();
-        this.inherit = definition.getExtends();
+        this.name = definition.name;
+        this.template = definition.template;
+        this.roles = definition.roles;
+        this.preparer = definition.preparer;
+        this.inherit = definition.inherit;
     }
 
     /**
@@ -142,16 +145,57 @@
      * @return the current value of the role property
      */
     public String getRole() {
-        return role;
+        String retValue = null;
+
+        if (roles != null && !roles.isEmpty()) {
+            StringBuilder builder = new StringBuilder();
+            Iterator<String> roleIt = roles.iterator();
+            if (roleIt.hasNext()) {
+                builder.append(roleIt.next());
+                while (roleIt.hasNext()) {
+                    builder.append(",");
+                    builder.append(roleIt.next());
+                }
+                retValue = builder.toString();
+            }
+        }
+
+        return retValue;
+    }
+
+    /**
+     * Returns the roles that can render this attribute.
+     *
+     * @return The enabled roles.
+     */
+    public Set<String> getRoles() {
+        return roles;
     }
 
     /**
      * Sets the value of the role property.
      *
-     * @param role the new value of the path property
+     * @param role the new value of the role property
      */
     public void setRole(String role) {
-        this.role = role;
+        if (role != null && role.trim().length() > 0) {
+            String[] rolesStrings = role.split("\\s*,\\s*");
+            roles = new HashSet<String>();
+            for (int i = 0; i < rolesStrings.length; i++) {
+                roles.add(rolesStrings[i]);
+            }
+        } else {
+            roles = null;
+        }
+    }
+
+    /**
+     * Sets the roles that can render this attribute.
+     *
+     * @param roles The enabled roles.
+     */
+    public void setRoles(Set<String> roles) {
+        this.roles = roles;
     }
 
     /**
@@ -308,7 +352,7 @@
             + ", template="
             + template
             + ", role="
-            + role
+            + getRoles()
             + ", preparerInstance="
             + preparer
             + ", attributes="

Modified: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/DefinitionsImpl.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/DefinitionsImpl.java?rev=605967&r1=605966&r2=605967&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/DefinitionsImpl.java (original)
+++ tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/DefinitionsImpl.java Thu Dec 20 07:33:45 2007
@@ -269,8 +269,8 @@
         if (child.getTemplate() == null) {
             child.setTemplate(parent.getTemplate());
         }
-        if (child.getRole() == null) {
-            child.setRole(parent.getRole());
+        if (child.getRoles() == null) {
+            child.setRoles(parent.getRoles());
         }
         if (child.getPreparer() == null) {
             child.setPreparer(parent.getPreparer());

Modified: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java?rev=605967&r1=605966&r2=605967&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java (original)
+++ tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java Thu Dec 20 07:33:45 2007
@@ -43,8 +43,10 @@
 import java.io.Writer;
 import java.net.URL;
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.StringTokenizer;
 
 /**
@@ -394,9 +396,12 @@
             throw new NoSuchDefinitionException(definitionName);
         }
 
-        if (!isPermitted(request, definition.getRole())) {
-            LOG.info("Access to definition '" + definitionName
-                    + "' denied.  User not in role '" + definition.getRole());
+        if (!isPermitted(request, definition.getRoles())) {
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Access to definition '" + definitionName
+                        + "' denied.  User not in role '"
+                        + definition.getRoles());
+            }
             return;
         }
 
@@ -439,9 +444,11 @@
             throw new TilesException("Cannot render a null attribute");
         }
 
-        if (!isPermitted(request, attr.getRole())) {
-            LOG.info("Access to attribute '" + attr.getName()
-                    + "' denied.  User not in role '" + attr.getRole());
+        if (!isPermitted(request, attr.getRoles())) {
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Access to attribute '" + attr.getName()
+                        + "' denied.  User not in role '" + attr.getRoles());
+            }
             return;
         }
 
@@ -520,21 +527,22 @@
      * specified in the <code>role</code> parameter.
      *
      * @param request The request context.
-     * @param role The comma-separated list of roles.
+     * @param roles The list of roles.
      * @return <code>true</code> if the current user is in one of those roles.
      */
-    private boolean isPermitted(TilesRequestContext request, String role) {
-        if (role == null) {
+    private boolean isPermitted(TilesRequestContext request, Set<String> roles) {
+        if (roles == null || roles.isEmpty()) {
             return true;
         }
 
-        StringTokenizer st = new StringTokenizer(role, ",");
-        while (st.hasMoreTokens()) {
-            if (request.isUserInRole(st.nextToken())) {
-                return true;
-            }
+        boolean retValue = false;
+
+        for (Iterator<String> roleIt = roles.iterator(); roleIt.hasNext()
+                && !retValue;) {
+            retValue = request.isUserInRole(roleIt.next());
         }
-        return false;
+
+        return retValue;
     }
 
     /**

Modified: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/mgmt/DefinitionManager.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/mgmt/DefinitionManager.java?rev=605967&r1=605966&r2=605967&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/mgmt/DefinitionManager.java (original)
+++ tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/mgmt/DefinitionManager.java Thu Dec 20 07:33:45 2007
@@ -228,8 +228,8 @@
             child.setTemplate(parent.getTemplate());
         }
 
-        if (child.getRole() == null) {
-            child.setRole(parent.getRole());
+        if (child.getRoles() == null) {
+            child.setRoles(parent.getRoles());
         }
 
         if (child.getPreparer() == null) {