001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.pojos;
020:
021: import java.io.Serializable;
022: import java.util.ArrayList;
023: import java.util.Date;
024: import java.util.HashSet;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Set;
028:
029: import org.apache.roller.RollerException;
030: import org.apache.roller.config.RollerConfig;
031: import org.apache.roller.business.Roller;
032: import org.apache.roller.util.PojoUtil;
033: import org.apache.roller.util.Utilities;
034:
035: /**
036: * User bean.
037: * @author David M Johnson
038: *
039: * @ejb:bean name="UserData"
040: * @struts.form include-all="true"
041: * @hibernate.class lazy="false" table="rolleruser"
042: * @hibernate.cache usage="read-write"
043: */
044: public class UserData extends org.apache.roller.pojos.PersistentObject
045: implements Serializable {
046: public static final UserData SYSTEM_USER = new UserData("n/a",
047: "systemuser", "n/a", "systemuser", "n/a", "en_US_WIN",
048: "America/Los_Angeles", new Date(), Boolean.TRUE);
049:
050: public static final UserData ANONYMOUS_USER = new UserData("n/a",
051: "anonymoususer", "n/a", "anonymoususer", "n/a",
052: "en_US_WIN", "America/Los_Angeles", new Date(),
053: Boolean.TRUE);
054:
055: static final long serialVersionUID = -6354583200913127874L;
056:
057: private String id;
058: private String userName;
059: private String password;
060: private String fullName;
061: private String emailAddress;
062: private Date dateCreated;
063: private String locale;
064: private String timeZone;
065: private Boolean enabled = Boolean.TRUE;
066:
067: private Set roles = new HashSet();
068: private List permissions = new ArrayList();
069:
070: public UserData() {
071: }
072:
073: public UserData(String id, String userName, String password,
074: String fullName, String emailAddress, String locale,
075: String timeZone, Date dateCreated, Boolean isEnabled) {
076: this .id = id;
077: this .userName = userName;
078: this .password = password;
079: this .fullName = fullName;
080: this .emailAddress = emailAddress;
081: this .dateCreated = (Date) dateCreated.clone();
082: this .locale = locale;
083: this .timeZone = timeZone;
084: this .enabled = enabled;
085: }
086:
087: public UserData(UserData otherData) {
088: setData(otherData);
089: }
090:
091: /**
092: * @hibernate.bag lazy="true" inverse="true" cascade="delete"
093: * @hibernate.collection-key column="user_id"
094: * @hibernate.collection-one-to-many
095: * class="org.apache.roller.pojos.PermissionsData"
096: */
097: public List getPermissions() {
098: return permissions;
099: }
100:
101: public void setPermissions(List perms) {
102: permissions = perms;
103: }
104:
105: /**
106: * @ejb:persistent-field
107: * @hibernate.property column="isenabled" non-null="true" unique="false"
108: */
109: public Boolean getEnabled() {
110: return this .enabled;
111: }
112:
113: /** @ejb:persistent-field */
114: public void setEnabled(Boolean enabled) {
115: this .enabled = enabled;
116: }
117:
118: /** Id of the User.
119: * Not remote since primary key may be extracted by other means.
120: *
121: * @struts.validator type="required" msgkey="errors.required"
122: * @ejb:persistent-field
123: * @hibernate.id column="id"
124: * generator-class="uuid.hex" unsaved-value="null"
125: */
126: public String getId() {
127: return this .id;
128: }
129:
130: /** @ejb:persistent-field */
131: public void setId(String id) {
132: this .id = id;
133: }
134:
135: /** User name of the user.
136: * @ejb:persistent-field
137: * @hibernate.property column="username" non-null="true" unique="true"
138: * @roller.wrapPojoMethod type="simple"
139: */
140: public String getUserName() {
141: return this .userName;
142: }
143:
144: /** @ejb:persistent-field */
145: public void setUserName(String userName) {
146: this .userName = userName;
147: }
148:
149: /**
150: * Get password.
151: * If password encryption is enabled, will return encrypted password.
152: *
153: * @ejb:persistent-field
154: * @hibernate.property column="passphrase" non-null="true"
155: */
156: public String getPassword() {
157: return this .password;
158: }
159:
160: /**
161: * Set password.
162: * If password encryption is turned on, then pass in an encrypted password.
163: * @ejb:persistent-field
164: */
165: public void setPassword(String password) {
166: this .password = password;
167: }
168:
169: /**
170: * Full name of the user.
171: *
172: * @roller.wrapPojoMethod type="simple"
173: * @ejb:persistent-field
174: * @hibernate.property column="fullname" non-null="true" unique="true"
175: */
176: public String getFullName() {
177: return this .fullName;
178: }
179:
180: /** @ejb:persistent-field */
181: public void setFullName(String fullName) {
182: this .fullName = fullName;
183: }
184:
185: /**
186: * E-mail address of the user.
187: *
188: * @roller.wrapPojoMethod type="simple"
189: * @ejb:persistent-field
190: * @hibernate.property column="emailaddress" non-null="true" unique="true"
191: */
192: public String getEmailAddress() {
193: return this .emailAddress;
194: }
195:
196: /** @ejb:persistent-field */
197: public void setEmailAddress(String emailAddress) {
198: this .emailAddress = emailAddress;
199: }
200:
201: /**
202: * @roller.wrapPojoMethod type="simple"
203: * @ejb:persistent-field
204: * @hibernate.property column="datecreated" non-null="true" unique="false"
205: */
206: public Date getDateCreated() {
207: if (dateCreated == null) {
208: return null;
209: } else {
210: return (Date) dateCreated.clone();
211: }
212: }
213:
214: /** @ejb:persistent-field */
215: public void setDateCreated(final Date date) {
216: if (date != null) {
217: dateCreated = (Date) date.clone();
218: } else {
219: dateCreated = null;
220: }
221: }
222:
223: /**
224: * Locale of the user.
225: * @ejb:persistent-field
226: * @hibernate.property column="locale" non-null="true" unique="false"
227: * @roller.wrapPojoMethod type="simple"
228: */
229: public String getLocale() {
230: return this .locale;
231: }
232:
233: /** @ejb:persistent-field */
234: public void setLocale(String locale) {
235: this .locale = locale;
236: }
237:
238: /**
239: * Timezone of the user.
240: * @ejb:persistent-field
241: * @hibernate.property column="timeZone" non-null="true" unique="false"
242: * @roller.wrapPojoMethod type="simple"
243: */
244: public String getTimeZone() {
245: return this .timeZone;
246: }
247:
248: /** @ejb:persistent-field */
249: public void setTimeZone(String timeZone) {
250: this .timeZone = timeZone;
251: }
252:
253: //------------------------------------------------------------------- citizenship
254: public String toString() {
255: StringBuffer str = new StringBuffer("{");
256:
257: str.append("id=" + id + " ");
258: str.append("userName=" + userName + " ");
259: str.append("password=" + password + " ");
260: str.append("fullName=" + fullName + " ");
261: str.append("emailAddress=" + emailAddress + " ");
262: str.append("dateCreated=" + dateCreated + " ");
263: str.append('}');
264:
265: return (str.toString());
266: }
267:
268: public boolean equals(Object pOther) {
269: if (pOther instanceof UserData) {
270: UserData lTest = (UserData) pOther;
271: boolean lEquals = true;
272: lEquals = PojoUtil.equals(lEquals, this .getId(), lTest
273: .getId());
274: lEquals = PojoUtil.equals(lEquals, this .getUserName(),
275: lTest.getUserName());
276: lEquals = PojoUtil.equals(lEquals, this .getPassword(),
277: lTest.getPassword());
278: lEquals = PojoUtil.equals(lEquals, this .getFullName(),
279: lTest.getFullName());
280: lEquals = PojoUtil.equals(lEquals, this .getEmailAddress(),
281: lTest.getEmailAddress());
282: return lEquals;
283: } else {
284: return false;
285: }
286: }
287:
288: /*public boolean equals( Object pOther )
289: {
290: if( pOther instanceof UserData )
291: {
292: UserData lTest = (UserData) pOther;
293: boolean lEquals = true;
294:
295: if( this.id == null )
296: {
297: lEquals = lEquals && ( lTest.id == null );
298: }
299: else
300: {
301: lEquals = lEquals && this.id.equals( lTest.id );
302: }
303: if( this.userName == null )
304: {
305: lEquals = lEquals && ( lTest.userName == null );
306: }
307: else
308: {
309: lEquals = lEquals && this.userName.equals( lTest.userName );
310: }
311: if( this.password == null )
312: {
313: lEquals = lEquals && ( lTest.password == null );
314: }
315: else
316: {
317: lEquals = lEquals && this.password.equals( lTest.password );
318: }
319: if( this.fullName == null )
320: {
321: lEquals = lEquals && ( lTest.fullName == null );
322: }
323: else
324: {
325: lEquals = lEquals && this.fullName.equals( lTest.fullName );
326: }
327: if( this.emailAddress == null )
328: {
329: lEquals = lEquals && ( lTest.emailAddress == null );
330: }
331: else
332: {
333: lEquals = lEquals && this.emailAddress.equals( lTest.emailAddress );
334: }
335:
336: if( this.dateCreated == null )
337: {
338: lEquals = lEquals && ( lTest.dateCreated == null );
339: }
340: else
341: {
342: lEquals = lEquals && datesEquivalent(this.dateCreated, lTest.dateCreated);
343: }
344:
345: return lEquals;
346: }
347: else
348: {
349: return false;
350: }
351: }*/
352:
353: private boolean datesEquivalent(Date d1, Date d2) {
354: boolean equiv = true;
355: equiv = equiv && d1.getHours() == d1.getHours();
356: equiv = equiv && d1.getMinutes() == d1.getMinutes();
357: equiv = equiv && d1.getSeconds() == d1.getSeconds();
358: equiv = equiv && d1.getMonth() == d1.getMonth();
359: equiv = equiv && d1.getDay() == d1.getDay();
360: equiv = equiv && d1.getYear() == d1.getYear();
361: return equiv;
362: }
363:
364: public int hashCode() {
365: int result = 17;
366: result = 37 * result
367: + ((this .id != null) ? this .id.hashCode() : 0);
368: result = 37
369: * result
370: + ((this .userName != null) ? this .userName.hashCode()
371: : 0);
372: result = 37
373: * result
374: + ((this .password != null) ? this .password.hashCode()
375: : 0);
376: result = 37
377: * result
378: + ((this .fullName != null) ? this .fullName.hashCode()
379: : 0);
380: result = 37
381: * result
382: + ((this .emailAddress != null) ? this .emailAddress
383: .hashCode() : 0);
384: result = 37
385: * result
386: + ((this .dateCreated != null) ? this .dateCreated
387: .hashCode() : 0);
388: return result;
389: }
390:
391: /**
392: * Setter is needed in RollerImpl.storePersistentObject()
393: */
394: public void setData(
395: org.apache.roller.pojos.PersistentObject otherData) {
396: UserData other = (UserData) otherData;
397: this .id = other.getId();
398: this .userName = other.getUserName();
399: this .password = other.getPassword();
400: this .fullName = other.getFullName();
401: this .emailAddress = other.getEmailAddress();
402: this .locale = other.getLocale();
403: this .timeZone = other.getTimeZone();
404: this .dateCreated = other.getDateCreated() != null ? (Date) other
405: .getDateCreated().clone()
406: : null;
407: }
408:
409: /**
410: * Reset this user's password.
411: * @param roller Roller instance to use for configuration information
412: * @param new1 New password
413: * @param new2 Confirm this matches new password
414: * @author Dave Johnson
415: */
416: public void resetPassword(Roller roller, String new1, String new2)
417: throws RollerException {
418: if (!new1.equals(new2)) {
419: throw new RollerException(
420: "newUser.error.mismatchedPasswords");
421: }
422:
423: String encrypt = RollerConfig
424: .getProperty("passwds.encryption.enabled");
425: String algorithm = RollerConfig
426: .getProperty("passwds.encryption.algorithm");
427: if (new Boolean(encrypt).booleanValue()) {
428: password = Utilities.encodePassword(new1, algorithm);
429: } else {
430: password = new1;
431: }
432: }
433:
434: /**
435: * @hibernate.set lazy="false" inverse="true" cascade="all-delete-orphan"
436: * @hibernate.collection-key column="userid"
437: * @hibernate.collection-one-to-many class="org.apache.roller.pojos.RoleData"
438: */
439: public Set getRoles() {
440: return roles;
441: }
442:
443: /**
444: * this is private to force the use of grant/revokeRole() methods.
445: */
446: private void setRoles(Set roles) {
447: this .roles = roles;
448: }
449:
450: /**
451: * Returns true if user has role specified.
452: */
453: public boolean hasRole(String roleName) {
454: Iterator iter = roles.iterator();
455: while (iter.hasNext()) {
456: RoleData role = (RoleData) iter.next();
457: if (role.getRole().equals(roleName)) {
458: return true;
459: }
460: }
461: return false;
462: }
463:
464: /**
465: * Revokes specified role from user.
466: */
467: public void revokeRole(String roleName) throws RollerException {
468: RoleData removeme = null;
469: Iterator iter = roles.iterator();
470: while (iter.hasNext()) {
471: RoleData role = (RoleData) iter.next();
472: if (role.getRole().equals(roleName)) {
473: removeme = role;
474: }
475: }
476:
477: /*
478: * NOTE: we do this outside the loop above because we are not allowed
479: * to modify the contents of the Set while we are iterating over it.
480: * doing so causes a ConcurrentModificationException
481: */
482: if (removeme != null) {
483: roles.remove(removeme);
484: }
485: }
486:
487: /**
488: * Grant to user role specified by role name.
489: */
490: public void grantRole(String roleName) throws RollerException {
491: if (!hasRole(roleName)) {
492: RoleData role = new RoleData(null, this, roleName);
493: roles.add(role);
494: }
495: }
496:
497: }
|