1 2 package org.roller.pojos; 3 4 import java.util.Date ; 5 import java.util.Iterator ; 6 import java.util.Set ; 7 import java.util.TreeSet ; 8 9 import org.roller.RollerException; 10 import org.roller.config.RollerConfig; 11 import org.roller.model.Roller; 12 import org.roller.model.RollerFactory; 13 import org.roller.model.UserManager; 14 import org.roller.util.Utilities; 15 16 17 26 public class UserData 27 extends org.roller.pojos.PersistentObject 28 implements java.io.Serializable 29 { 30 public static final UserData SYSTEM_USER = 31 new UserData("n/a","systemuser","n/a","systemuser","n/a",new Date ()); 32 33 public static final UserData ANONYMOUS_USER = 34 new UserData("n/a","anonymoususer","n/a","anonymoususer","n/a",new Date ()); 35 36 static final long serialVersionUID = -6354583200913127874L; 37 38 protected java.lang.String id; 39 protected java.lang.String userName; 40 protected java.lang.String password; 41 protected java.lang.String fullName; 42 protected java.lang.String emailAddress; 43 protected java.util.Date dateCreated; 44 private Set roles = new TreeSet (); 45 46 public UserData() 47 { 48 } 49 50 public UserData( java.lang.String id, java.lang.String userName, 51 java.lang.String password, java.lang.String fullName, 52 java.lang.String emailAddress, java.util.Date dateCreated) 53 { 54 this.id = id; 55 this.userName = userName; 56 this.password = password; 57 this.fullName = fullName; 58 this.emailAddress = emailAddress; 59 this.dateCreated = (Date )dateCreated.clone(); 60 } 61 62 public UserData( UserData otherData ) 63 { 64 this.id = otherData.id; 65 this.userName = otherData.userName; 66 this.password = otherData.password; 67 this.fullName = otherData.fullName; 68 this.emailAddress = otherData.emailAddress; 69 this.dateCreated = (Date )otherData.dateCreated.clone(); 70 71 } 72 73 81 public java.lang.String getId() 82 { 83 return this.id; 84 } 85 86 public void setId( java.lang.String id ) 87 { 88 this.id = id; 89 } 90 91 95 public java.lang.String getUserName() 96 { 97 return this.userName; 98 } 99 100 public void setUserName( java.lang.String userName ) 101 { 102 this.userName = userName; 103 } 104 105 111 public java.lang.String getPassword() 112 { 113 return this.password; 114 } 115 120 public void setPassword( java.lang.String password ) 121 { 122 this.password = password; 123 } 124 125 129 public java.lang.String getFullName() 130 { 131 return this.fullName; 132 } 133 134 public void setFullName( java.lang.String fullName ) 135 { 136 this.fullName = fullName; 137 } 138 139 143 public java.lang.String getEmailAddress() 144 { 145 return this.emailAddress; 146 } 147 148 public void setEmailAddress( java.lang.String emailAddress ) 149 { 150 this.emailAddress = emailAddress; 151 } 152 153 157 public java.util.Date getDateCreated() 158 { 159 if (dateCreated == null) 160 { 161 return null; 162 } 163 else 164 { 165 return (Date )dateCreated.clone(); 166 } 167 } 168 169 public void setDateCreated(final java.util.Date date) 170 { 171 if (date != null) 172 { 173 dateCreated = (Date )date.clone(); 174 } 175 else 176 { 177 dateCreated = null; 178 } 179 } 180 181 public String toString() 182 { 183 StringBuffer str = new StringBuffer ("{"); 184 185 str.append("id=" + id + " "); 186 str.append("userName=" + userName + " "); 187 str.append("password=" + password + " "); 188 str.append("fullName=" + fullName + " "); 189 str.append("emailAddress=" + emailAddress + " "); 190 str.append("dateCreated=" + dateCreated + " "); 191 str.append('}'); 192 193 return(str.toString()); 194 } 195 196 public boolean equals( Object pOther ) 197 { 198 if( pOther instanceof UserData ) 199 { 200 UserData lTest = (UserData) pOther; 201 boolean lEquals = true; 202 203 if( this.id == null ) 204 { 205 lEquals = lEquals && ( lTest.id == null ); 206 } 207 else 208 { 209 lEquals = lEquals && this.id.equals( lTest.id ); 210 } 211 if( this.userName == null ) 212 { 213 lEquals = lEquals && ( lTest.userName == null ); 214 } 215 else 216 { 217 lEquals = lEquals && this.userName.equals( lTest.userName ); 218 } 219 if( this.password == null ) 220 { 221 lEquals = lEquals && ( lTest.password == null ); 222 } 223 else 224 { 225 lEquals = lEquals && this.password.equals( lTest.password ); 226 } 227 if( this.fullName == null ) 228 { 229 lEquals = lEquals && ( lTest.fullName == null ); 230 } 231 else 232 { 233 lEquals = lEquals && this.fullName.equals( lTest.fullName ); 234 } 235 if( this.emailAddress == null ) 236 { 237 lEquals = lEquals && ( lTest.emailAddress == null ); 238 } 239 else 240 { 241 lEquals = lEquals && this.emailAddress.equals( lTest.emailAddress ); 242 } 243 244 if( this.dateCreated == null ) 245 { 246 lEquals = lEquals && ( lTest.dateCreated == null ); 247 } 248 else 249 { 250 lEquals = lEquals && datesEquivalent(this.dateCreated, lTest.dateCreated); 251 } 252 253 return lEquals; 254 } 255 else 256 { 257 return false; 258 } 259 } 260 261 private boolean datesEquivalent(Date d1, Date d2) 262 { 263 boolean equiv = true; 264 equiv = equiv && d1.getHours() == d1.getHours(); 265 equiv = equiv && d1.getMinutes() == d1.getMinutes(); 266 equiv = equiv && d1.getSeconds() == d1.getSeconds(); 267 equiv = equiv && d1.getMonth() == d1.getMonth(); 268 equiv = equiv && d1.getDay() == d1.getDay(); 269 equiv = equiv && d1.getYear() == d1.getYear(); 270 return equiv; 271 } 272 273 public int hashCode() 274 { 275 int result = 17; 276 result = 37*result + ((this.id != null) ? this.id.hashCode() : 0); 277 result = 37*result + ((this.userName != null) ? this.userName.hashCode() : 0); 278 result = 37*result + ((this.password != null) ? this.password.hashCode() : 0); 279 result = 37*result + ((this.fullName != null) ? this.fullName.hashCode() : 0); 280 result = 37*result + ((this.emailAddress != null) ? this.emailAddress.hashCode() : 0); 281 result = 37*result + ((this.dateCreated != null) ? this.dateCreated.hashCode() : 0); 282 return result; 283 } 284 285 288 public void setData( org.roller.pojos.PersistentObject otherData ) 289 { 290 this.id = ((UserData)otherData).id; 291 this.userName = ((UserData)otherData).userName; 292 this.password = ((UserData)otherData).password; 293 this.fullName = ((UserData)otherData).fullName; 294 this.emailAddress = ((UserData)otherData).emailAddress; 295 this.dateCreated = ((UserData)otherData).dateCreated; 296 } 297 298 302 public void remove() throws RollerException 303 { 304 UserManager uMgr = RollerFactory.getRoller().getUserManager(); 305 uMgr.removeUserWebsites(this); 306 307 Iterator roles = uMgr.getUserRoles(this).iterator(); 309 while (roles.hasNext()) 310 { 311 ((RoleData)roles.next()).remove(); 312 } 313 314 super.remove(); 315 } 316 317 324 public void resetPassword(Roller roller, String new1, String new2) throws RollerException 325 { 326 if (!new1.equals(new2)) 327 { 328 throw new RollerException("newUser.error.mismatchedPasswords"); 329 } 330 331 String encrypt = RollerConfig.getProperty("passwds.encryption.enabled"); 332 String algorithm = RollerConfig.getProperty("passwds.encryption.algorithm"); 333 if (new Boolean (encrypt).booleanValue()) 334 { 335 password = Utilities.encodePassword(new1, algorithm); 336 } 337 else 338 { 339 password = new1; 340 } 341 } 342 343 348 public Set getRoles() 349 { 350 return roles; 351 } 352 353 public void setRoles(Set roles) 354 { 355 this.roles = roles; 356 } 357 358 359 364 public boolean hasRole(String roleName) 365 { 366 Iterator iter = roles.iterator(); 367 while (iter.hasNext()) 368 { 369 RoleData role = (RoleData) iter.next(); 370 if (role.getRole().equals(roleName)) 371 { 372 return true; 373 } 374 } 375 return false; 376 } 377 378 382 public void revokeRole(String roleName) throws RollerException 383 { 384 RoleData removeme = null; 385 Iterator iter = roles.iterator(); 386 while (iter.hasNext()) 387 { 388 RoleData role = (RoleData) iter.next(); 389 if (role.getRole().equals(roleName)) 390 { 391 removeme = role; 392 } 393 } 394 if (removeme != null) 395 { 396 roles.remove(removeme); 397 RollerFactory.getRoller().getUserManager().removeRole(removeme.getId()); 398 } 399 } 400 401 405 public void grantRole(String roleName) throws RollerException 406 { 407 if (!hasRole(roleName)) 408 { 409 RoleData role = new RoleData(null, this, roleName); 410 RollerFactory.getRoller().getUserManager().storeRole(role); 411 roles.add(role); 412 } 413 } 414 } 415 | Popular Tags |