View Javadoc

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.springframework.security.userdetails.memory;
17  
18  import org.springframework.security.GrantedAuthority;
19  import org.springframework.security.GrantedAuthorityImpl;
20  
21  import java.util.ArrayList;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Vector;
25  
26  
27  /**
28   * Used by {@link InMemoryDaoImpl} to temporarily store the attributes associated with a user.
29   *
30   * @author Ben Alex
31   * @version $Id: UserAttribute.java 2217 2007-10-27 00:45:30Z luke_t $
32   */
33  public class UserAttribute {
34      //~ Instance fields ================================================================================================
35  
36      private List authorities = new Vector();
37      private String password;
38      private boolean enabled = true;
39  
40      //~ Constructors ===================================================================================================
41  
42      public UserAttribute() {
43          super();
44      }
45  
46      //~ Methods ========================================================================================================
47  
48      public void addAuthority(GrantedAuthority newAuthority) {
49          this.authorities.add(newAuthority);
50      }
51  
52      public GrantedAuthority[] getAuthorities() {
53          GrantedAuthority[] toReturn = {new GrantedAuthorityImpl("demo")};
54  
55          return (GrantedAuthority[]) this.authorities.toArray(toReturn);
56      }
57  
58      /**
59       * Set all authorities for this user.
60       *
61       * @param authorities {@link List} <{@link GrantedAuthority}>
62       * @since 1.1
63       */
64      public void setAuthorities(List authorities) {
65          this.authorities = authorities;
66      }
67  
68      /**
69       * Set all authorities for this user from String values.
70       * It will create the necessary {@link GrantedAuthority} objects.
71       *
72       * @param authoritiesAsString {@link List} <{@link String}>
73       * @since 1.1
74       */
75      public void setAuthoritiesAsString(List authoritiesAsString) {
76          setAuthorities(new ArrayList(authoritiesAsString.size()));
77          Iterator it = authoritiesAsString.iterator();
78          while (it.hasNext()) {
79              GrantedAuthority grantedAuthority = new GrantedAuthorityImpl((String) it.next());
80              addAuthority(grantedAuthority);
81          }
82      }
83  
84      public String getPassword() {
85          return password;
86      }
87  
88      public boolean isEnabled() {
89          return enabled;
90      }
91  
92      public boolean isValid() {
93          if ((this.password != null) && (authorities.size() > 0)) {
94              return true;
95          } else {
96              return false;
97          }
98      }
99  
100     public void setEnabled(boolean enabled) {
101         this.enabled = enabled;
102     }
103 
104     public void setPassword(String password) {
105         this.password = password;
106     }
107 }