| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| UserMapEditor |
|
| 2.5;2.5 |
| 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 | 44 | 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 | 43 | UserAttributeEditor configAttribEd = new UserAttributeEditor(); |
| 51 | ||
| 52 | 43 | for (Iterator iter = props.keySet().iterator(); iter.hasNext();) { |
| 53 | 52 | String username = (String) iter.next(); |
| 54 | 52 | String value = props.getProperty(username); |
| 55 | ||
| 56 | // Convert value to a password, enabled setting, and list of granted authorities |
|
| 57 | 52 | configAttribEd.setAsText(value); |
| 58 | ||
| 59 | 52 | UserAttribute attr = (UserAttribute) configAttribEd.getValue(); |
| 60 | ||
| 61 | // Make a user object, assuming the properties were properly provided |
|
| 62 | 52 | if (attr != null) { |
| 63 | 51 | UserDetails user = new User(username, attr.getPassword(), attr.isEnabled(), true, true, true, |
| 64 | attr.getAuthorities()); |
|
| 65 | 51 | userMap.addUser(user); |
| 66 | } |
|
| 67 | 52 | } |
| 68 | ||
| 69 | 43 | return userMap; |
| 70 | } |
|
| 71 | ||
| 72 | public void setAsText(String s) throws IllegalArgumentException { |
|
| 73 | 44 | UserMap userMap = new UserMap(); |
| 74 | ||
| 75 | 44 | if ((s == null) || "".equals(s)) { |
| 76 | // Leave value in property editor null |
|
| 77 | } else { |
|
| 78 | // Use properties editor to tokenize the string |
|
| 79 | 42 | PropertiesEditor propertiesEditor = new PropertiesEditor(); |
| 80 | 42 | propertiesEditor.setAsText(s); |
| 81 | ||
| 82 | 42 | Properties props = (Properties) propertiesEditor.getValue(); |
| 83 | 42 | addUsersFromProperties(userMap, props); |
| 84 | } |
|
| 85 | ||
| 86 | 44 | setValue(userMap); |
| 87 | 44 | } |
| 88 | } |