View Javadoc

1   /* Copyright 2004, 2005 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 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      //~ Methods ================================================================
69  
70      public void setAsText(String s) throws IllegalArgumentException {
71          UserMap userMap = new UserMap();
72  
73          if ((s == null) || "".equals(s)) {
74              // Leave value in property editor null
75          } else {
76              // Use properties editor to tokenize the string
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          // Now we have properties, process each one individually
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              // Convert value to a password, enabled setting, and list of granted authorities
97              configAttribEd.setAsText(value);
98  
99              UserAttribute attr = (UserAttribute) configAttribEd.getValue();
100 
101             // Make a user object, assuming the properties were properly provided
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 }