1 17 18 package org.apache.lenya.ac.file; 19 20 import java.io.File ; 21 import java.io.Serializable ; 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 38 public class FileUser extends AbstractUser implements Item, Serializable { 39 private static final Logger log = Logger.getLogger(FileUser.class); 40 41 public static final String ID = "identity"; 42 public static final String EMAIL = "email"; 43 public static final String PASSWORD = "password"; 44 public static final String GROUPS = "groups"; 45 public static final String GROUP = "group"; 46 public static final String PASSWORD_ATTRIBUTE = "type"; 47 48 51 public FileUser() { 52 } 53 54 63 public FileUser(File configurationDirectory, String id, String fullName, String email, 64 String password) { 65 super(id, fullName, email, password); 66 setConfigurationDirectory(configurationDirectory); 67 } 68 69 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 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 log.error("User " + getId() + 112 " doesn't seem to have any groups"); 113 } 114 } 115 116 117 123 protected Configuration createConfiguration() { 124 DefaultConfiguration config = new DefaultConfiguration(ID); 125 new ItemConfiguration().save(this, config); 126 127 DefaultConfiguration child = null; 128 129 child = new DefaultConfiguration(EMAIL); 131 child.setValue(getEmail()); 132 config.addChild(child); 133 134 child = new DefaultConfiguration(PASSWORD); 136 child.setValue(getEncryptedPassword()); 137 child.setAttribute(PASSWORD_ATTRIBUTE, "md5"); 138 config.addChild(child); 139 140 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 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 e) { 165 throw new AccessControlException(e); 166 } 167 } 168 169 172 public void delete() throws AccessControlException { 173 super.delete(); 174 getFile().delete(); 175 } 176 177 181 protected File getFile() { 182 File xmlPath = getConfigurationDirectory(); 183 File xmlFile = new File (xmlPath, getId() + FileUserManager.SUFFIX); 184 return xmlFile; 185 } 186 187 private File configurationDirectory; 188 189 193 protected File getConfigurationDirectory() { 194 return configurationDirectory; 195 } 196 197 200 public void setConfigurationDirectory(File configurationDirectory) { 201 assert (configurationDirectory != null) && configurationDirectory.isDirectory(); 202 this.configurationDirectory = configurationDirectory; 203 } 204 205 } 206 | Popular Tags |