1 30 31 34 package com.nightlabs.ipanema.security; 35 36 import java.io.Serializable ; 37 import java.util.Collection ; 38 import java.util.Date ; 39 import java.util.HashMap ; 40 import java.util.Iterator ; 41 import java.util.Map ; 42 43 import javax.jdo.PersistenceManager; 44 import javax.jdo.spi.PersistenceCapable; 45 46 import org.apache.log4j.Logger; 47 48 51 52 60 public class UserRef implements Serializable { 62 public static Logger LOGGER = Logger.getLogger(UserRef.class); 63 64 65 69 private String authorityID; 70 71 75 private String organisationID; 76 77 81 private String userID; 82 83 84 85 90 private Authority authority; 91 92 97 private User user; 98 99 100 101 110 private boolean visible; 111 112 115 private int userGroupReferenceCount = 0; 116 117 133 private Map roleGroupRefs = new HashMap (); 134 135 136 152 private Map roleRefs = new HashMap (); 153 154 private Date changeDT; 155 156 public UserRef() { } 157 158 public UserRef(Authority _authority, User _user, boolean _visible) 159 { 160 this.authorityID = _authority.getAuthorityID(); 161 this.organisationID = _user.getOrganisationID(); 162 this.userID = _user.getUserID(); 163 this.authority = _authority; 164 this.user = _user; 165 this.visible = _visible; 166 this.changeDT = new Date (); 167 } 168 169 172 public String getAuthorityID() { 173 return authorityID; 174 } 175 178 public String getOrganisationID() { 179 return organisationID; 180 } 181 184 public String getUserID() { 185 return userID; 186 } 187 188 191 public Authority getAuthority() { 192 return authority; 193 } 194 197 public User getUser() { 198 return user; 199 } 200 201 204 public boolean isVisible() { 205 return visible; 206 } 207 210 public void setVisible(boolean visible) { 211 this.visible = visible; 212 } 213 214 217 public int getUserGroupReferenceCount() { 218 return userGroupReferenceCount; 219 } 220 221 224 protected int incrementUserGroupReferenceCount(int inc) { 225 return (userGroupReferenceCount += inc); 226 } 227 228 231 protected int decrementUserGroupReferenceCount(int dec) { 232 return (userGroupReferenceCount -= dec); 233 } 234 235 238 public Date getChangeDT() { 239 return changeDT; 240 } 241 244 public void setChangeDT(Date changeDT) { 245 this.changeDT = changeDT; 246 } 247 248 protected transient String login = null; 249 255 public String getLogin() 256 { 257 if (login == null) { 258 StringBuffer sb = new StringBuffer (); 259 sb.append(getUserID()); 260 sb.append('@'); 261 sb.append(getOrganisationID()); 262 login = sb.toString(); 263 } 264 return login; 265 } 266 267 274 public void addRoleGroupRef(RoleGroupRef roleGroupRef) 275 { 276 if (roleGroupRef == null) 277 throw new NullPointerException ("roleGroupRef must not be null!"); 278 279 String roleGroupID = roleGroupRef.getRoleGroupID(); 280 if (roleGroupRefs.containsKey(roleGroupID)) 281 return; 282 283 if (authority.getRoleGroupRef(roleGroupID) == null) 284 throw new IllegalStateException ("RoleGroup \""+roleGroupID+"\" not registered in authority \""+authorityID+"\"!"); 285 286 roleGroupRefs.put(roleGroupID, roleGroupRef); 287 roleGroupRef._addUserRef(this); 288 289 RoleGroup roleGroup = roleGroupRef.getRoleGroup(); 290 for (Iterator it = roleGroup.getRoles().iterator(); it.hasNext(); ) { 291 Role role = (Role) it.next(); 292 _addRole(role, 1); 293 } 294 changeDT = new Date (); 295 } 296 297 public void removeRoleGroupRef(RoleGroupRef roleGroupRef) 298 { 299 if (roleGroupRef == null) 300 throw new NullPointerException ("roleGroup must not be null!"); 301 302 if (!authorityID.equals(roleGroupRef.getAuthorityID())) 303 throw new IllegalArgumentException ("userRef.authorityID != roleGroupRef.authorityID"); 304 305 removeRoleGroupRef(roleGroupRef.getRoleGroupID()); 306 } 307 308 public void removeRoleGroupRef(String roleGroupID) 309 { 310 if (roleGroupID == null) 311 throw new NullPointerException ("roleGroupID must not be null!"); 312 313 RoleGroupRef roleGroupRef = (RoleGroupRef)roleGroupRefs.get(roleGroupID); 314 315 if (roleGroupRef == null) 316 return; 317 318 for (Iterator it = roleGroupRef.getRoleGroup().getRoles().iterator(); it.hasNext(); ) { 319 Role role = (Role) it.next(); 320 _removeRole(role, 1); 321 } 323 roleGroupRefs.remove(roleGroupID); 324 roleGroupRef._removeUserRef(this); 325 changeDT = new Date (); 326 } 327 328 333 protected void _addRole(Role role, int incRefCount) 334 { 335 if (role == null) 336 throw new NullPointerException ("role must not be null!"); 337 338 if (incRefCount < 1) 339 throw new IllegalArgumentException ("incRefCount must be >= 1"); 340 341 String roleID = role.getRoleID(); 342 RoleRef roleRef = (RoleRef)roleRefs.get(roleID); 343 if (roleRef == null) { 344 roleRef = new RoleRef(authority, this, role); 345 roleRefs.put(roleID, roleRef); 346 } 347 roleRef.incrementReferenceCount(incRefCount); 348 changeDT = new Date (); 349 } 350 351 protected void _removeRole(Role role, int decRefCount) 352 { 353 String roleID = role.getRoleID(); 354 RoleRef roleRef = (RoleRef)roleRefs.get(roleID); 355 if (roleRef == null) { 356 LOGGER.warn("_removeRole(\""+roleID+"\"): The User \""+toString()+"\" does not contain a RoleRef for this role!"); 357 return; 358 } 359 360 if (decRefCount < 1) 361 throw new IllegalArgumentException ("decRefCount must be >= 1"); 362 363 if (roleRef.decrementReferenceCount(decRefCount) <= 0) { 364 if (roleRef.getReferenceCount() < 0) 365 LOGGER.warn("_removeRole(\""+roleID+"\"): referenceCount < 0!!! user=\""+toString()+"\"!"); 366 367 roleRefs.remove(roleID); 368 } changeDT = new Date (); 370 } 371 372 public Collection getRoleGroupRefs() 373 { 374 return roleGroupRefs.values(); 375 } 376 377 public Collection getRoleRefs() 378 { 379 return roleRefs.values(); 380 } 381 382 public boolean containsRoleRef(String roleID) 383 { 384 return roleRefs.containsKey(roleID); 385 } 386 387 protected transient String thisString = null; 388 public String toString() 389 { 390 if (thisString == null) { 391 StringBuffer sb = new StringBuffer (); 392 sb.append(this.getClass().getName()); 393 sb.append('{'); 394 sb.append(getLogin()); sb.append('}'); 396 thisString = sb.toString(); 397 } 398 return thisString; 399 } 400 401 404 public boolean equals(Object obj) { 405 if (obj == this) 406 return true; 407 408 if (!(obj instanceof User)) 409 return false; 410 411 User other = (User)obj; 412 return 413 this.getOrganisationID().equals(other.getOrganisationID()) 414 && 415 this.getUserID().equals(other.getUserID()); 416 } 417 418 421 public int hashCode() { 422 return this.getOrganisationID().hashCode() ^ this.getUserID().hashCode(); 423 } 424 425 public static int INCLUDE_NONE = 0; 426 public static int INCLUDE_AUTHORITY = 0x1; 427 public static int INCLUDE_USER = 0x2; 428 public static int INCLUDE_ROLEGROUPREFS = 0x4; 429 public static int INCLUDE_ALL = Integer.MAX_VALUE; 431 432 public void makeTransient(int includeMask) 433 { 434 PersistenceManager pm = ((PersistenceCapable)this).jdoGetPersistenceManager(); 435 if (pm == null) 436 return; 437 438 if ((INCLUDE_AUTHORITY & includeMask) != 0) 439 getAuthority().makeTransient(Authority.INCLUDE_NONE); 440 441 if ((INCLUDE_USER & includeMask) != 0) 442 getUser().makeTransient(User.INCLUDE_NONE); 443 444 Map tmpRoleGroupRefs = null; 445 Map tmpRoleRefs = null; 446 447 if ((INCLUDE_ROLEGROUPREFS & includeMask) != 0) { 448 tmpRoleGroupRefs = new HashMap (); 449 for (Iterator it = getRoleGroupRefs().iterator(); it.hasNext(); ) { 450 RoleGroupRef roleGroupRef = (RoleGroupRef)it.next(); 451 roleGroupRef.makeTransient(RoleGroupRef.INCLUDE_NONE); 452 tmpRoleGroupRefs.put(roleGroupRef.getRoleGroupID(), roleGroupRef); 453 } 454 } 455 456 pm.makeTransient(this); 457 458 if ((INCLUDE_AUTHORITY & includeMask) == 0) 459 authority = null; 460 461 if ((INCLUDE_USER & includeMask) == 0) 462 user = null; 463 464 roleGroupRefs = tmpRoleGroupRefs; 465 roleRefs = tmpRoleRefs; 466 } 467 468 493 } 494 | Popular Tags |