001 /*
002 Licensed to the Apache Software Foundation (ASF) under one
003 or more contributor license agreements. See the NOTICE file
004 distributed with this work for additional information
005 regarding copyright ownership. The ASF licenses this file
006 to you under the Apache License, Version 2.0 (the
007 "License"); you may not use this file except in compliance
008 with the License. You may obtain a copy of the License at
009
010 http://www.apache.org/licenses/LICENSE-2.0
011
012 Unless required by applicable law or agreed to in writing,
013 software distributed under the License is distributed on an
014 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 KIND, either express or implied. See the License for the
016 specific language governing permissions and limitations
017 under the License.
018 */
019 package org.apache.wiki.auth.user;
020
021 import java.io.Serializable;
022 import java.util.Date;
023 import java.util.HashMap;
024 import java.util.Map;
025
026 /**
027 * Default implementation for representing wiki user information, such as the
028 * login name, full name, wiki name, and e-mail address.
029 * @since 2.3
030 */
031
032 public final class DefaultUserProfile implements UserProfile
033 {
034 private static final long serialVersionUID = -5600466893735300647L;
035
036 private static final String EMPTY_STRING = "";
037
038 private static final String WHITESPACE = "\\s";
039
040 private Map<String,Serializable> m_attributes = new HashMap<String,Serializable>();
041
042 private Date m_created = null;
043
044 private String m_email = null;
045
046 private String m_fullname = null;
047
048 private Date m_lockExpiry = null;
049
050 private String m_loginName = null;
051
052 private Date m_modified = null;
053
054 private String m_password = null;
055
056 private String m_uid = null;
057
058 private String m_wikiname = null;
059
060 /**
061 * Private constructor to prevent direct instantiation.
062 */
063 private DefaultUserProfile() {}
064
065 /**
066 * Static factory method that creates a new DefaultUserProfile
067 * and sets a unique identifier (uid) for the supplied UserDatabase.
068 * @param db the UserDatabase for which the uid should be
069 * created
070 * @return the new profile
071 */
072 protected static UserProfile newProfile( UserDatabase db )
073 {
074 UserProfile profile = new DefaultUserProfile();
075 profile.setUid( AbstractUserDatabase.generateUid( db ) );
076 return profile;
077 }
078
079 /**
080 * {@inheritDoc}
081 */
082 public boolean equals( Object o )
083 {
084 if ( ( o != null ) && ( o instanceof UserProfile ) )
085 {
086 DefaultUserProfile u = (DefaultUserProfile) o;
087 return same( m_fullname, u.m_fullname ) && same( m_password, u.m_password )
088 && same( m_loginName, u.m_loginName ) && same( m_email, u.m_email ) && same( m_wikiname,
089 u.m_wikiname );
090 }
091
092 return false;
093 }
094
095 public int hashCode()
096 {
097 return (m_fullname != null ? m_fullname.hashCode() : 0) ^
098 (m_password != null ? m_password.hashCode() : 0) ^
099 (m_loginName != null ? m_loginName.hashCode() : 0) ^
100 (m_wikiname != null ? m_wikiname.hashCode() : 0) ^
101 (m_email != null ? m_email.hashCode() : 0);
102 }
103
104 /**
105 * Returns the creation date
106 * @return the creation date
107 * @see org.apache.wiki.auth.user.UserProfile#getCreated()
108 */
109 public Date getCreated()
110 {
111 return m_created;
112 }
113
114 /**
115 * Returns the user's e-mail address.
116 * @return the e-mail address
117 */
118 public String getEmail()
119 {
120 return m_email;
121 }
122
123 /**
124 * Returns the user's full name.
125 * @return the full name
126 */
127 public String getFullname()
128 {
129 return m_fullname;
130 }
131
132 /**
133 * Returns the last-modified date.
134 * @return the last-modified date
135 * @see org.apache.wiki.auth.user.UserProfile#getLastModified()
136 */
137 public Date getLastModified()
138 {
139 return m_modified;
140 }
141
142 /**
143 * Returns the user's login name.
144 * @return the login name
145 */
146 public String getLoginName()
147 {
148 return m_loginName;
149 }
150
151 /**
152 * Returns the user password for use with custom authentication. Note that
153 * the password field is not meaningful for container authentication; the
154 * user's private credentials are generally stored elsewhere. While it
155 * depends on the {@link UserDatabase}implementation, in most cases the
156 * value returned by this method will be a password hash, not the password
157 * itself.
158 * @return the password
159 */
160 public String getPassword()
161 {
162 return m_password;
163 }
164
165 /**
166 * Returns the user's wiki name.
167 * @return the wiki name.
168 */
169 public String getWikiName()
170 {
171 return m_wikiname;
172 }
173
174 /**
175 * Returns <code>true</code> if the user profile is
176 * new. This implementation checks whether
177 * {@link #getLastModified()} returns <code>null</code>
178 * to determine the status.
179 * @see org.apache.wiki.auth.user.UserProfile#isNew()
180 */
181 public boolean isNew()
182 {
183 return m_modified == null;
184 }
185
186 /**
187 * @param date the creation date
188 * @see org.apache.wiki.auth.user.UserProfile#setCreated(java.util.Date)
189 */
190 public void setCreated(Date date)
191 {
192 m_created = date;
193 }
194
195 /**
196 * Sets the user's e-mail address.
197 * @param email the e-mail address
198 */
199 public void setEmail( String email )
200 {
201 m_email = email;
202 }
203
204 /**
205 * Sets the user's full name. For example, "Janne Jalkanen."
206 * @param arg the full name
207 */
208 public void setFullname( String arg )
209 {
210 m_fullname = arg;
211
212 // Compute wiki name
213 if ( m_fullname != null )
214 {
215 m_wikiname = m_fullname.replaceAll(WHITESPACE, EMPTY_STRING);
216 }
217 }
218
219 /**
220 * Sets the last-modified date.
221 * @param date the last-modified date
222 * @see org.apache.wiki.auth.user.UserProfile#setLastModified(java.util.Date)
223 */
224 public void setLastModified( Date date )
225 {
226 m_modified = date;
227 }
228
229 /**
230 * Sets the name by which the user logs in. The login name is used as the
231 * username for custom authentication (see
232 * {@link org.apache.wiki.auth.AuthenticationManager#login(WikiSession,HttpServletRequest, String, String)}).
233 * The login name is typically a short name ("jannej"). In contrast, the
234 * wiki name is typically of type FirstnameLastName ("JanneJalkanen").
235 * @param name the login name
236 */
237 public void setLoginName( String name )
238 {
239 m_loginName = name;
240 }
241
242 /**
243 * Sets the user's password for use with custom authentication. It is
244 * <em>not</em> the responsibility of implementing classes to hash the
245 * password; that responsibility is borne by the UserDatabase implementation
246 * during save operations (see {@link UserDatabase#save(UserProfile)}).
247 * Note that the password field is not meaningful for container
248 * authentication; the user's private credentials are generally stored
249 * elsewhere.
250 * @param arg the password
251 */
252 public void setPassword( String arg )
253 {
254 m_password = arg;
255 }
256
257 /**
258 * No-op method. In previous versions of JSPWiki, the method
259 * set the user's wiki name directly. Now, the wiki name is automatically
260 * calculated based on the full name.
261 * @param name the wiki name
262 * @deprecated This method will be removed in a future release.
263 */
264 @SuppressWarnings("deprecation")
265 public void setWikiName( String name )
266 {
267 }
268
269 /**
270 * Returns a string representation of this user profile.
271 * @return the string
272 */
273 public String toString()
274 {
275 return "[DefaultUserProfile: '" + getFullname() + "']";
276 }
277
278 /**
279 * Private method that compares two objects and determines whether they are
280 * equal. Two nulls are considered equal.
281 * @param arg1 the first object
282 * @param arg2 the second object
283 * @return the result of the comparison
284 */
285 private boolean same( Object arg1, Object arg2 )
286 {
287 if ( arg1 == null && arg2 == null )
288 {
289 return true;
290 }
291 if ( arg1 == null || arg2 == null )
292 {
293 return false;
294 }
295 return arg1.equals( arg2 );
296 }
297
298 //--------------------------- Attribute and lock interface implementations ---------------------------
299
300 /**
301 * {@inheritDoc}
302 */
303 public Map<String,Serializable> getAttributes()
304 {
305 return m_attributes;
306 }
307
308 /**
309 * {@inheritDoc}
310 */
311 public Date getLockExpiry()
312 {
313 return isLocked() ? m_lockExpiry : null;
314 }
315
316 /**
317 * {@inheritDoc}
318 */
319 public String getUid()
320 {
321 return m_uid;
322 }
323
324 /**
325 * {@inheritDoc}
326 */
327 public boolean isLocked()
328 {
329 boolean locked = m_lockExpiry != null && System.currentTimeMillis() < m_lockExpiry.getTime();
330
331 // Clear the lock if it's expired already
332 if ( !locked && m_lockExpiry != null )
333 {
334 m_lockExpiry = null;
335 }
336 return locked;
337 }
338
339 /**
340 * {@inheritDoc}
341 */
342 public void setLockExpiry( Date expiry )
343 {
344 m_lockExpiry = expiry;
345 }
346
347 /**
348 * {@inheritDoc}
349 */
350 public void setUid( String uid )
351 {
352 m_uid = uid;
353 }
354 }