View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.security;
35  
36  import info.magnolia.cms.beans.config.ContentRepository;
37  import info.magnolia.cms.core.Content;
38  import info.magnolia.cms.core.ItemType;
39  import info.magnolia.cms.core.Path;
40  import info.magnolia.cms.core.HierarchyManager;
41  
42  import java.util.Collection;
43  
44  import javax.jcr.PathNotFoundException;
45  import javax.jcr.RepositoryException;
46  
47  import org.slf4j.Logger;
48  import org.slf4j.LoggerFactory;
49  
50  
51  /**
52   * Wraps a role jcr-node.
53   * @author philipp
54   * @version $Revision: 33093 $ ($Author: gjoseph $)
55   */
56  public class MgnlRole implements Role {
57      private static final Logger log = LoggerFactory.getLogger(MgnlRole.class);
58  
59      /**
60       * Add or remove any permission
61       */
62      public static long PERMISSION_ANY = -1;
63  
64      /**
65       * the content object
66       */
67      private final Content roleNode;
68  
69      /**
70       * @param roleNode the Content object representing this role
71       */
72      protected MgnlRole(Content roleNode) {
73          this.roleNode = roleNode;
74      }
75  
76      public String getName() {
77          return roleNode.getName();
78      }
79  
80      public void addPermission(String repository, String path, long permission) {
81          try {
82              Content aclNode = getAclNode(repository);
83              if (!this.existsPermission(aclNode, path, permission)) {
84                  HierarchyManager hm = MgnlSecurityUtil.getSystemHierarchyManager(ContentRepository.USER_ROLES);
85                  String nodename = Path.getUniqueLabel(hm, aclNode.getHandle(), "0");
86                  Content node = aclNode.createContent(nodename, ItemType.CONTENTNODE);
87                  node.createNodeData("path").setValue(path);
88                  node.createNodeData("permissions").setValue(String.valueOf(permission));
89                  roleNode.save();
90              }
91          }
92          catch (Exception e) {
93              log.error("can't add permission", e);
94          }
95      }
96  
97      public void removePermission(String repository, String path) {
98          this.removePermission(repository, path, MgnlRole.PERMISSION_ANY);
99      }
100 
101     public void removePermission(String repository, String path, long permission) {
102         try {
103             Content aclNode = getAclNode(repository);
104             Collection<Content> children = aclNode.getChildren();
105             for (Content child : children) {
106                 if (child.getNodeData("path").getString().equals(path)) {    
107                     if (permission == MgnlRole.PERMISSION_ANY
108                         || child.getNodeData("permissions").getLong() == permission) {
109                         child.delete();
110                     }
111                 }
112             }
113             roleNode.save();
114         }
115         catch (Exception e) {
116             log.error("can't remove permission", e);
117         }
118     }
119 
120     /**
121      * Get the acl node for the current role node
122      * @param repository
123      * @return
124      * @throws RepositoryException
125      * @throws PathNotFoundException
126      * @throws AccessDeniedException
127      */
128     private Content getAclNode(String repository) throws RepositoryException, PathNotFoundException,
129         AccessDeniedException {
130         Content aclNode;
131         if (!roleNode.hasContent("acl_" + repository)) {
132             aclNode = roleNode.createContent("acl_" + repository, ItemType.CONTENTNODE);
133         }
134         else {
135             aclNode = roleNode.getContent("acl_" + repository);
136         }
137         return aclNode;
138     }
139 
140     /**
141      * Does this permission exist?
142      * @param aclNode
143      * @param path
144      * @param permission
145      */
146     private boolean existsPermission(Content aclNode, String path, long permission) {
147         Collection<Content> children = aclNode.getChildren();
148         for (Content child : children) {
149             if (child.getNodeData("path").getString().equals(path)) {
150                 if (permission == MgnlRole.PERMISSION_ANY
151                     || child.getNodeData("permissions").getLong() == permission) {
152                     return true;
153                 }
154             }
155         }
156         return false;
157     }
158 
159     public Content getRoleNode() {
160         return roleNode;
161     }
162 }