1 30 31 34 package com.nightlabs.ipanema.security; 35 import java.rmi.RemoteException ; 36 import java.util.ArrayList ; 37 import java.util.Collection ; 38 import java.util.List ; 39 40 import javax.ejb.CreateException ; 41 import javax.ejb.EJBException ; 42 import javax.ejb.SessionBean ; 43 import javax.ejb.SessionContext ; 44 import javax.jdo.JDOObjectNotFoundException; 45 import javax.jdo.PersistenceManager; 46 import javax.jdo.Query; 47 48 import com.nightlabs.ModuleException; 49 import com.nightlabs.ipanema.base.BaseSessionBeanImpl; 50 import com.nightlabs.ipanema.security.id.AuthorityID; 51 import com.nightlabs.ipanema.security.id.RoleGroupID; 52 import com.nightlabs.ipanema.security.id.UserID; 53 import com.nightlabs.ipanema.servermanager.IpanemaServerManager; 54 55 62 public abstract class AuthorityManagerBean extends BaseSessionBeanImpl implements SessionBean 63 { 64 public static final boolean CLOSE_PM = true; 67 68 71 public void setSessionContext(SessionContext sessionContext) 72 throws EJBException , RemoteException 73 { 74 super.setSessionContext(sessionContext); 75 } 76 77 81 public void ejbCreate() throws CreateException 82 { 83 } 92 97 public void ejbRemove() throws EJBException , RemoteException { } 98 99 103 public void createAuthority(String authorityID, String name, String description) 104 throws SecurityException 105 { 106 try { 107 PersistenceManager pm = getPersistenceManager(); 108 try { 109 Authority authority = new Authority(authorityID); 110 authority.setName(null, name); 111 authority.setDescription(null, description); 112 pm.makePersistent(authority); 113 } finally { 114 if (CLOSE_PM) pm.close(); 115 } 116 } catch (Exception x) { 117 throw new SecurityException (x); 118 } 119 } 120 121 125 public Authority getAuthority(String authorityID, String [] fetchGroups) 126 throws SecurityException 127 { 128 try 129 { 130 PersistenceManager pm = getPersistenceManager(); 131 132 if (fetchGroups != null) 133 pm.getFetchPlan().setGroups(fetchGroups); 134 else 135 pm.getFetchPlan().clearGroups(); 136 137 try 138 { 139 pm.getExtent(Authority.class, true); 140 try 141 { 142 Object o = pm.getObjectById(AuthorityID.create(authorityID), true); 143 return (Authority)pm.detachCopy(o); 144 } 145 catch (JDOObjectNotFoundException x) 146 { 147 throw new AuthorityNotFoundException("Authority \""+authorityID+"\" not found in organisation \""+getOrganisationID()+"\"!"); 148 } 149 } 150 finally 151 { 152 if (CLOSE_PM) pm.close(); 153 } 154 } 155 catch (Exception x) 156 { 157 throw new SecurityException (x); 158 } 159 } 160 161 165 public AuthoritySearchResult searchAuthorities ( 166 String searchStr, boolean exact, int itemsPerPage, int pageIndex, int includeMask) 167 throws SecurityException 168 { 169 try 170 { 171 PersistenceManager pm = getPersistenceManager(); 172 173 try 178 { 179 AuthoritySearchResult result = Authority.searchAuthorities(pm, searchStr, exact, itemsPerPage, pageIndex); 180 result.makeTransient(includeMask); 181 return result; 182 } 183 finally 184 { 185 if (CLOSE_PM) pm.close(); 186 } 187 } 188 catch (Exception x) 189 { 190 throw new SecurityException (x); 191 } 192 } 193 194 199 public List getAllAuthorities() 200 throws ModuleException 201 { 202 PersistenceManager pm = getPersistenceManager(); 203 try 204 { 205 Query query = pm.newQuery(pm.getExtent(Authority.class, true)); 206 Collection c = (Collection )query.execute(); 207 List result = new ArrayList (pm.detachCopyAll(c)); 208 return result; 209 } 210 finally 211 { 212 pm.close(); 213 } 214 } 215 216 220 public RoleGroupRefSearchResult searchRoleGroupRefs( 221 String authorityID, 222 String searchStr, boolean exact, int itemsPerPage, int pageIndex, int includeMask) 223 throws SecurityException 224 { 225 try { 226 PersistenceManager pm = getPersistenceManager(); 227 try { 228 pm.getExtent(Authority.class, true); 229 Authority authority; 230 try { 231 authority = (Authority)pm.getObjectById(AuthorityID.create(authorityID), true); 232 } catch (JDOObjectNotFoundException x) { 233 throw new AuthorityNotFoundException("Authority \""+authorityID+"\" could not be found in organisation \""+getOrganisationID()+"\"!"); 234 } 235 236 RoleGroupRefSearchResult result = authority.searchRoleGroupRefs( 237 searchStr, exact, itemsPerPage, pageIndex); 238 result.makeTransient(includeMask); 239 return result; 240 } finally { 241 if (CLOSE_PM) pm.close(); 242 } 243 } catch (Exception x) { 244 throw new SecurityException (x); 245 } 246 } 247 248 252 public UserRefSearchResult searchUserRefs( 253 String authorityID, 254 String searchStr, boolean exact, int itemsPerPage, int pageIndex, int includeMask) 255 throws SecurityException 256 { 257 try { 258 PersistenceManager pm = getPersistenceManager(); 259 try { 260 pm.getExtent(Authority.class, true); 261 Authority authority; 262 try { 263 authority = (Authority)pm.getObjectById(AuthorityID.create(authorityID), true); 264 } catch (JDOObjectNotFoundException x) { 265 throw new AuthorityNotFoundException("Authority \""+authorityID+"\" could not be found in organisation \""+getOrganisationID()+"\"!"); 266 } 267 268 UserRefSearchResult result = authority.searchUserRefs( 269 searchStr, exact, itemsPerPage, pageIndex); 270 result.makeTransient(includeMask); 271 return result; 272 } finally { 273 if (CLOSE_PM) pm.close(); 274 } 275 } catch (Exception x) { 276 throw new SecurityException (x); 277 } 278 } 279 280 284 289 public void createUserRef(String authorityID, String userID) 290 throws SecurityException 291 { 292 try { 293 PersistenceManager pm = getPersistenceManager(); 294 try { 295 pm.getExtent(Authority.class, true); 296 pm.getExtent(User.class, true); 297 298 Authority authority; 299 try { 300 authority = (Authority)pm.getObjectById(AuthorityID.create(authorityID), true); 301 } catch (JDOObjectNotFoundException x) { 302 throw new AuthorityNotFoundException("Authority \""+authorityID+"\" not found in organisation \""+getOrganisationID()+"\"!"); 303 } 304 User user; 305 try { 306 user = (User)pm.getObjectById(UserID.create(getOrganisationID(), userID), true); 307 } catch (JDOObjectNotFoundException x) { 308 throw new UserNotFoundException("User \""+userID+"\" not found in organisation \""+getOrganisationID()+"\"!"); 309 } 310 311 authority.createUserRef(user); 312 } finally { 313 if (CLOSE_PM) pm.close(); 314 } 315 } catch (SecurityException x) { 316 throw x; 317 } catch (Exception x) { 318 throw new SecurityException (x); 319 } 320 } 321 322 327 public void destroyUserRef(String authorityID, String userID) 328 throws SecurityException 329 { 330 try { 331 IpanemaServerManager ism = getIpanemaServerManager(); 332 try { 333 PersistenceManager pm = getPersistenceManager(); 334 try { 335 pm.getExtent(Authority.class, true); 336 338 Authority authority; 339 try { 340 authority = (Authority)pm.getObjectById(AuthorityID.create(authorityID), true); 341 } catch (JDOObjectNotFoundException x) { 342 throw new AuthorityNotFoundException("Authority \""+authorityID+"\" not found in organisation \""+getOrganisationID()+"\"!"); 343 } 344 351 authority.destroyUserRef(userID); 352 ism.ipanemaSecurity_flushCache(); 353 } finally { 354 if (CLOSE_PM) pm.close(); 355 } 356 } finally { 357 ism.close(); 358 } 359 } catch (SecurityException x) { 360 throw x; 361 } catch (Exception x) { 362 throw new SecurityException (x); 363 } 364 } 365 366 371 public void destroyRoleGroupRef(String authorityID, String roleGroupID) 372 throws SecurityException 373 { 374 try { 375 IpanemaServerManager ism = getIpanemaServerManager(); 376 try { 377 PersistenceManager pm = getPersistenceManager(); 378 try { 379 pm.getExtent(Authority.class, true); 380 382 Authority authority; 383 try { 384 authority = (Authority)pm.getObjectById(AuthorityID.create(authorityID), true); 385 } catch (JDOObjectNotFoundException x) { 386 throw new AuthorityNotFoundException("Authority \""+authorityID+"\" not found in organisation \""+getOrganisationID()+"\"!"); 387 } 388 395 authority.destroyRoleGroupRef(roleGroupID); 396 ism.ipanemaSecurity_flushCache(); 397 } finally { 398 if (CLOSE_PM) pm.close(); 399 } 400 } finally { 401 ism.close(); 402 } 403 } catch (SecurityException x) { 404 throw x; 405 } catch (Exception x) { 406 throw new SecurityException (x); 407 } 408 } 409 410 415 public UserRef getUserRef(String authorityID, String userID, int includeMask) 416 throws SecurityException 417 { 418 try { 419 PersistenceManager pm = getPersistenceManager(); 420 try { 421 pm.getExtent(Authority.class, true); 422 pm.getExtent(User.class, true); 423 424 Authority authority; 425 try { 426 authority = (Authority)pm.getObjectById(AuthorityID.create(authorityID), true); 427 } catch (JDOObjectNotFoundException x) { 428 throw new AuthorityNotFoundException("Authority \""+authorityID+"\" not found in organisation \""+getOrganisationID()+"\"!"); 429 } 430 UserRef userRef = authority.getUserRef(userID); 431 if (userRef == null) 432 throw new UserRefNotFoundException("UserRef for User \""+userID+"\" not found in authority \""+authorityID+"\" in organisation \""+getOrganisationID()+"\"!"); 433 434 userRef.makeTransient(includeMask); 435 436 return userRef; 437 } finally { 438 if (CLOSE_PM) pm.close(); 439 } 440 } catch (SecurityException x) { 441 throw x; 442 } catch (Exception x) { 443 throw new SecurityException (x); 444 } 445 } 446 447 452 public void createRoleGroupRef(String authorityID, String roleGroupID) 453 throws SecurityException 454 { 455 try { 456 PersistenceManager pm = getPersistenceManager(); 457 try { 458 pm.getExtent(Authority.class, true); 459 pm.getExtent(RoleGroup.class, true); 460 461 Authority authority; 462 try { 463 authority = (Authority)pm.getObjectById(AuthorityID.create(authorityID), true); 464 } catch (JDOObjectNotFoundException x) { 465 throw new AuthorityNotFoundException("Authority \""+authorityID+"\" not found in organisation \""+getOrganisationID()+"\"!"); 466 } 467 RoleGroup roleGroup; 468 try { 469 roleGroup = (RoleGroup)pm.getObjectById(RoleGroupID.create(roleGroupID), true); 470 } catch (JDOObjectNotFoundException x) { 471 throw new UserNotFoundException("RoleGroup \""+roleGroupID+"\" not found in organisation \""+getOrganisationID()+"\"!"); 472 } 473 474 authority.createRoleGroupRef(roleGroup); 475 } finally { 476 if (CLOSE_PM) pm.close(); 477 } 478 } catch (SecurityException x) { 479 throw x; 480 } catch (Exception x) { 481 throw new SecurityException (x); 482 } 483 } 484 485 490 public RoleGroupRef getRoleGroupRef(String authorityID, String roleGroupID, int includeMask) 491 throws SecurityException 492 { 493 try { 494 PersistenceManager pm = getPersistenceManager(); 495 try { 496 pm.getExtent(Authority.class, true); 497 pm.getExtent(User.class, true); 498 499 Authority authority; 500 try { 501 authority = (Authority)pm.getObjectById(AuthorityID.create(authorityID), true); 502 } catch (JDOObjectNotFoundException x) { 503 throw new AuthorityNotFoundException("Authority \""+authorityID+"\" not found in organisation \""+getOrganisationID()+"\"!"); 504 } 505 RoleGroupRef roleGroupRef = authority.getRoleGroupRef(roleGroupID); 506 if (roleGroupRef == null) 507 throw new RoleGroupRefNotFoundException("RoleGroupRef for RoleGroup \""+roleGroupID+"\" not found in authority \""+authorityID+"\" in organisation \""+getOrganisationID()+"\"!"); 508 509 roleGroupRef.makeTransient(includeMask); 510 511 return roleGroupRef; 512 } finally { 513 if (CLOSE_PM) pm.close(); 514 } 515 } catch (SecurityException x) { 516 throw x; 517 } catch (Exception x) { 518 throw new SecurityException (x); 519 } 520 } 521 522 532 public void addRoleGroupRefToUserRef(String authorityID, String userID, String roleGroupID) 533 throws SecurityException 534 { 535 try { 536 IpanemaServerManager ism = getIpanemaServerManager(); 537 try { 538 PersistenceManager pm = getPersistenceManager(); 539 try { 540 pm.getExtent(User.class, true); 542 pm.getExtent(RoleGroup.class, true); 543 544 Authority authority; 545 try { 546 authority = (Authority)pm.getObjectById(AuthorityID.create(authorityID), true); 547 } catch (JDOObjectNotFoundException x) { 548 throw new AuthorityNotFoundException("Authority \""+authorityID+"\" not found in organisation \""+getOrganisationID()+"\"!"); 549 } 550 551 UserRef userRef = authority.getUserRef(userID); 552 if (userRef == null) 553 throw new UserRefNotFoundException("UserRef for user \""+userID+"\" not found in authority \""+authorityID+"\" in organisation \""+getOrganisationID()+"\"!"); 554 555 RoleGroupRef roleGroupRef = authority.getRoleGroupRef(roleGroupID); 556 if (roleGroupRef == null) 557 throw new RoleGroupRefNotFoundException("RoleGroupRef for roleGroup \""+roleGroupID+"\" not found in authority \""+authorityID+"\" in organisation \""+getOrganisationID()+"\"!"); 558 559 userRef.addRoleGroupRef(roleGroupRef); 560 ism.ipanemaSecurity_flushCache(userID); 561 } finally { 562 if (CLOSE_PM) pm.close(); 563 } 564 } finally { 565 ism.close(); 566 } 567 } catch (SecurityException x) { 568 throw x; 569 } catch (Exception x) { 570 throw new SecurityException (x); 571 } 572 } 573 574 584 public void removeRoleGroupRefFromUserRef(String authorityID, String userID, String roleGroupID) 585 throws SecurityException 586 { 587 try { 588 IpanemaServerManager ism = getIpanemaServerManager(); 589 try { 590 PersistenceManager pm = getPersistenceManager(); 591 try { 592 pm.getExtent(User.class, true); 594 pm.getExtent(RoleGroup.class, true); 595 596 Authority authority; 597 try { 598 authority = (Authority)pm.getObjectById(AuthorityID.create(authorityID), true); 599 } catch (JDOObjectNotFoundException x) { 600 throw new AuthorityNotFoundException("Authority \""+authorityID+"\" not found in organisation \""+getOrganisationID()+"\"!"); 601 } 602 603 UserRef userRef = authority.getUserRef(userID); 604 if (userRef == null) 605 throw new UserRefNotFoundException("UserRef for user \""+userID+"\" not found in authority \""+authorityID+"\" in organisation \""+getOrganisationID()+"\"!"); 606 607 RoleGroupRef roleGroupRef = authority.getRoleGroupRef(roleGroupID); 608 if (roleGroupRef == null) 609 throw new UserRefNotFoundException("RoleGroupRef for roleGroup \""+roleGroupID+"\" not found in authority \""+authorityID+"\" in organisation \""+getOrganisationID()+"\"!"); 610 611 userRef.removeRoleGroupRef(roleGroupRef); 612 ism.ipanemaSecurity_flushCache(userID); 613 } finally { 614 if (CLOSE_PM) pm.close(); 615 } 616 } finally { 617 ism.close(); 618 } 619 } catch (SecurityException x) { 620 throw x; 621 } catch (Exception x) { 622 throw new SecurityException (x); 623 } 624 } 625 } | Popular Tags |