KickJava   Java API By Example, From Geeks To Geeks.

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


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.cms.util.NodeDataUtil;
22 import info.magnolia.context.MgnlContext;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.GregorianCalendar JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 import javax.jcr.ItemNotFoundException;
30 import javax.jcr.PathNotFoundException;
31 import javax.jcr.PropertyType;
32 import javax.jcr.RepositoryException;
33
34 import org.apache.commons.codec.binary.Base64;
35 import org.apache.commons.lang.StringUtils;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39
40 /**
41  * This class wrapps a user content object to provide some nice methods
42  * @author philipp
43  * @version $Revision: 7706 $ ($Author: gjoseph $)
44  */

45 public class MgnlUser implements User {
46
47     public static Logger log = LoggerFactory.getLogger(User.class);
48
49     /**
50      * Under this subnodes the assigned roles are saved
51      */

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

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

56     /**
57      * the content object
58      */

59     private Content userNode;
60
61     /**
62      * @param userNode the Content object representing this user
63      */

64     protected MgnlUser(Content userNode) {
65         this.userNode = userNode;
66     }
67
68     /**
69      * Is this user in a specified role?
70      * @param groupName the name of the role
71      * @return true if in role
72      */

73     public boolean inGroup(String JavaDoc groupName) {
74         return this.hasAny(groupName, NODE_GROUPS);
75     }
76
77     /**
78      * Remove a group. Implementation is optional
79      * @param groupName
80      */

81     public void removeGroup(String JavaDoc groupName) throws UnsupportedOperationException JavaDoc {
82         this.remove(groupName, NODE_GROUPS);
83     }
84
85     /**
86      * Adds this user to a group. Implementation is optional
87      * @param groupName
88      */

89     public void addGroup(String JavaDoc groupName) throws UnsupportedOperationException JavaDoc {
90         this.add(groupName, NODE_GROUPS);
91     }
92
93     /**
94      * Is this user in a specified role?
95      * @param roleName the name of the role
96      * @return true if in role
97      */

98     public boolean hasRole(String JavaDoc roleName) {
99         return this.hasAny(roleName, NODE_ROLES);
100     }
101
102     public void removeRole(String JavaDoc roleName) {
103         this.remove(roleName, NODE_ROLES);
104     }
105
106     /**
107      * Adds a role to this user
108      * @param roleName the name of the role
109      */

110     public void addRole(String JavaDoc roleName) {
111         this.add(roleName, NODE_ROLES);
112     }
113
114     /**
115      * checks is any object exist with the given name under this node
116      * @param name
117      * @param nodeName
118      */

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

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

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

241     public String JavaDoc getName() {
242         return this.userNode.getName();
243     }
244
245     /**
246      * get user password
247      * @return password string
248      */

249     public String JavaDoc getPassword() {
250         String JavaDoc pswd = this.userNode.getNodeData("pswd").getString().trim();
251         return new String JavaDoc(Base64.decodeBase64(pswd.getBytes()));
252     }
253
254     /**
255      * the language of the current user
256      */

257     public String JavaDoc getLanguage() {
258         return userNode.getNodeData("language").getString(); //$NON-NLS-1$
259
}
260
261     public Collection JavaDoc getGroups() {
262         ArrayList JavaDoc list = new ArrayList JavaDoc();
263
264         try {
265             Content groups = null;
266             try {
267                 // get "groups" node under node "user"
268
groups = userNode.getContent("groups");
269             }
270             catch (javax.jcr.PathNotFoundException e) {
271                 log.warn("the user " + getName() + " does have not groups node");
272             }
273
274             if (groups != null) {
275                 Collection JavaDoc c = groups.getNodeDataCollection();
276                 Iterator JavaDoc it = c.iterator();
277                 while (it.hasNext()) {
278                     NodeData nd = (NodeData) it.next();
279                     String JavaDoc uuid = nd.getString();
280                     Content group = MgnlContext
281                         .getSystemContext()
282                         .getHierarchyManager(ContentRepository.USER_GROUPS)
283                         .getContentByUUID(uuid);
284                     list.add(group.getName());
285                 }
286             }
287
288         }
289         catch (Exception JavaDoc e) {
290             log.warn("cant read groups of user.", e);
291         }
292
293         return list;
294     }
295
296     public Collection JavaDoc getRoles() {
297         ArrayList JavaDoc list = new ArrayList JavaDoc();
298
299         try {
300             Content roles = null;
301             try {
302                 // get "groups" node under node "user"
303
roles = userNode.getContent("roles");
304             }
305             catch (javax.jcr.PathNotFoundException e) {
306                 log.warn("the user " + getName() + " does have not roles node");
307             }
308
309             if (roles != null) {
310                 Collection JavaDoc c = roles.getNodeDataCollection();
311                 Iterator JavaDoc it = c.iterator();
312                 while (it.hasNext()) {
313                     NodeData nd = (NodeData) it.next();
314                     String JavaDoc uuid = nd.getString();
315                     Content role = MgnlContext
316                         .getSystemContext()
317                         .getHierarchyManager(ContentRepository.USER_ROLES)
318                         .getContentByUUID(uuid);
319                     list.add(role.getName());
320                 }
321             }
322
323         }
324         catch (Exception JavaDoc e) {
325             log.warn("can't read roles of user.", e);
326         }
327
328         return list;
329     }
330
331     /**
332      * Update the "last access" timestamp.
333      */

334     public void setLastAccess() {
335
336         NodeData lastaccess;
337         try {
338             lastaccess = NodeDataUtil.getOrCreate(userNode, "lastaccess", PropertyType.DATE);
339             synchronized (lastaccess) {
340                 lastaccess.setValue(new GregorianCalendar JavaDoc());
341                 lastaccess.save();
342             }
343         }
344         catch (RepositoryException e) {
345             log.debug(
346                 "Unable to set the last access date due to a " + e.getClass().getName() + " - " + e.getMessage(),
347                 e);
348         }
349
350     }
351 }
Popular Tags