KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > auth > User


1 /**
2  * Copyright 2004-2005 jManage.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.jmanage.core.auth;
17
18 import org.jmanage.core.crypto.Crypto;
19
20 import java.util.List JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.security.Principal JavaDoc;
23
24 /**
25  * Date : Jun 27, 2004 11:52:43 PM
26  * @author Shashank
27  */

28 public class User implements Principal JavaDoc, java.io.Serializable JavaDoc{
29
30     public final static String JavaDoc STATUS_ACTIVE = "A";
31     public final static String JavaDoc STATUS_LOCKED = "L";
32     //public final static String STATUS_INACTIVE = "I";
33

34     private String JavaDoc username;
35     private String JavaDoc plaintextPassword;
36     private String JavaDoc password; // hashed password
37
private List JavaDoc roles;
38     private String JavaDoc status;
39     private int lockCount;
40
41     private boolean externalUser = false;
42
43
44     /**
45      * Default,
46      */

47     public User(){}
48
49     /**
50      * Create users with specified permissions (represented by roles).
51      *
52      * @param username
53      * @param password
54      * @param roles
55      */

56     public User(String JavaDoc username, String JavaDoc password, List JavaDoc roles, String JavaDoc status,
57                 int lockCount) {
58         this.username = username;
59         this.password = password;
60         this.roles = roles;
61         this.status = status;
62         this.lockCount = lockCount;
63     }
64
65     public String JavaDoc getUsername() {
66         return username;
67     }
68
69     public void setUsername(String JavaDoc username) {
70         this.username = username;
71     }
72
73     /**
74      *
75      * @return password hash
76      */

77     public String JavaDoc getPassword() {
78         if(password == null){
79             assert plaintextPassword != null;
80             password = Crypto.hash(plaintextPassword);
81         }
82         return password;
83     }
84
85     /**
86      * This is used by the CLI to set the plaintext password. Note that this
87      * is used as we don't want to use the hash function in CLI. Password
88      * is hashed later on when getPassword() is called.
89      *
90      * @param plaintext
91      */

92     public void setPlaintextPassword(String JavaDoc plaintext){
93         this.plaintextPassword = plaintext;
94     }
95
96     public void setPassword(String JavaDoc password) {
97         this.password = password;
98     }
99
100     public List JavaDoc getRoles() {
101         return roles;
102     }
103
104     public String JavaDoc getRolesAsString(){
105         StringBuffer JavaDoc strRoles = new StringBuffer JavaDoc();
106         for(Iterator JavaDoc it=roles.iterator(); it.hasNext();){
107             Role role = (Role)it.next();
108             strRoles.append(role.getName());
109             if(it.hasNext())
110                 strRoles.append(", ");
111         }
112         return strRoles.toString();
113     }
114
115     public void setRoles(List JavaDoc roles) {
116         this.roles = roles;
117     }
118
119     public String JavaDoc getStatus() {
120         return status;
121     }
122
123     public void setStatus(String JavaDoc status) {
124         this.status = status;
125     }
126
127     public int getLockCount() {
128         return lockCount;
129     }
130
131     public void setLockCount(int lockCount) {
132         this.lockCount = lockCount;
133     }
134
135     public String JavaDoc getName() {
136         return getUsername();
137     }
138
139     public boolean isExternalUser() {
140         return externalUser;
141     }
142
143     public void setExternalUser(boolean externalUser) {
144         this.externalUser = externalUser;
145     }
146
147     public boolean hasRole(String JavaDoc role) {
148         for(Iterator JavaDoc it=getRoles().iterator(); it.hasNext(); ){
149             Role roleObj = (Role)it.next();
150             if(role.equals(roleObj.getName())){
151                 return true;
152             }
153         }
154         return false;
155     }
156
157     public boolean equals(Object JavaDoc o){
158         if(o == null)
159             return false;
160
161         if(this == o)
162             return true;
163
164         if(!(o instanceof Principal JavaDoc))
165             return false;
166         Principal JavaDoc that = (Principal JavaDoc)o;
167
168         if(this.getName().equals(that.getName()))
169             return true;
170         return false;
171     }
172
173     public String JavaDoc toString(){
174         return username;
175     }
176 }
Popular Tags