KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > security > DefaultUser


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.security;
21
22 import java.io.Serializable JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Date JavaDoc;
25
26 import com.sslexplorer.realms.Realm;
27
28 /**
29  * Implementation of a {@link com.sslexplorer.security.User}
30  * for <i>Default users</i>.
31  * @author James D Robinson <a HREF="mailto:james@3sp.com">&lt;james@3sp.com&gt;</a>
32  */

33 public class DefaultUser implements User, Serializable JavaDoc {
34     protected String JavaDoc principalName;
35     protected String JavaDoc email;
36     protected String JavaDoc fullname;
37     protected Date JavaDoc lastPasswordChange;
38     protected Role[] roles;
39     private final Realm realm;
40
41     /**
42      * @param principalName
43      * @param email
44      * @param fullname
45      * @param lastPasswordChange
46      * @param realm
47      */

48     public DefaultUser(String JavaDoc principalName, String JavaDoc email, String JavaDoc fullname, Date JavaDoc lastPasswordChange, Realm realm) {
49         this.principalName = principalName;
50         this.email = email;
51         this.fullname = fullname;
52         this.lastPasswordChange = lastPasswordChange;
53         this.realm = realm;
54     }
55
56     /*
57      * (non-Javadoc)
58      * @see com.sslexplorer.security.User#getLastPasswordChange()
59      */

60     public Date JavaDoc getLastPasswordChange() {
61         return lastPasswordChange;
62     }
63
64     public boolean requiresPasswordChange() {
65         return lastPasswordChange == null;
66     }
67
68     /*
69      * (non-Javadoc)
70      * @see com.sslexplorer.permissions.Principal#getPrincipalName()
71      */

72     public String JavaDoc getPrincipalName() {
73         return principalName;
74     }
75
76     /*
77      * (non-Javadoc)
78      * @see com.sslexplorer.security.User#getEmail()
79      */

80     public String JavaDoc getEmail() {
81         return email;
82     }
83
84     /*
85      * (non-Javadoc)
86      * @see com.sslexplorer.security.User#getFullname()
87      */

88     public String JavaDoc getFullname() {
89         return fullname;
90     }
91
92     /*
93      * (non-Javadoc)
94      * @see com.sslexplorer.security.User#getHomeNetworkPlaceUri()
95      */

96     public String JavaDoc getHomeNetworkPlaceUri() {
97         return null;
98     }
99
100     /**
101      * Get if the user is part of the specified role.
102      *
103      * @param rolename role
104      * @return <code>true<code> if the user is part of the specified role.
105      */

106     public boolean containsRole(String JavaDoc rolename) {
107         if (roles == null) {
108             return false;
109         }
110
111         for (int i = 0; i < roles.length; i++) {
112             if (roles[i].getPrincipalName().equals(rolename))
113                 return true;
114         }
115
116         return false;
117     }
118     
119     /*
120      * (non-Javadoc)
121      * @see com.sslexplorer.security.User#getRoles()
122      */

123     public Role[] getRoles() {
124         return roles;
125     }
126
127     /**
128      * Set the roles
129      * @param roles roles
130      */

131     public void setRoles(Role[] roles) {
132         this.roles = roles;
133     }
134     
135     /**
136      * Set the principal name
137      * @param principalName
138      */

139     public void setPrincipalName(String JavaDoc principalName) {
140         this.principalName = principalName;
141     }
142
143     /**
144      * Set the email address
145      * @param email email address
146      */

147     public void setEmail(String JavaDoc email) {
148         this.email = email;
149     }
150
151     /**
152      * Set the full name
153      * @param fullname full name
154      */

155     public void setFullname(String JavaDoc fullname) {
156         this.fullname = fullname;
157         
158     }
159     
160     /**
161      * Set the last password change
162      * @param lastPasswordChange
163      */

164     public void setLastPasswordChange(Date JavaDoc lastPasswordChange) {
165         this.lastPasswordChange = lastPasswordChange;
166     }
167     
168     /* (non-Javadoc)
169      * @see java.lang.Object#equals(java.lang.Object)
170      */

171     public boolean equals(Object JavaDoc o) {
172         return o instanceof User && ((User)o).getPrincipalName().equals(getPrincipalName())
173             && ((User)o).getRealm().equals(getRealm());
174     }
175
176     /* (non-Javadoc)
177      * @see java.lang.Object#hashCode()
178      */

179     @Override JavaDoc
180     public int hashCode() {
181         return ( getRealm().getResourceId() + "_" + getPrincipalName() ).hashCode();
182     }
183
184     /* (non-Javadoc)
185      * @see java.lang.Comparable#compareTo(java.lang.Object)
186      */

187     public int compareTo(Object JavaDoc o) {
188         return o instanceof User ? (getPrincipalName().compareTo(((User)o).getPrincipalName())) : 1;
189     }
190
191     /* (non-Javadoc)
192      * @see com.sslexplorer.policyframework.Principal#getRealm()
193      */

194     public Realm getRealm() {
195         return this.realm;
196     }
197
198     @Override JavaDoc
199     public String JavaDoc toString() {
200         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(super.toString());
201         buffer.append("[");
202         buffer.append("principalName='").append(principalName).append("' ");
203         buffer.append("email='").append(email).append("' ");
204         buffer.append("fullname='").append(fullname).append("' ");
205         buffer.append("lastPasswordChange='").append(lastPasswordChange).append("' ");
206         buffer.append("roles='").append(roles == null ? "" : Arrays.asList(roles)).append("' ");
207         buffer.append("realm='").append(realm).append("'");
208         buffer.append("']");
209         return buffer.toString();
210     }
211 }
Popular Tags