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