1 /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 package org.acegisecurity.userdetails.memory;
17
18 import org.acegisecurity.userdetails.User;
19 import org.acegisecurity.userdetails.UserDetails;
20
21 import org.springframework.beans.propertyeditors.PropertiesEditor;
22
23 import java.beans.PropertyEditorSupport;
24
25 import java.util.Iterator;
26 import java.util.Properties;
27
28
29 /**
30 * Property editor to assist with the setup of a {@link UserMap}.<p>The format of entries should be:</p>
31 * <p><code> username=password,grantedAuthority[,grantedAuthority][,enabled|disabled] </code></p>
32 * <p>The <code>password</code> must always be the first entry after the equals. The <code>enabled</code> or
33 * <code>disabled</code> keyword can appear anywhere (apart from the first entry reserved for the password). If
34 * neither <code>enabled</code> or <code>disabled</code> appear, the default is <code>enabled</code>. At least one
35 * granted authority must be listed.</p>
36 * <p>The <code>username</code> represents the key and duplicates are handled the same was as duplicates would be
37 * in Java <code>Properties</code> files.</p>
38 * <p>If the above requirements are not met, the invalid entry will be silently ignored.</p>
39 * <p>This editor always assumes each entry has a non-expired account and non-expired credentials. However, it
40 * does honour the user enabled/disabled flag as described above.</p>
41 *
42 * @author Ben Alex
43 * @version $Id: UserMapEditor.java 1496 2006-05-23 13:38:33Z benalex $
44 */
45 public class UserMapEditor extends PropertyEditorSupport {
46 //~ Methods ========================================================================================================
47
48 public static UserMap addUsersFromProperties(UserMap userMap, Properties props) {
49 // Now we have properties, process each one individually
50 UserAttributeEditor configAttribEd = new UserAttributeEditor();
51
52 for (Iterator iter = props.keySet().iterator(); iter.hasNext();) {
53 String username = (String) iter.next();
54 String value = props.getProperty(username);
55
56 // Convert value to a password, enabled setting, and list of granted authorities
57 configAttribEd.setAsText(value);
58
59 UserAttribute attr = (UserAttribute) configAttribEd.getValue();
60
61 // Make a user object, assuming the properties were properly provided
62 if (attr != null) {
63 UserDetails user = new User(username, attr.getPassword(), attr.isEnabled(), true, true, true,
64 attr.getAuthorities());
65 userMap.addUser(user);
66 }
67 }
68
69 return userMap;
70 }
71
72 public void setAsText(String s) throws IllegalArgumentException {
73 UserMap userMap = new UserMap();
74
75 if ((s == null) || "".equals(s)) {
76 // Leave value in property editor null
77 } else {
78 // Use properties editor to tokenize the string
79 PropertiesEditor propertiesEditor = new PropertiesEditor();
80 propertiesEditor.setAsText(s);
81
82 Properties props = (Properties) propertiesEditor.getValue();
83 addUsersFromProperties(userMap, props);
84 }
85
86 setValue(userMap);
87 }
88 }