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.HierarchyManager;
39  import info.magnolia.cms.core.ItemType;
40  import info.magnolia.cms.core.NodeData;
41  import info.magnolia.cms.core.Path;
42  import info.magnolia.cms.util.NodeDataUtil;
43  import org.apache.commons.lang.StringUtils;
44  import org.slf4j.Logger;
45  import org.slf4j.LoggerFactory;
46  
47  import javax.jcr.ItemNotFoundException;
48  import javax.jcr.PathNotFoundException;
49  import javax.jcr.RepositoryException;
50  import java.util.Collection;
51  
52  
53  /**
54   * @author Sameer Charles $Id: MgnlGroup.java 33093 2010-03-19 21:16:40Z gjoseph $
55   */
56  public class MgnlGroup implements Group {
57      private static final Logger log = LoggerFactory.getLogger(MgnlGroup.class);
58  
59      /**
60       * Under this subnodes the assigned roles are saved
61       */
62      private static final String NODE_ROLES = "roles"; //$NON-NLS-1$
63  
64      private static final String NODE_GROUPS = "groups"; //$NON-NLS-1$
65  
66      /**
67       * group node
68       */
69      private final Content groupNode;
70  
71      /**
72       * @param groupNode the Content object representing this group
73       */
74      protected MgnlGroup(Content groupNode) {
75          this.groupNode = groupNode;
76      }
77  
78      /**
79       * get name of this node
80       * @return group name
81       */
82      public String getName() {
83          return getGroupNode().getName();
84      }
85  
86      /**
87       * add role to this group
88       * @param roleName
89       * @throws UnsupportedOperationException if the implementation does not support writing
90       * @throws AccessDeniedException if loggen in repository user does not sufficient rights
91       */
92      public void addRole(String roleName) throws UnsupportedOperationException, AccessDeniedException {
93          this.add(roleName, NODE_ROLES);
94      }
95  
96      /**
97       * add subgroup to this group
98       * @param groupName
99       * @throws UnsupportedOperationException if the implementation does not support writing
100      * @throws AccessDeniedException if loggen in repository user does not sufficient rights
101      */
102     public void addGroup(String groupName) throws UnsupportedOperationException, AccessDeniedException {
103         this.add(groupName, NODE_GROUPS);
104     }
105 
106     /**
107      * remove role from this group
108      * @param roleName
109      * @throws UnsupportedOperationException if the implementation does not support writing
110      * @throws AccessDeniedException if loggen in repository user does not sufficient rights
111      */
112     public void removeRole(String roleName) throws UnsupportedOperationException, AccessDeniedException {
113         this.remove(roleName, NODE_ROLES);
114     }
115 
116     /**
117      * remove subgroup from this group
118      * @param groupName
119      * @throws UnsupportedOperationException if the implementation does not support writing
120      * @throws AccessDeniedException if loggen in repository user does not sufficient rights
121      */
122     public void removeGroup(String groupName) throws UnsupportedOperationException, AccessDeniedException {
123         this.remove(groupName, NODE_GROUPS);
124     }
125 
126     /**
127      * returns true if role exist in this group
128      * @param roleName
129      * @throws UnsupportedOperationException if the implementation does not exist
130      * @throws AccessDeniedException if loggen in repository user does not sufficient rights
131      */
132     public boolean hasRole(String roleName) throws UnsupportedOperationException, AccessDeniedException {
133         return this.hasAny(roleName, NODE_ROLES);
134     }
135 
136     public String getProperty(String propertyName) {
137         return NodeDataUtil.getString(getGroupNode(), propertyName, null);
138     }
139 
140     public void setProperty(String propertyName, String value) {
141         try {
142             NodeDataUtil.getOrCreateAndSet(getGroupNode(), propertyName, value);
143             getGroupNode().save();
144         } catch (RepositoryException e) {
145             throw new RuntimeException(e); // TODO
146         }
147     }
148 
149     public Collection<String> getRoles() {
150         return MgnlSecurityUtil.collectPropertyNames(getGroupNode(), "roles", ContentRepository.USER_ROLES, false);
151     }
152 
153     public Collection<String> getGroups() {
154         return MgnlSecurityUtil.collectPropertyNames(getGroupNode(), "groups", ContentRepository.USER_GROUPS, false);
155     }
156 
157     public Collection<String> getAllGroups() {
158         return MgnlSecurityUtil.collectPropertyNames(getGroupNode(), "groups", ContentRepository.USER_GROUPS, true);
159     }
160 
161     /**
162      * checks is any object exist with the given name under this node
163      * @param name
164      * @param nodeName
165      */
166     private boolean hasAny(String name, String nodeName) {
167         try {
168             final String hmName;
169             if (StringUtils.equalsIgnoreCase(nodeName, NODE_ROLES)) {
170                 hmName = ContentRepository.USER_ROLES;
171             }
172             else {
173                 hmName = ContentRepository.USER_GROUPS;
174             }
175             final HierarchyManager hm = MgnlSecurityUtil.getSystemHierarchyManager(hmName);
176 
177             Content node = getGroupNode().getContent(nodeName);
178             for (NodeData nodeData : node.getNodeDataCollection()) {
179                 // check for the existence of this ID
180                 try {
181                     if (hm.getContentByUUID(nodeData.getString()).getName().equalsIgnoreCase(name)) {
182                         return true;
183                     }
184                 }
185                 catch (ItemNotFoundException e) {
186                     log.debug("[{}] does not exist in the {} repository", name, hmName);
187                 }
188                 catch (IllegalArgumentException e) {
189                     log.debug(nodeData.getHandle() + " has invalid value");
190                 }
191             }
192         }
193         catch (RepositoryException e) {
194             log.debug(e.getMessage(), e);
195         }
196         return false;
197     }
198 
199     /**
200      * removed node
201      * @param name
202      * @param nodeName
203      */
204     private void remove(String name, String nodeName) {
205         try {
206             final String hmName;
207             if (StringUtils.equalsIgnoreCase(nodeName, NODE_ROLES)) {
208                 hmName = ContentRepository.USER_ROLES;
209             }
210             else {
211                 hmName = ContentRepository.USER_GROUPS;
212             }
213             final HierarchyManager hm = MgnlSecurityUtil.getContextHierarchyManager(hmName);
214             Content node = getGroupNode().getContent(nodeName);
215 
216             for (NodeData nodeData: node.getNodeDataCollection()) {
217                 // check for the existence of this ID
218                 try {
219                     if (hm.getContentByUUID(nodeData.getString()).getName().equalsIgnoreCase(name)) {
220                         nodeData.delete();
221                     }
222                 }
223                 catch (ItemNotFoundException e) {
224                     log.debug("[{}] does not exist in the {} repository", name, hmName);
225                 }
226                 catch (IllegalArgumentException e) {
227                     log.debug(nodeData.getHandle() + " has invalid value");
228                 }
229             }
230             getGroupNode().save();
231         }
232         catch (RepositoryException e) {
233             log.error("failed to remove " + name + " from group [" + this.getName() + "]", e);
234         }
235     }
236 
237     /**
238      * adds a new node under specified node collection
239      */
240     private void add(String name, String nodeName) {
241         try {
242             final Content groupNode = getGroupNode();
243             final String hmName;
244             if (StringUtils.equalsIgnoreCase(nodeName, NODE_ROLES)) {
245                 hmName = ContentRepository.USER_ROLES;
246             } else {
247                 hmName = ContentRepository.USER_GROUPS;
248             }
249 
250             final HierarchyManager hm = MgnlSecurityUtil.getContextHierarchyManager(hmName);
251 
252             if (!this.hasAny(name, nodeName)) {
253                 if (!groupNode.hasContent(nodeName)) {
254                     groupNode.createContent(nodeName, ItemType.CONTENTNODE);
255                 }
256                 Content node = groupNode.getContent(nodeName);
257                 // add corresponding ID
258                 try {
259                     String value = hm.getContent("/" + name).getUUID(); // assuming that there is a flat hierarchy
260                     // used only to get the unique label
261                     final HierarchyManager sysHM = MgnlSecurityUtil.getSystemHierarchyManager(ContentRepository.USER_GROUPS);
262                     final String newName = Path.getUniqueLabel(sysHM, node.getHandle(), "0");
263                     node.createNodeData(newName).setValue(value);
264                     groupNode.save();
265                 }
266                 catch (PathNotFoundException e) {
267                     log.debug("[{}] does not exist in the {} repository", name, hmName);
268                 }
269             }
270         }
271         catch (RepositoryException e) {
272             log.error("failed to add " + name + " to group [" + this.getName() + "]", e);
273         }
274     }
275 
276     public Content getGroupNode() {
277         return groupNode;
278     }
279 }