KickJava   Java API By Example, From Geeks To Geeks.

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


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.security.auth.Entity;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import javax.jcr.PathNotFoundException;
27 import javax.jcr.RepositoryException;
28 import javax.security.auth.Subject JavaDoc;
29
30 import org.apache.commons.codec.binary.Base64;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34
35 /**
36  * Manages the users stored in magnolia itself.
37  * @author philipp
38  * @version $Revision: 7706 $ ($Author: gjoseph $)
39  */

40 public class MgnlUserManager implements UserManager {
41
42     /**
43      * Logger
44      */

45     public static Logger log = LoggerFactory.getLogger(MgnlUserManager.class);
46
47     /**
48      * Do not instantiate it!
49      */

50     public MgnlUserManager() {
51     }
52
53     /**
54      * Get the user object
55      * @param name
56      * @return the user object
57      */

58     public User getUser(String JavaDoc name) {
59         try {
60             Content node = getHierarchyManager().getContent(name);
61             MgnlUser user = new MgnlUser(node);
62             user.setLastAccess();
63             return user;
64         }
65         catch (PathNotFoundException e) {
66             log.debug("User not found: [{}]", name);
67             return null;
68         }
69         catch (RepositoryException e) {
70             log.info("Unable to load user [" + name + "] due to: " //
71
+ e.getClass().getName()
72                 + " - "
73                 + e.getMessage(), e);
74             return null;
75         }
76     }
77
78     /**
79      * Get system user, this user must always exist in magnolia repository.
80      * @return system user
81      */

82     public User getSystemUser() {
83         try {
84             return new MgnlUser(getHierarchyManager().getContent(UserManager.SYSTEM_USER));
85         }
86         catch (Exception JavaDoc e) {
87             log.error("failed to get System user", e);
88             log.info("Try to create new system user with default password");
89             return this.createUser(UserManager.SYSTEM_USER, UserManager.SYSTEM_PSWD);
90         }
91     }
92
93     /**
94      * Get Anonymous user, this user must always exist in magnolia repository.
95      * @return anonymous user
96      */

97     public User getAnonymousUser() {
98         try {
99             return new MgnlUser(getHierarchyManager().getContent(UserManager.ANONYMOUS_USER));
100         }
101         catch (Exception JavaDoc e) {
102             log.error("failed to get Anonymous user", e);
103             log.info("Try to create new system user with default password");
104             return this.createUser(UserManager.ANONYMOUS_USER, "");
105         }
106     }
107
108     /**
109      * All users
110      */

111     public Collection JavaDoc getAllUsers() {
112         Collection JavaDoc users = new ArrayList JavaDoc();
113         try {
114             Collection JavaDoc nodes = getHierarchyManager().getRoot().getChildren(ItemType.USER);
115             for (Iterator JavaDoc iter = nodes.iterator(); iter.hasNext();) {
116                 users.add(new MgnlUser((Content) iter.next()));
117             }
118         }
119         catch (Exception JavaDoc e) {
120             log.error("can't find user");
121         }
122         return users;
123     }
124
125     /**
126      * @param name
127      * @param pw
128      * @return the created User
129      */

130     public User createUser(String JavaDoc name, String JavaDoc pw) {
131         try {
132             Content node;
133             node = getHierarchyManager().createContent("/", name, ItemType.USER.getSystemName());
134             node.createNodeData("name").setValue(name);
135             node.createNodeData("pswd").setValue(new String JavaDoc(Base64.encodeBase64(pw.getBytes())));
136             node.createNodeData("language").setValue("en");
137             getHierarchyManager().save();
138             return new MgnlUser(node);
139         }
140         catch (Exception JavaDoc e) {
141             log.info("can't create user [" + name + "]", e);
142             return null;
143         }
144     }
145
146     /**
147      * Initialize new user using JAAS authenticated/authorized subject
148      * @param subject
149      * @throws UnsupportedOperationException
150      */

151     public User getUser(Subject JavaDoc subject) throws UnsupportedOperationException JavaDoc {
152         User user = null;
153         // this could be the case if no one is logged in yet
154
if (subject == null) {
155             return new DummyUser();
156         }
157
158         Set JavaDoc principalSet = subject.getPrincipals(Entity.class);
159         Iterator JavaDoc entityIterator = principalSet.iterator();
160         Entity userDetails = (Entity) entityIterator.next();
161         String JavaDoc name = (String JavaDoc) userDetails.getProperty(Entity.NAME);
162         try {
163             Content node = getHierarchyManager().getContent(name);
164             user = new MgnlUser(node);
165             ((MgnlUser) user).setLastAccess();
166         }
167         catch (PathNotFoundException e) {
168             log.error("user not registered in magnolia itself [" + name + "]");
169         }
170         catch (Exception JavaDoc e) {
171             log.error("can't get jcr-node of current user", e);
172         }
173         if (user == null) {
174             user = new DummyUser();
175         }
176
177         return user;
178     }
179
180     /**
181      * return the user HierarchyManager
182      */

183     protected HierarchyManager getHierarchyManager() {
184         return ContentRepository.getHierarchyManager(ContentRepository.USERS);
185     }
186
187 }
188
Popular Tags