KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > security > MgnlGroup


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.security;
14
15 import info.magnolia.cms.beans.config.ContentRepository;
16 import info.magnolia.cms.core.Content;
17 import info.magnolia.cms.core.HierarchyManager;
18 import info.magnolia.cms.core.ItemType;
19 import info.magnolia.cms.core.NodeData;
20 import info.magnolia.cms.core.Path;
21 import info.magnolia.context.MgnlContext;
22
23 import java.util.Iterator JavaDoc;
24
25 import javax.jcr.ItemNotFoundException;
26 import javax.jcr.PathNotFoundException;
27 import javax.jcr.RepositoryException;
28
29 import org.apache.commons.lang.StringUtils;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34 /**
35  * @author Sameer Charles $Id: MgnlGroup.java 7706 2006-12-07 17:02:40Z gjoseph $
36  */

37 public class MgnlGroup implements Group {
38
39     /**
40      * Logger
41      */

42     public static Logger log = LoggerFactory.getLogger(MgnlGroup.class);
43
44     /**
45      * Under this subnodes the assigned roles are saved
46      */

47     private static final String JavaDoc NODE_ROLES = "roles"; //$NON-NLS-1$
48

49     private static final String JavaDoc NODE_GROUPS = "groups"; //$NON-NLS-1$
50

51     /**
52      * group node
53      */

54     private Content groupNode;
55
56     /**
57      * @param groupNode the Content object representing this group
58      */

59     MgnlGroup(Content groupNode) {
60         this.groupNode = groupNode;
61     }
62
63     /**
64      * get name of this node
65      * @return group name
66      */

67     public String JavaDoc getName() {
68         return this.groupNode.getName();
69     }
70
71     /**
72      * add role to this group
73      * @param roleName
74      * @throws UnsupportedOperationException if the implementation does not support writing
75      * @throws AccessDeniedException if loggen in repository user does not sufficient rights
76      */

77     public void addRole(String JavaDoc roleName) throws UnsupportedOperationException JavaDoc, AccessDeniedException {
78         this.add(roleName, NODE_ROLES);
79     }
80
81     /**
82      * add subgroup to this group
83      * @param groupName
84      * @throws UnsupportedOperationException if the implementation does not support writing
85      * @throws AccessDeniedException if loggen in repository user does not sufficient rights
86      */

87     public void addGroup(String JavaDoc groupName) throws UnsupportedOperationException JavaDoc, AccessDeniedException {
88         this.add(groupName, NODE_GROUPS);
89     }
90
91     /**
92      * remove role from this group
93      * @param roleName
94      * @throws UnsupportedOperationException if the implementation does not support writing
95      * @throws AccessDeniedException if loggen in repository user does not sufficient rights
96      */

97     public void removeRole(String JavaDoc roleName) throws UnsupportedOperationException JavaDoc, AccessDeniedException {
98         this.remove(roleName, NODE_ROLES);
99     }
100
101     /**
102      * remove subgroup from this group
103      * @param groupName
104      * @throws UnsupportedOperationException if the implementation does not support writing
105      * @throws AccessDeniedException if loggen in repository user does not sufficient rights
106      */

107     public void removeGroup(String JavaDoc groupName) throws UnsupportedOperationException JavaDoc, AccessDeniedException {
108         this.remove(groupName, NODE_GROUPS);
109     }
110
111     /**
112      * returns true if role exist in this group
113      * @param roleName
114      * @throws UnsupportedOperationException if the implementation does not exist
115      * @throws AccessDeniedException if loggen in repository user does not sufficient rights
116      */

117     public boolean hasRole(String JavaDoc roleName) throws UnsupportedOperationException JavaDoc, AccessDeniedException {
118         return this.hasAny(roleName, NODE_ROLES);
119     }
120
121     /**
122      * checks is any object exist with the given name under this node
123      * @param name
124      * @param nodeName
125      */

126     private boolean hasAny(String JavaDoc name, String JavaDoc nodeName) {
127         try {
128             HierarchyManager hm;
129             if (StringUtils.equalsIgnoreCase(nodeName, NODE_ROLES)) {
130                 hm = MgnlContext.getHierarchyManager(ContentRepository.USER_ROLES);
131             }
132             else {
133                 hm = MgnlContext.getHierarchyManager(ContentRepository.USER_GROUPS);
134             }
135
136             Content node = groupNode.getContent(nodeName);
137             for (Iterator JavaDoc iter = node.getNodeDataCollection().iterator(); iter.hasNext();) {
138                 NodeData nodeData = (NodeData) iter.next();
139                 // check for the existence of this ID
140
try {
141                     if (hm.getContentByUUID(nodeData.getString()).getName().equalsIgnoreCase(name)) {
142                         return true;
143                     }
144                 }
145                 catch (ItemNotFoundException e) {
146                     if (log.isDebugEnabled()) {
147                         log.debug("Role [ " + name + " ] does not exist in the ROLES repository");
148                     }
149                 }
150                 catch (IllegalArgumentException JavaDoc e) {
151                     if (log.isDebugEnabled()) {
152                         log.debug(nodeData.getHandle() + " has invalid value");
153                     }
154                 }
155             }
156         }
157         catch (RepositoryException e) {
158             log.debug(e.getMessage(), e);
159         }
160         return false;
161     }
162
163     /**
164      * removed node
165      * @param name
166      * @param nodeName
167      */

168     private void remove(String JavaDoc name, String JavaDoc nodeName) {
169         try {
170             HierarchyManager hm;
171             if (StringUtils.equalsIgnoreCase(nodeName, NODE_ROLES)) {
172                 hm = MgnlContext.getHierarchyManager(ContentRepository.USER_ROLES);
173             }
174             else {
175                 hm = MgnlContext.getHierarchyManager(ContentRepository.USER_GROUPS);
176             }
177             Content node = groupNode.getContent(nodeName);
178
179             for (Iterator JavaDoc iter = node.getNodeDataCollection().iterator(); iter.hasNext();) {
180                 NodeData nodeData = (NodeData) iter.next();
181                 // check for the existence of this ID
182
try {
183                     if (hm.getContentByUUID(nodeData.getString()).getName().equalsIgnoreCase(name)) {
184                         nodeData.delete();
185                     }
186                 }
187                 catch (ItemNotFoundException e) {
188                     if (log.isDebugEnabled()) {
189                         log.debug("Role [ " + name + " ] does not exist in the ROLES repository");
190                     }
191                 }
192                 catch (IllegalArgumentException JavaDoc e) {
193                     if (log.isDebugEnabled()) {
194                         log.debug(nodeData.getHandle() + " has invalid value");
195                     }
196                 }
197             }
198             groupNode.save();
199         }
200         catch (RepositoryException e) {
201             log.error("failed to remove " + name + " from user [" + this.getName() + "]", e);
202         }
203     }
204
205     /**
206      * adds a new node under specified node collection
207      */

208     private void add(String JavaDoc name, String JavaDoc nodeName) {
209         try {
210             HierarchyManager hm;
211             if (StringUtils.equalsIgnoreCase(nodeName, NODE_ROLES)) {
212                 hm = MgnlContext.getHierarchyManager(ContentRepository.USER_ROLES);
213             }
214             else {
215                 hm = MgnlContext.getHierarchyManager(ContentRepository.USER_GROUPS);
216             }
217
218             if (!this.hasAny(name, nodeName)) {
219                if (!groupNode.hasContent(nodeName)) {
220                     groupNode.createContent(nodeName, ItemType.CONTENTNODE);
221                }
222                 Content node = groupNode.getContent(nodeName);
223                 // add corresponding ID
224
try {
225                     String JavaDoc value = hm.getContent("/" + name).getUUID(); // assuming that there is a flat hierarchy
226
// used only to get the unique label
227
HierarchyManager usersHM = ContentRepository.getHierarchyManager(ContentRepository.USERS);
228                     String JavaDoc newName = Path.getUniqueLabel(usersHM, node.getHandle(), "0");
229                     node.createNodeData(newName).setValue(value);
230                     groupNode.save();
231                 }
232                 catch (PathNotFoundException e) {
233                     if (log.isDebugEnabled()) {
234                         log.debug("Role [ " + name + " ] does not exist in the ROLES repository");
235                     }
236                 }
237             }
238         }
239         catch (RepositoryException e) {
240             log.error("failed to add " + name + " to user [" + this.getName() + "]", e);
241         }
242     }
243
244     /**
245      * return the role HierarchyManager
246      */

247     protected HierarchyManager getHierarchyManager() {
248         return MgnlContext.getHierarchyManager(ContentRepository.USER_GROUPS);
249     }
250
251 }
252
Popular Tags