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   * @version $Revision: 39130 $ ($Author: had $)
54   */
55  public class MgnlRole implements Role {
56      private static final Logger log = LoggerFactory.getLogger(MgnlRole.class);
57  
58      public static long PERMISSION_ANY = -1;
59  
60      private final Content roleNode;
61  
62      protected MgnlRole(Content roleNode) {
63          this.roleNode = roleNode;
64      }
65  
66      public String getName() {
67          return roleNode.getName();
68      }
69  
70      public void addPermission(String repository, String path, long permission) {
71          try {
72              Content aclNode = getAclNode(repository);
73              if (!this.existsPermission(aclNode, path, permission)) {
74                  HierarchyManager hm = MgnlSecurityUtil.getSystemHierarchyManager(ContentRepository.USER_ROLES);
75                  String nodename = Path.getUniqueLabel(hm, aclNode.getHandle(), "0");
76                  Content node = aclNode.createContent(nodename, ItemType.CONTENTNODE);
77                  node.setNodeData("path", path);
78                  node.setNodeData("permissions", permission);
79                  roleNode.save();
80              }
81          }
82          catch (Exception e) {
83              log.error("can't add permission", e);
84          }
85      }
86  
87      public void removePermission(String repository, String path) {
88          this.removePermission(repository, path, MgnlRole.PERMISSION_ANY);
89      }
90  
91      public void removePermission(String repository, String path, long permission) {
92          try {
93              Content aclNode = getAclNode(repository);
94              Collection<Content> children = aclNode.getChildren();
95              for (Content child : children) {
96                  if (child.getNodeData("path").getString().equals(path)) {
97                      if (permission == MgnlRole.PERMISSION_ANY
98                              || child.getNodeData("permissions").getLong() == permission) {
99                          child.delete();
100                     }
101                 }
102             }
103             roleNode.save();
104         }
105         catch (Exception e) {
106             log.error("can't remove permission", e);
107         }
108     }
109 
110     /**
111      * Get the ACL node for the current role node.
112      */
113     private Content getAclNode(String repository) throws RepositoryException, PathNotFoundException,
114     AccessDeniedException {
115         Content aclNode;
116         if (!roleNode.hasContent("acl_" + repository)) {
117             aclNode = roleNode.createContent("acl_" + repository, ItemType.CONTENTNODE);
118         }
119         else {
120             aclNode = roleNode.getContent("acl_" + repository);
121         }
122         return aclNode;
123     }
124 
125     /**
126      * Does this permission exist?
127      */
128     private boolean existsPermission(Content aclNode, String path, long permission) {
129         Collection<Content> children = aclNode.getChildren();
130         for (Content child : children) {
131             if (child.getNodeData("path").getString().equals(path)) {
132                 if (permission == MgnlRole.PERMISSION_ANY
133                         || child.getNodeData("permissions").getLong() == permission) {
134                     return true;
135                 }
136             }
137         }
138         return false;
139     }
140 
141     public Content getRoleNode() {
142         return roleNode;
143     }
144 }