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 java.beans.PropertyEditorSupport;
19: import java.util.ArrayList;
20: import java.util.List;
21:
22: import org.springframework.util.StringUtils;
23:
24: /**
25: * Property editor that creates a {@link UserAttribute} from a comma separated list of values.
26: *
27: * @author Ben Alex
28: * @version $Id: UserAttributeEditor.java 1642 2006-09-04 21:54:15Z carlossg $
29: */
30: public class UserAttributeEditor extends PropertyEditorSupport {
31: //~ Methods ========================================================================================================
32:
33: public void setAsText(String s) throws IllegalArgumentException {
34: if (StringUtils.hasText(s)) {
35: String[] tokens = StringUtils
36: .commaDelimitedListToStringArray(s);
37: UserAttribute userAttrib = new UserAttribute();
38:
39: List authoritiesAsString = new ArrayList();
40:
41: for (int i = 0; i < tokens.length; i++) {
42: String currentToken = tokens[i].trim();
43:
44: if (i == 0) {
45: userAttrib.setPassword(currentToken);
46: } else {
47: if (currentToken.toLowerCase().equals("enabled")) {
48: userAttrib.setEnabled(true);
49: } else if (currentToken.toLowerCase().equals(
50: "disabled")) {
51: userAttrib.setEnabled(false);
52: } else {
53: authoritiesAsString.add(currentToken);
54: }
55: }
56: }
57: userAttrib.setAuthoritiesAsString(authoritiesAsString);
58:
59: if (userAttrib.isValid()) {
60: setValue(userAttrib);
61: } else {
62: setValue(null);
63: }
64: } else {
65: setValue(null);
66: }
67: }
68: }
|