01: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
02: *
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * 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: * Property editor to assist with the setup of a {@link UserMap}.<p>The format of entries should be:</p>
30: * <p><code> username=password,grantedAuthority[,grantedAuthority][,enabled|disabled] </code></p>
31: * <p>The <code>password</code> must always be the first entry after the equals. The <code>enabled</code> or
32: * <code>disabled</code> keyword can appear anywhere (apart from the first entry reserved for the password). If
33: * neither <code>enabled</code> or <code>disabled</code> appear, the default is <code>enabled</code>. At least one
34: * granted authority must be listed.</p>
35: * <p>The <code>username</code> represents the key and duplicates are handled the same was as duplicates would be
36: * in Java <code>Properties</code> files.</p>
37: * <p>If the above requirements are not met, the invalid entry will be silently ignored.</p>
38: * <p>This editor always assumes each entry has a non-expired account and non-expired credentials. However, it
39: * does honour the user enabled/disabled flag as described above.</p>
40: *
41: * @author Ben Alex
42: * @version $Id: UserMapEditor.java 1496 2006-05-23 13:38:33Z benalex $
43: */
44: public class UserMapEditor extends PropertyEditorSupport {
45: //~ Methods ========================================================================================================
46:
47: public static UserMap addUsersFromProperties(UserMap userMap,
48: 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
60: .getValue();
61:
62: // Make a user object, assuming the properties were properly provided
63: if (attr != null) {
64: UserDetails user = new User(username, attr
65: .getPassword(), attr.isEnabled(), true, true,
66: true, attr.getAuthorities());
67: userMap.addUser(user);
68: }
69: }
70:
71: return userMap;
72: }
73:
74: public void setAsText(String s) throws IllegalArgumentException {
75: UserMap userMap = new UserMap();
76:
77: if ((s == null) || "".equals(s)) {
78: // Leave value in property editor null
79: } else {
80: // Use properties editor to tokenize the string
81: PropertiesEditor propertiesEditor = new PropertiesEditor();
82: propertiesEditor.setAsText(s);
83:
84: Properties props = (Properties) propertiesEditor.getValue();
85: addUsersFromProperties(userMap, props);
86: }
87:
88: setValue(userMap);
89: }
90: }
|