View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 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 javax.jcr.Node;
37  import javax.jcr.NodeIterator;
38  import javax.jcr.PathNotFoundException;
39  import javax.jcr.RepositoryException;
40  import javax.jcr.Session;
41  
42  import info.magnolia.cms.core.Content;
43  import info.magnolia.cms.core.ItemType;
44  import info.magnolia.cms.core.Path;
45  import info.magnolia.context.MgnlContext;
46  import info.magnolia.cms.core.HierarchyManager;
47  import info.magnolia.repository.RepositoryConstants;
48  
49  import org.slf4j.Logger;
50  import org.slf4j.LoggerFactory;
51  
52  
53  /**
54   * Manages the users stored in the {@link info.magnolia.repository.RepositoryConstants#USER_ROLES} workspace.
55   * @author philipp
56   * @version $Revision: 50687 $ ($Author: dlipp $)
57   */
58  public class MgnlRoleManager extends RepositoryBackedSecurityManager implements RoleManager {
59      private static final Logger log = LoggerFactory.getLogger(MgnlRoleManager.class);
60  
61      /**
62       * Do not instantiate it!
63       */
64      public MgnlRoleManager() {
65      }
66  
67      @Override
68      public Role getRole(String name) {
69          try {
70              return newRoleInstance(findPrincipalNode(name, MgnlContext.getJCRSession(getRepositoryName())));
71          }
72          catch (Exception e) {
73              log.debug("can't find role [" + name + "]", e);
74              return null;
75          }
76      }
77  
78      @Override
79      public Role createRole(String name) {
80          try {
81              Content node = getHierarchyManager().createContent("/", name, ItemType.ROLE.getSystemName());
82              getHierarchyManager().save();
83              return newRoleInstance(node);
84          }
85          catch (Exception e) {
86              log.error("can't create role [" + name + "]", e);
87              return null;
88          }
89      }
90  
91      /**
92       * @deprecated since 4.5
93       */
94      @Deprecated
95      protected MgnlRole newRoleInstance(Content node) throws RepositoryException {
96          return newRoleInstance(node.getJCRNode());
97      }
98  
99      protected MgnlRole newRoleInstance(Node node) throws RepositoryException {
100         return new MgnlRole(node.getName(), node.getIdentifier(), getACLs(node).values());
101     }
102 
103     protected HierarchyManager getHierarchyManager() {
104         return MgnlContext.getHierarchyManager(RepositoryConstants.USER_ROLES);
105     }
106 
107     @Override
108     public void removePermission(Role role, String repository, String path, long permission) {
109         try {
110             Session session = MgnlContext.getJCRSession(RepositoryConstants.USER_ROLES);
111             Node roleNode = session.getNodeByIdentifier(role.getId());
112             Node aclNode = getAclNode(roleNode, repository);
113             NodeIterator children = aclNode.getNodes();
114             while(children.hasNext()) {
115                 Node child = children.nextNode();
116                 if (child.getProperty("path").getString().equals(path)) {
117                     if (permission == MgnlRole.PERMISSION_ANY
118                             || child.getProperty("permissions").getLong() == permission) {
119                         child.remove();
120                     }
121                 }
122             }
123             session.save();
124         }
125         catch (Exception e) {
126             log.error("can't remove permission", e);
127         }
128     }
129 
130     /**
131      * Get the ACL node for the current role node.
132      * @param roleNode
133      */
134     private Node getAclNode(Node roleNode, String repository) throws RepositoryException, PathNotFoundException,
135     AccessDeniedException {
136         Node aclNode;
137         if (!roleNode.hasNode("acl_" + repository)) {
138             aclNode = roleNode.addNode("acl_" + repository, ItemType.CONTENTNODE.getSystemName());
139         }
140         else {
141             aclNode = roleNode.getNode("acl_" + repository);
142         }
143         return aclNode;
144     }
145 
146     /**
147      * Does this permission exist?
148      */
149     private boolean existsPermission(Node aclNode, String path, long permission) throws RepositoryException {
150         NodeIterator children = aclNode.getNodes();
151         while(children.hasNext()) {
152             Node child = children.nextNode();
153             if (child.hasProperty("path") && child.getProperty("path").getString().equals(path)) {
154                 if (permission == MgnlRole.PERMISSION_ANY
155                         || child.getProperty("permissions").getLong() == permission) {
156                     return true;
157                 }
158             }
159         }
160         return false;
161     }
162 
163     @Override
164     public void addPermission(Role role, String repository, String path, long permission) {
165         try {
166             Session session = MgnlContext.getJCRSession(getRepositoryName());
167             Node roleNode = session.getNodeByIdentifier(role.getId());
168             Node aclNode = getAclNode(roleNode, repository);
169             if (!this.existsPermission(aclNode, path, permission)) {
170                 String nodename = Path.getUniqueLabel(session, aclNode.getPath(), "0");
171                 Node node = aclNode.addNode(nodename, ItemType.CONTENTNODE.getSystemName());
172                 node.setProperty("path", path);
173                 node.setProperty("permissions", permission);
174                 session.save();
175             }
176         }
177         catch (Exception e) {
178             log.error("can't add permission", e);
179         }
180     }
181 
182     @Override
183     protected Node findPrincipalNode(String principalName, Session session) throws RepositoryException {
184         return session.getNode("/" + principalName);
185     }
186 
187     @Override
188     protected String getRepositoryName() {
189         return RepositoryConstants.USER_ROLES;
190     }
191 
192     @Override
193     public String getRoleNameById(String string) {
194         return getResourceName(string);
195     }
196 
197 }