1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.userdetails.memory;
17
18 import java.beans.PropertyEditorSupport;
19 import java.util.Iterator;
20 import java.util.Properties;
21
22 import org.acegisecurity.userdetails.User;
23 import org.acegisecurity.userdetails.UserDetails;
24 import org.springframework.beans.propertyeditors.PropertiesEditor;
25
26
27 /***
28 * Property editor to assist with the setup of a {@link UserMap}.
29 *
30 * <p>
31 * The format of entries should be:
32 * </p>
33 *
34 * <p>
35 * <code>
36 * username=password,grantedAuthority[,grantedAuthority][,enabled|disabled]
37 * </code>
38 * </p>
39 *
40 * <p>
41 * The <code>password</code> must always be the first entry after the equals.
42 * The <code>enabled</code> or <code>disabled</code> keyword can appear
43 * anywhere (apart from the first entry reserved for the password). If neither
44 * <code>enabled</code> or <code>disabled</code> appear, the default is
45 * <code>enabled</code>. At least one granted authority must be listed.
46 * </p>
47 *
48 * <p>
49 * The <code>username</code> represents the key and duplicates are handled the
50 * same was as duplicates would be in Java <code>Properties</code> files.
51 * </p>
52 *
53 * <p>
54 * If the above requirements are not met, the invalid entry will be silently
55 * ignored.
56 * </p>
57 *
58 * <p>
59 * This editor always assumes each entry has a non-expired account and
60 * non-expired credentials. However, it does honour the user enabled/disabled
61 * flag as described above.
62 * </p>
63 *
64 * @author Ben Alex
65 * @version $Id: UserMapEditor.java,v 1.10 2005/11/29 13:10:09 benalex Exp $
66 */
67 public class UserMapEditor extends PropertyEditorSupport {
68
69
70 public void setAsText(String s) throws IllegalArgumentException {
71 UserMap userMap = new UserMap();
72
73 if ((s == null) || "".equals(s)) {
74
75 } else {
76
77 PropertiesEditor propertiesEditor = new PropertiesEditor();
78 propertiesEditor.setAsText(s);
79
80 Properties props = (Properties) propertiesEditor.getValue();
81 addUsersFromProperties(userMap, props);
82 }
83
84 setValue(userMap);
85 }
86
87 public static UserMap addUsersFromProperties(UserMap userMap,
88 Properties props) {
89
90 UserAttributeEditor configAttribEd = new UserAttributeEditor();
91
92 for (Iterator iter = props.keySet().iterator(); iter.hasNext();) {
93 String username = (String) iter.next();
94 String value = props.getProperty(username);
95
96
97 configAttribEd.setAsText(value);
98
99 UserAttribute attr = (UserAttribute) configAttribEd.getValue();
100
101
102 if (attr != null) {
103 UserDetails user = new User(username, attr.getPassword(),
104 attr.isEnabled(), true, true, true,
105 attr.getAuthorities());
106 userMap.addUser(user);
107 }
108 }
109
110 return userMap;
111 }
112 }