001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.lenya.ac.file;
020:
021: import java.io.File;
022: import java.io.Serializable;
023:
024: import org.apache.avalon.framework.configuration.Configuration;
025: import org.apache.avalon.framework.configuration.ConfigurationException;
026: import org.apache.avalon.framework.configuration.DefaultConfiguration;
027: import org.apache.avalon.framework.configuration.DefaultConfigurationSerializer;
028: import org.apache.avalon.framework.logger.Logger;
029: import org.apache.lenya.ac.AccessControlException;
030: import org.apache.lenya.ac.Group;
031: import org.apache.lenya.ac.GroupManager;
032: import org.apache.lenya.ac.Item;
033: import org.apache.lenya.ac.ItemManager;
034: import org.apache.lenya.ac.impl.AbstractUser;
035: import org.apache.lenya.ac.impl.ItemConfiguration;
036:
037: /**
038: * File-based user implementation.
039: * @version $Id: FileUser.java 485769 2006-12-11 17:41:23Z andreas $
040: */
041: public class FileUser extends AbstractUser implements Item,
042: Serializable {
043:
044: /**
045: *
046: */
047: private static final long serialVersionUID = 1L;
048: protected static final String ID = "identity";
049: protected static final String EMAIL = "email";
050: protected static final String MENU_LOCALE = "default-menu-locale";
051: protected static final String DOCUMENT_LOCALE = "default-document-locale";
052: protected static final String PASSWORD = "password";
053: protected static final String GROUPS = "groups";
054: protected static final String GROUP = "group";
055: protected static final String PASSWORD_ATTRIBUTE = "type";
056:
057: /**
058: * Creates a new FileUser object.
059: * @param itemManager The item manager.
060: * @param logger The logger.
061: */
062: public FileUser(ItemManager itemManager, Logger logger) {
063: super (itemManager, logger);
064: FileItemManager fileItemManager = (FileItemManager) itemManager;
065: setConfigurationDirectory(fileItemManager
066: .getConfigurationDirectory());
067: }
068:
069: /**
070: * Create a FileUser
071: * @param itemManager The item manager.
072: * @param logger The logger.
073: * @param id the user id
074: * @param fullName the full name of the user
075: * @param email the users email address
076: * @param password the users password
077: */
078: public FileUser(ItemManager itemManager, Logger logger, String id,
079: String fullName, String email, String password) {
080: super (itemManager, logger, id, fullName, email, password);
081: FileItemManager fileItemManager = (FileItemManager) itemManager;
082: setConfigurationDirectory(fileItemManager
083: .getConfigurationDirectory());
084: }
085:
086: /**
087: * Configure this FileUser.
088: * @param config where the user details are specified
089: * @throws ConfigurationException if the necessary details aren't specified
090: * in the config
091: */
092: public void configure(Configuration config)
093: throws ConfigurationException {
094: new ItemConfiguration().configure(this , config);
095: setEmail(config.getChild(EMAIL).getValue(""));
096: setDefaultMenuLocale(config.getChild(MENU_LOCALE).getValue(""));
097: setDefaultDocumentLocale(config.getChild(DOCUMENT_LOCALE)
098: .getValue(""));
099: setEncryptedPassword(config.getChild(PASSWORD).getValue(null));
100:
101: removeFromAllGroups();
102: Configuration[] groups = config.getChildren(GROUPS);
103:
104: if (groups.length == 1) {
105: groups = groups[0].getChildren(GROUP);
106:
107: GroupManager manager;
108: try {
109: manager = getAccreditableManager().getGroupManager();
110: } catch (AccessControlException e) {
111: throw new ConfigurationException(
112: "configuration failed: ", e);
113: }
114:
115: for (int i = 0; i < groups.length; i++) {
116: String groupId = groups[i].getValue();
117: Group group = manager.getGroup(groupId);
118:
119: if (group == null) {
120: throw new ConfigurationException(
121: "Couldn't find Group for group name ["
122: + groupId + "]");
123: }
124:
125: if (!group.contains(this )) {
126: group.add(this );
127: }
128:
129: }
130: } else {
131: // strange, it should have groups
132: getLogger().error(
133: "User " + getId()
134: + " doesn't seem to have any groups");
135: }
136: }
137:
138: /**
139: * Create a configuration from the current user details. Can be used for
140: * saving.
141: * @return a <code>Configuration</code>
142: */
143: protected Configuration createConfiguration() {
144: DefaultConfiguration config = new DefaultConfiguration(ID);
145: new ItemConfiguration().save(this , config);
146:
147: DefaultConfiguration child = null;
148:
149: // add email node
150: child = new DefaultConfiguration(EMAIL);
151: child.setValue(getEmail());
152: config.addChild(child);
153:
154: // add defaultMenuLocale node
155: child = new DefaultConfiguration(MENU_LOCALE);
156: child.setValue(getDefaultMenuLocale());
157: config.addChild(child);
158:
159: // add defaultDocumentLocale node
160: child = new DefaultConfiguration(DOCUMENT_LOCALE);
161: child.setValue(getDefaultDocumentLocale());
162: config.addChild(child);
163:
164: // add password node
165: child = new DefaultConfiguration(PASSWORD);
166: child.setValue(getEncryptedPassword());
167: child.setAttribute(PASSWORD_ATTRIBUTE, "md5");
168: config.addChild(child);
169:
170: // add group nodes
171: child = new DefaultConfiguration(GROUPS);
172: config.addChild(child);
173:
174: Group[] groups = getGroups();
175:
176: for (int i = 0; i < groups.length; i++) {
177: DefaultConfiguration groupNode = new DefaultConfiguration(
178: GROUP);
179: groupNode.setValue(groups[i].getId());
180: child.addChild(groupNode);
181: }
182:
183: return config;
184: }
185:
186: /**
187: * @see org.apache.lenya.ac.User#save()
188: */
189: public void save() throws AccessControlException {
190: DefaultConfigurationSerializer serializer = new DefaultConfigurationSerializer();
191: Configuration config = createConfiguration();
192:
193: try {
194: serializer.serializeToFile(getFile(), config);
195: } catch (Exception e) {
196: throw new AccessControlException(e);
197: }
198: }
199:
200: /**
201: * @see org.apache.lenya.ac.User#delete()
202: */
203: public void delete() throws AccessControlException {
204: super .delete();
205: getFile().delete();
206: }
207:
208: /**
209: * Returns the configuration file.
210: * @return A file object.
211: */
212: protected File getFile() {
213: File xmlPath = getConfigurationDirectory();
214: File xmlFile = new File(xmlPath, getId()
215: + FileUserManager.SUFFIX);
216: return xmlFile;
217: }
218:
219: private File configurationDirectory;
220:
221: /**
222: * Returns the configuration directory.
223: * @return A file object.
224: */
225: protected File getConfigurationDirectory() {
226: return this .configurationDirectory;
227: }
228:
229: protected void setConfigurationDirectory(
230: File _configurationDirectory) {
231: assert (_configurationDirectory != null)
232: && _configurationDirectory.isDirectory();
233: this.configurationDirectory = _configurationDirectory;
234: }
235:
236: }
|