KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > ac > file > FileUser


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 package org.apache.lenya.ac.file;
19
20 import java.io.File JavaDoc;
21 import java.io.Serializable JavaDoc;
22
23 import org.apache.avalon.framework.configuration.Configuration;
24 import org.apache.avalon.framework.configuration.ConfigurationException;
25 import org.apache.avalon.framework.configuration.DefaultConfiguration;
26 import org.apache.avalon.framework.configuration.DefaultConfigurationSerializer;
27 import org.apache.lenya.ac.AccessControlException;
28 import org.apache.lenya.ac.Group;
29 import org.apache.lenya.ac.Item;
30 import org.apache.lenya.ac.impl.AbstractUser;
31 import org.apache.lenya.ac.impl.ItemConfiguration;
32 import org.apache.log4j.Logger;
33
34 /**
35  * File-based user implementation.
36  * @version $Id: FileUser.java 159123 2005-03-26 22:31:39Z gregor $
37  */

38 public class FileUser extends AbstractUser implements Item, Serializable JavaDoc {
39     private static final Logger log = Logger.getLogger(FileUser.class);
40
41     public static final String JavaDoc ID = "identity";
42     public static final String JavaDoc EMAIL = "email";
43     public static final String JavaDoc PASSWORD = "password";
44     public static final String JavaDoc GROUPS = "groups";
45     public static final String JavaDoc GROUP = "group";
46     public static final String JavaDoc PASSWORD_ATTRIBUTE = "type";
47
48     /**
49      * Creates a new FileUser object.
50      */

51     public FileUser() {
52     }
53
54     /**
55      * Create a FileUser
56      *
57      * @param configurationDirectory where the user will be attached to
58      * @param id the user id
59      * @param fullName the full name of the user
60      * @param email the users email address
61      * @param password the users password
62      */

63     public FileUser(File JavaDoc configurationDirectory, String JavaDoc id, String JavaDoc fullName, String JavaDoc email,
64         String JavaDoc password) {
65         super(id, fullName, email, password);
66         setConfigurationDirectory(configurationDirectory);
67     }
68
69     /**
70      * Configure this FileUser.
71      *
72      * @param config where the user details are specified
73      * @throws ConfigurationException if the necessary details aren't specified in the config
74      */

75     public void configure(Configuration config) throws ConfigurationException {
76         new ItemConfiguration().configure(this, config);
77         setEmail(config.getChild(EMAIL).getValue(""));
78         setEncryptedPassword(config.getChild(PASSWORD).getValue(null));
79
80         removeFromAllGroups();
81         Configuration[] groups = config.getChildren(GROUPS);
82
83         if (groups.length == 1) {
84             groups = groups[0].getChildren(GROUP);
85
86             FileGroupManager manager = null;
87
88             try {
89                 manager = FileGroupManager.instance(configurationDirectory);
90             } catch (AccessControlException e) {
91                 throw new ConfigurationException(
92                     "Exception when trying to fetch GroupManager for directory: [" +
93                     configurationDirectory + "]", e);
94             }
95
96             for (int i = 0; i < groups.length; i++) {
97                 String JavaDoc groupId = groups[i].getValue();
98                 Group group = manager.getGroup(groupId);
99
100                 if (group == null) {
101                     throw new ConfigurationException("Couldn't find Group for group name [" + groupId + "]");
102                 }
103                 
104                 if (!group.contains(this)) {
105                     group.add(this);
106                 }
107
108             }
109         } else {
110             // strange, it should have groups
111
log.error("User " + getId() +
112                 " doesn't seem to have any groups");
113         }
114     }
115
116
117     /**
118      * Create a configuration from the current user details. Can
119      * be used for saving.
120      *
121      * @return a <code>Configuration</code>
122      */

123     protected Configuration createConfiguration() {
124         DefaultConfiguration config = new DefaultConfiguration(ID);
125         new ItemConfiguration().save(this, config);
126
127         DefaultConfiguration child = null;
128
129         // add email node
130
child = new DefaultConfiguration(EMAIL);
131         child.setValue(getEmail());
132         config.addChild(child);
133
134         // add password node
135
child = new DefaultConfiguration(PASSWORD);
136         child.setValue(getEncryptedPassword());
137         child.setAttribute(PASSWORD_ATTRIBUTE, "md5");
138         config.addChild(child);
139
140         // add group nodes
141
child = new DefaultConfiguration(GROUPS);
142         config.addChild(child);
143
144         Group[] groups = getGroups();
145
146         for (int i = 0; i < groups.length; i++) {
147             DefaultConfiguration groupNode = new DefaultConfiguration(GROUP);
148             groupNode.setValue(groups[i].getId());
149             child.addChild(groupNode);
150         }
151
152         return config;
153     }
154
155     /**
156      * @see org.apache.lenya.ac.User#save()
157      */

158     public void save() throws AccessControlException {
159         DefaultConfigurationSerializer serializer = new DefaultConfigurationSerializer();
160         Configuration config = createConfiguration();
161
162         try {
163             serializer.serializeToFile(getFile(), config);
164         } catch (Exception JavaDoc e) {
165             throw new AccessControlException(e);
166         }
167     }
168
169     /**
170      * @see org.apache.lenya.ac.User#delete()
171      */

172     public void delete() throws AccessControlException {
173         super.delete();
174         getFile().delete();
175     }
176     
177     /**
178      * Returns the configuration file.
179      * @return A file object.
180      */

181     protected File JavaDoc getFile() {
182         File JavaDoc xmlPath = getConfigurationDirectory();
183         File JavaDoc xmlFile = new File JavaDoc(xmlPath, getId() + FileUserManager.SUFFIX);
184         return xmlFile;
185     }
186
187     private File JavaDoc configurationDirectory;
188
189     /**
190      * Returns the configuration directory.
191      * @return A file object.
192      */

193     protected File JavaDoc getConfigurationDirectory() {
194         return configurationDirectory;
195     }
196
197     /**
198      * @see org.apache.lenya.ac.Item#setConfigurationDirectory(java.io.File)
199      */

200     public void setConfigurationDirectory(File JavaDoc configurationDirectory) {
201         assert (configurationDirectory != null) && configurationDirectory.isDirectory();
202         this.configurationDirectory = configurationDirectory;
203     }
204     
205 }
206
Popular Tags