1 18 19 package cowsultants.itracker.ejb.beans.session; 20 21 import java.util.*; 22 import javax.ejb.*; 23 import javax.jms.*; 24 import javax.naming.*; 25 import javax.rmi.*; 26 27 import cowsultants.itracker.ejb.authentication.*; 28 import cowsultants.itracker.ejb.beans.entity.*; 29 import cowsultants.itracker.ejb.beans.message.*; 30 import cowsultants.itracker.ejb.client.exceptions.*; 31 import cowsultants.itracker.ejb.client.interfaces.*; 32 import cowsultants.itracker.ejb.client.models.*; 33 import cowsultants.itracker.ejb.client.util.*; 34 35 36 40 public class UserHandlerBean implements SessionBean { 41 private static final String DEFAULT_AUTHENTICATOR = "cowsultants.itracker.ejb.authentication.DefaultAuthenticator"; 42 43 private static String notificationFactoryName = NotificationMessageBean.DEFAULT_CONNECTION_FACTORY; 44 private static String notificationQueueName = NotificationMessageBean.DEFAULT_QUEUE_NAME; 45 private static String authenticatorClassName = null; 46 private static Class authenticatorClass = null; 47 private static String systemBaseURL = ""; 48 49 private SessionContext context; 50 private static boolean allowSelfRegister = false; 51 52 InitialContext ic = null; 53 54 IDGeneratorHome idHome = null; 55 ProjectHandlerHome phHome = null; 56 SystemConfigurationHome scHome = null; 57 58 NotificationLocalHome nHome = null; 59 PermissionLocalHome pHome = null; 60 ProjectLocalHome projectHome = null; 61 UserLocalHome uHome = null; 62 UserPreferencesLocalHome upHome = null; 63 64 public UserModel getUser(Integer userId) { 65 try { 66 UserLocal user = uHome.findByPrimaryKey(userId); 67 return user.getModel(); 68 } catch(FinderException fe) { 69 } 70 return null; 71 } 72 73 public UserModel getUserByLogin(String login) { 74 try { 75 UserLocal user = uHome.findByLogin(login); 76 return user.getModel(); 77 } catch(FinderException fe) { 78 } 79 return null; 80 } 81 82 public String getUserPasswordByLogin(String login) { 83 try { 84 UserLocal user = uHome.findByLogin(login); 85 return user.getPassword(); 86 } catch(FinderException fe) { 87 } 88 return null; 89 } 90 91 public UserModel[] getAllUsers() { 92 int i = 0; 93 UserModel[] userArray = new UserModel[0]; 94 95 try { 96 Collection users = uHome.findAll(); 97 userArray = new UserModel[users.size()]; 98 for(Iterator iterator = users.iterator(); iterator.hasNext(); i++) { 99 userArray[i] = ((UserLocal) iterator.next()).getModel(); 100 } 101 } catch(FinderException fe) { 102 } 103 return userArray; 104 } 105 106 public int getNumberUsers() { 107 try { 108 Collection users = uHome.findAll(); 109 return users.size(); 110 } catch(FinderException fe) { 111 } 112 return 0; 113 } 114 115 public UserModel[] getActiveUsers() { 116 int i = 0; 117 UserModel[] userArray = new UserModel[0]; 118 119 try { 120 Collection users = uHome.findActive(); 121 userArray = new UserModel[users.size()]; 122 for(Iterator iterator = users.iterator(); iterator.hasNext(); i++) { 123 userArray[i] = ((UserLocal) iterator.next()).getModel(); 124 } 125 } catch(FinderException fe) { 126 } 127 return userArray; 128 } 129 130 public UserModel[] getSuperUsers() { 131 int i = 0; 132 UserModel[] userArray = new UserModel[0]; 133 134 try { 135 Collection superUsers = uHome.findSuperUsers(); 136 userArray = new UserModel[superUsers.size()]; 137 for(Iterator iterator = superUsers.iterator(); iterator.hasNext(); i++) { 138 userArray[i] = ((UserLocal) iterator.next()).getModel(); 139 } 140 } catch(FinderException fe) { 141 } 142 return userArray; 143 } 144 145 public UserPreferencesModel getUserPreferencesByUserId(Integer userId) { 146 try { 147 UserPreferencesLocal userPrefs = upHome.findByUserId(userId); 148 return userPrefs.getModel(); 149 } catch(FinderException fe) { 150 } 151 return new UserPreferencesModel(); 152 } 153 154 public UserModel createUser(UserModel model) throws UserException { 155 try { 156 if(model == null || model.getLogin() == null || model.getLogin().equals("")) { 157 throw new CreateException("User data was null, or login was empty."); 158 } 159 160 try { 161 UserLocal userTest = uHome.findByLogin(model.getLogin()); 162 throw new CreateException("User already exists with login: " + model.getLogin()); 163 } catch(ObjectNotFoundException onfe) { 164 } catch(FinderException fe) { 165 throw new CreateException("Testing for existing login failed: " + fe.getMessage()); 166 } 167 168 try { 169 PluggableAuthenticator authenticator = (PluggableAuthenticator) authenticatorClass.newInstance(); 170 if(authenticator != null) { 171 HashMap values = new HashMap(); 172 values.put("userHandler", context.getEJBLocalObject()); 173 values.put("systemConfiguration", scHome.create()); 174 authenticator.initialize(values); 175 authenticator.createProfile(model, null, AuthenticationConstants.AUTH_TYPE_UNKNOWN, AuthenticationConstants.REQ_SOURCE_UNKNOWN); 176 } else { 177 Logger.logError("Unable to create new authenticator."); 178 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 179 } 180 } catch(IllegalAccessException iae) { 181 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 182 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 183 } catch(InstantiationException ie) { 184 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 185 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 186 } catch(ClassCastException cce) { 187 Logger.logError("Authenticator class " + authenticatorClassName + " does not extend the PluggableAuthenticator class."); 188 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 189 } catch(CreateException ce) { 190 Logger.logError("Error creating new SystemConfiguration. " + ce.getMessage()); 191 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 192 } 193 194 IDGenerator idGen = idHome.create(); 195 UserLocal user = uHome.create(idGen.getId(UserLocal.ID_NAME)); 196 user.setModel(model); 197 user.setStatus(UserUtilities.STATUS_ACTIVE); 198 user.setRegistrationType(model.getRegistrationType()); 199 return user.getModel(); 200 } catch(CreateException ce) { 201 Logger.logDebug("Could not create user.", ce); 202 throw new UserException(ce.getMessage()); 203 } catch(AuthenticatorException ae) { 204 Logger.logDebug("Could not create user.", ae); 205 throw new UserException(ae.getMessage()); 206 } 207 } 208 209 public UserModel updateUser(UserModel model) throws UserException { 210 try { 211 UserLocal user = uHome.findByPrimaryKey(model.getId()); 212 213 if(! user.getLogin().equals(model.getLogin())) { 214 try { 215 UserLocal userTest = uHome.findByLogin(model.getLogin()); 216 throw new UserException("User already exists with login: " + model.getLogin()); 217 } catch(ObjectNotFoundException onfe) { 218 } 219 } 220 221 try { 222 PluggableAuthenticator authenticator = (PluggableAuthenticator) authenticatorClass.newInstance(); 223 if(authenticator != null) { 224 HashMap values = new HashMap(); 225 values.put("userHandler", context.getEJBLocalObject()); 226 values.put("systemConfiguration", scHome.create()); 227 authenticator.initialize(values); 228 authenticator.updateProfile(model, AuthenticationConstants.UPDATE_TYPE_CORE, null, AuthenticationConstants.AUTH_TYPE_UNKNOWN, AuthenticationConstants.REQ_SOURCE_UNKNOWN); 229 } else { 230 Logger.logError("Unable to create new authenticator."); 231 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 232 } 233 } catch(IllegalAccessException iae) { 234 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 235 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 236 } catch(InstantiationException ie) { 237 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 238 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 239 } catch(ClassCastException cce) { 240 Logger.logError("Authenticator class " + authenticatorClassName + " does not extend the PluggableAuthenticator class."); 241 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 242 } catch(CreateException ce) { 243 Logger.logError("Error creating new SystemConfiguration. " + ce.getMessage()); 244 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 245 } 246 247 user.setModel(model); 248 return user.getModel(); 249 } catch(FinderException fe) { 250 } catch(AuthenticatorException ae) { 251 Logger.logError("Unable to update user."); 252 Logger.logDebug("AuthenticatorException caught.", ae); 253 throw new UserException(ae.getMessage()); 254 } 255 return null; 256 } 257 258 public String generateUserPassword(UserModel model) throws PasswordException { 259 try { 260 UserLocal user = uHome.findByPrimaryKey(model.getId()); 261 String password = UserUtilities.generatePassword(); 262 user.setPassword(password); 263 return password; 264 } catch(FinderException fe) { 265 throw new PasswordException(PasswordException.UNKNOWN_USER); 266 } 267 } 268 269 public UserPreferencesModel updateUserPreferences(UserPreferencesModel model) throws UserException { 270 try { 271 UserPreferencesLocal userPrefs = null; 272 273 UserLocal user = uHome.findByPrimaryKey(model.getUserId()); 274 UserModel updateUser = user.getModel(); 275 updateUser.setPreferences(model); 276 try { 277 PluggableAuthenticator authenticator = (PluggableAuthenticator) authenticatorClass.newInstance(); 278 if(authenticator != null) { 279 HashMap values = new HashMap(); 280 values.put("userHandler", context.getEJBLocalObject()); 281 values.put("systemConfiguration", scHome.create()); 282 authenticator.initialize(values); 283 authenticator.updateProfile(updateUser, AuthenticationConstants.UPDATE_TYPE_PREFERENCE, null, AuthenticationConstants.AUTH_TYPE_UNKNOWN, AuthenticationConstants.REQ_SOURCE_UNKNOWN); 284 } else { 285 Logger.logError("Unable to create new authenticator."); 286 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 287 } 288 } catch(IllegalAccessException iae) { 289 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 290 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 291 } catch(InstantiationException ie) { 292 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 293 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 294 } catch(ClassCastException cce) { 295 Logger.logError("Authenticator class " + authenticatorClassName + " does not extend the PluggableAuthenticator class."); 296 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 297 } catch(CreateException ce) { 298 Logger.logError("Error creating new SystemConfiguration. " + ce.getMessage()); 299 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 300 } 301 302 303 try { 304 userPrefs = upHome.findByUserId(model.getUserId()); 305 } catch(ObjectNotFoundException onfe) { 306 if(userPrefs == null || userPrefs.getUser().getId().intValue() != model.getUserId().intValue()) { 307 IDGenerator idGen = idHome.create(); 308 userPrefs = upHome.create(idGen.getId(UserPreferencesLocal.ID_NAME)); 309 } 310 } 311 312 userPrefs.setUser(user); 313 userPrefs.setModel(model); 314 model.setUserId(user.getId()); 316 model.setUserLogin(user.getLogin()); 317 return model; 318 } catch(CreateException ce) { 319 throw new UserException("Unable to create new preferences."); 320 } catch(AuthenticatorException ae) { 321 Logger.logDebug("AuthenticatorException while updating preferences.", ae); 322 throw new UserException("Unable to create new preferences."); 323 } catch(FinderException fe) { 324 } 325 return null; 326 } 327 328 public boolean deleteUser(UserModel model) { 329 if(model != null) { 330 try { 331 UserLocal user = uHome.findByPrimaryKey(model.getId()); 332 user.remove(); 333 return true; 334 } catch(FinderException fe) { 335 Logger.logWarn("Could not find user (" + model.getId() + ") to remove.", fe); 336 } catch(RemoveException re) { 337 Logger.logError("Could not remove user (" + model.getId() + ").", re); 338 } 339 } 340 return false; 341 } 342 343 public boolean setUserStatus(Integer userId, int status) { 344 try { 345 UserLocal user = uHome.findByPrimaryKey(userId); 346 user.setStatus(status); 347 return true; 348 } catch(FinderException fe) { 349 Logger.logWarn("Could not find user (" + userId + ") to set status.", fe); 350 } 351 return false; 352 } 353 354 public boolean clearOwnedProjects(Integer userId) { 355 try { 356 UserLocal user = uHome.findByPrimaryKey(userId); 357 Collection projects = user.getProjects(); 358 projects.clear(); 359 return true; 360 } catch(FinderException fe) { 361 Logger.logWarn("Could not find user (" + userId + ") to clear owned projects.", fe); 362 } 363 return false; 364 } 365 366 public UserModel[] getUsersWithPermissionLocal(PermissionModel permission) { 367 int i = 0; 368 UserModel[] userArray = new UserModel[0]; 369 370 if(permission != null && permission.getProjectId() != null) { 371 try { 372 Collection permissions = pHome.findByProjectIdAndPermission(permission.getProjectId(), permission.getPermissionType()); 373 userArray = new UserModel[permissions.size()]; 374 for(Iterator iterator = permissions.iterator(); iterator.hasNext(); i++) { 375 userArray[i] = ((PermissionLocal) iterator.next()).getUser().getModel(); 376 } 377 } catch(FinderException fe) { 378 } 379 } 380 return userArray; 381 } 382 383 public PermissionModel[] getUserPermissionsLocal(UserModel user) { 384 int i = 0; 385 PermissionModel[] permissionArray = new PermissionModel[0]; 386 387 if(user != null && user.getId() != null) { 388 try { 389 Collection permissions = pHome.findByUserId(user.getId()); 390 permissionArray = new PermissionModel[permissions.size()]; 391 for(Iterator iterator = permissions.iterator(); iterator.hasNext(); i++) { 392 permissionArray[i] = ((PermissionLocal) iterator.next()).getModel(); 393 } 394 } catch(FinderException fe) { 395 } 396 } 397 return permissionArray; 398 } 399 400 public PermissionModel[] getPermissionsByUserId(Integer userId) { 401 PermissionModel[] permissionArray = new PermissionModel[0]; 402 403 UserModel user = getUser(userId); 404 if(user != null) { 405 try { 406 PluggableAuthenticator authenticator = (PluggableAuthenticator) authenticatorClass.newInstance(); 407 if(authenticator != null) { 408 HashMap values = new HashMap(); 409 values.put("userHandler", context.getEJBLocalObject()); 410 values.put("systemConfiguration", scHome.create()); 411 authenticator.initialize(values); 412 permissionArray = authenticator.getUserPermissions(user, AuthenticationConstants.REQ_SOURCE_UNKNOWN); 413 } 414 Logger.logDebug("Found " + permissionArray.length + " permissions for user " + user.getLogin()); 415 } catch(IllegalAccessException iae) { 416 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 417 } catch(InstantiationException ie) { 418 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 419 } catch(ClassCastException cce) { 420 Logger.logError("Authenticator class " + authenticatorClassName + " does not extend the PluggableAuthenticator class."); 421 } catch(CreateException ce) { 422 Logger.logError("Error creating new SystemConfiguration. " + ce.getMessage()); 423 } catch(AuthenticatorException ae) { 424 Logger.logError("Authenticator exception: " + ae.getMessage()); 425 Logger.logDebug("Authenticator exception: ", ae); 426 } 427 } 428 return permissionArray; 429 } 430 431 public boolean addUserPermissions(Integer userId, PermissionModel[] newPermissions) { 432 boolean successful = false; 433 434 if(newPermissions == null || newPermissions.length == 0) { 435 return false; 436 } 437 438 try { 439 IDGenerator idGen = idHome.create(); 440 UserLocal user = uHome.findByPrimaryKey(userId); 441 UserModel updateUser = user.getModel(); 442 updateUser.setPermissions(newPermissions); 443 try { 444 PluggableAuthenticator authenticator = (PluggableAuthenticator) authenticatorClass.newInstance(); 445 if(authenticator != null) { 446 HashMap values = new HashMap(); 447 values.put("userHandler", context.getEJBLocalObject()); 448 values.put("systemConfiguration", scHome.create()); 449 authenticator.initialize(values); 450 if(authenticator.updateProfile(updateUser, AuthenticationConstants.UPDATE_TYPE_PERMISSION_SET, null, AuthenticationConstants.AUTH_TYPE_UNKNOWN, AuthenticationConstants.REQ_SOURCE_UNKNOWN)) { 451 newPermissions = updateUser.getPermissions(); 452 } 453 } else { 454 Logger.logError("Unable to create new authenticator."); 455 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 456 } 457 } catch(IllegalAccessException iae) { 458 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 459 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 460 } catch(InstantiationException ie) { 461 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 462 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 463 } catch(ClassCastException cce) { 464 Logger.logError("Authenticator class " + authenticatorClassName + " does not extend the PluggableAuthenticator class."); 465 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 466 } catch(CreateException ce) { 467 Logger.logError("Error creating new SystemConfiguration. " + ce.getMessage()); 468 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 469 } 470 471 Collection permissions = pHome.findByUserId(userId); 472 473 Integer projectId = null; 474 ProjectLocal project = null; 475 for(int i = 0; i < newPermissions.length; i++) { 476 if(newPermissions[i].getProjectId() == null) { 477 continue; 478 } 479 480 try { 481 if(projectId == null || projectId.intValue() != newPermissions[i].getProjectId().intValue()) { 482 projectId = newPermissions[i].getProjectId(); 483 project = projectHome.findByPrimaryKey(projectId); 484 } 485 486 PermissionLocal permission = pHome.create(idGen.getId(PermissionLocal.ID_NAME)); 487 permission.setModel(newPermissions[i]); 488 permission.setProject(project); 489 permission.setUser(user); 490 permissions.add(permission); 491 } catch(CreateException ce) { 492 Logger.logWarn("Error adding user (" + userId + ") permission (Proj: " + newPermissions[i].getProjectId() + " Perm: " + newPermissions[i].getPermissionType() + "). Could not create permission.", ce); 493 successful = false; 494 } catch(FinderException fe) { 495 Logger.logWarn("Error adding user (" + userId + ") permission (Proj: " + newPermissions[i].getProjectId() + " Perm: " + newPermissions[i].getPermissionType() + "). Could not find project.", fe); 496 successful = false; 497 } 498 } 499 successful = true; 500 } catch(CreateException ce) { 501 Logger.logWarn("Error setting user (" + userId + ") permissions. Could not create session beans.", ce); 502 successful = false; 503 } catch(AuthenticatorException ae) { 504 Logger.logWarn("Error setting user (" + userId + ") permissions. AuthenticatorException.", ae); 505 successful = false; 506 } catch(FinderException fe) { 507 Logger.logWarn("Error setting user (" + userId + ") permissions. Could not find user.", fe); 508 successful = false; 509 } 510 511 return successful; 512 } 513 514 public boolean setUserPermissions(Integer userId, PermissionModel[] newPermissions) { 515 boolean successful = true; 516 517 try { 518 IDGenerator idGen = idHome.create(); 519 UserLocal user = uHome.findByPrimaryKey(userId); 520 UserModel updateUser = user.getModel(); 521 updateUser.setPermissions(newPermissions); 522 try { 523 PluggableAuthenticator authenticator = (PluggableAuthenticator) authenticatorClass.newInstance(); 524 if(authenticator != null) { 525 HashMap values = new HashMap(); 526 values.put("userHandler", context.getEJBLocalObject()); 527 values.put("systemConfiguration", scHome.create()); 528 authenticator.initialize(values); 529 if(authenticator.updateProfile(updateUser, AuthenticationConstants.UPDATE_TYPE_PERMISSION_SET, null, AuthenticationConstants.AUTH_TYPE_UNKNOWN, AuthenticationConstants.REQ_SOURCE_UNKNOWN)) { 530 newPermissions = updateUser.getPermissions(); 531 } 532 } else { 533 Logger.logError("Unable to create new authenticator."); 534 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 535 } 536 } catch(IllegalAccessException iae) { 537 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 538 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 539 } catch(InstantiationException ie) { 540 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 541 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 542 } catch(ClassCastException cce) { 543 Logger.logError("Authenticator class " + authenticatorClassName + " does not extend the PluggableAuthenticator class."); 544 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 545 } catch(CreateException ce) { 546 Logger.logError("Error creating new SystemConfiguration. " + ce.getMessage()); 547 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 548 } 549 550 Collection permissions = pHome.findByUserId(userId); 551 552 if(newPermissions == null || newPermissions.length == 0) { 553 for(Iterator iterator = permissions.iterator(); iterator.hasNext(); ) { 554 PermissionLocal permission = (PermissionLocal) iterator.next(); 555 try { 556 permission.remove(); 557 } catch(RemoveException re) { 558 } 559 } 560 } else { 561 HashMap newPermissionsMap = new HashMap(); 562 for(int i = 0; i < newPermissions.length; i++) { 563 if(newPermissions[i] != null) { 564 newPermissionsMap.put("Perm" + newPermissions[i].getPermissionType() + "Proj" + newPermissions[i].getProjectId(), newPermissions[i]); 565 } 566 } 567 568 for(Iterator iterator = permissions.iterator(); iterator.hasNext(); ) { 569 PermissionLocal permission = (PermissionLocal) iterator.next(); 570 PermissionModel model = permission.getModel(); 571 if(newPermissionsMap.containsKey("Perm" + model.getPermissionType() + "Proj" + model.getProjectId())) { 572 newPermissionsMap.remove("Perm" + model.getPermissionType() + "Proj" + model.getProjectId()); 573 } else { 574 try { 575 iterator.remove(); 576 permission.remove(); 577 } catch(RemoveException re) { 578 Logger.logWarn("Error removing user (" + userId + ") permission (Proj: " + model.getProjectId() + " Perm: " + model.getPermissionType() + ").", re); 579 successful = false; 580 } 581 } 582 } 583 if(newPermissionsMap.values() != null) { 584 for(Iterator iterator = newPermissionsMap.values().iterator(); iterator.hasNext(); ) { 585 PermissionModel model = (PermissionModel) iterator.next(); 586 if(model.getProjectId() != null) { 587 try { 588 ProjectLocal project = projectHome.findByPrimaryKey(model.getProjectId()); 589 590 PermissionLocal permission = pHome.create(idGen.getId(PermissionLocal.ID_NAME)); 591 permission.setModel(model); 592 permission.setProject(project); 593 permission.setUser(user); 594 permissions.add(permission); 595 } catch(CreateException ce) { 596 Logger.logWarn("Error adding user (" + userId + ") permission (Proj: " + model.getProjectId() + " Perm: " + model.getPermissionType() + "). Could not create permission.", ce); 597 successful = false; 598 } catch(FinderException fe) { 599 Logger.logWarn("Error adding user (" + userId + ") permission (Proj: " + model.getProjectId() + " Perm: " + model.getPermissionType() + "). Could not find project.", fe); 600 successful = false; 601 } 602 } 603 } 604 } 605 } 606 } catch(CreateException ce) { 607 Logger.logWarn("Error setting user (" + userId + ") permissions. Could not create session beans.", ce); 608 successful = false; 609 } catch(FinderException fe) { 610 Logger.logWarn("Error setting user (" + userId + ") permissions. Could not find user.", fe); 611 successful = false; 612 } catch(AuthenticatorException ae) { 613 Logger.logWarn("Error setting user (" + userId + ") permissions. AuthenticatorException.", ae); 614 successful = false; 615 } 616 return successful; 617 } 618 619 public HashMap getUserPermissions(UserModel user, int reqSource) { 620 HashMap permissions = new HashMap(); 621 622 if(user == null) { 623 return permissions; 624 } 625 626 PermissionModel[] permissionArray = new PermissionModel[0]; 627 628 try { 629 PluggableAuthenticator authenticator = (PluggableAuthenticator) authenticatorClass.newInstance(); 630 if(authenticator != null) { 631 HashMap values = new HashMap(); 632 values.put("userHandler", context.getEJBLocalObject()); 633 values.put("systemConfiguration", scHome.create()); 634 authenticator.initialize(values); 635 permissionArray = authenticator.getUserPermissions(user, (reqSource == 0 ? AuthenticationConstants.REQ_SOURCE_UNKNOWN : reqSource)); 636 } 637 Logger.logDebug("Found " + permissionArray.length + " permissions for user " + user.getLogin()); 638 } catch(IllegalAccessException iae) { 639 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 640 } catch(InstantiationException ie) { 641 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 642 } catch(ClassCastException cce) { 643 Logger.logError("Authenticator class " + authenticatorClassName + " does not extend the PluggableAuthenticator class."); 644 } catch(CreateException ce) { 645 Logger.logError("Error creating new SystemConfiguration. " + ce.getMessage()); 646 } catch(AuthenticatorException ae) { 647 Logger.logError("Authenticator exception: " + ae.getMessage()); 648 Logger.logDebug("Authenticator exception: ", ae); 649 } 650 651 permissions = UserUtilities.permissionsArrayToMap(permissionArray); 652 653 if(allowSelfRegister) { 654 try { 655 ProjectHandler ph = phHome.create(); 656 ProjectModel[] projects = ph.getAllProjects(); 657 for(int i = 0; i < projects.length; i++) { 658 if(projects[i].getOptions() >= ProjectUtilities.OPTION_ALLOW_SELF_REGISTERED_CREATE) { 659 if(permissions.get(projects[i].getId()) == null) { 660 HashSet projectPermissions = new HashSet(); 661 permissions.put(projects[i].getId(), projectPermissions); 662 } 663 if(ProjectUtilities.hasOption(ProjectUtilities.OPTION_ALLOW_SELF_REGISTERED_CREATE, projects[i].getOptions())) { 664 ((HashSet) permissions.get(projects[i].getId())).add(Integer.toString(UserUtilities.PERMISSION_VIEW_USERS)); 665 ((HashSet) permissions.get(projects[i].getId())).add(Integer.toString(UserUtilities.PERMISSION_CREATE)); 666 } 667 if(ProjectUtilities.hasOption(ProjectUtilities.OPTION_ALLOW_SELF_REGISTERED_VIEW_ALL, projects[i].getOptions())) { 668 ((HashSet) permissions.get(projects[i].getId())).add(Integer.toString(UserUtilities.PERMISSION_VIEW_ALL)); 669 } 670 } 671 } 672 } catch(CreateException ce) { 673 } 674 } 675 676 return permissions; 677 } 678 679 public UserModel[] getUsersWithProjectPermission(Integer projectId, int permission) { 680 return getUsersWithProjectPermission(projectId, permission, true); 681 } 682 683 public UserModel[] getUsersWithProjectPermission(Integer projectId, int permission, boolean activeOnly) { 684 return getUsersWithAnyProjectPermission(projectId, new int[] {permission}, activeOnly); 685 } 686 687 public UserModel[] getUsersWithAnyProjectPermission(Integer projectId, int[] permissions) { 688 return getUsersWithAnyProjectPermission(projectId, permissions, true); 689 } 690 691 public UserModel[] getUsersWithAnyProjectPermission(Integer projectId, int[] permissions, boolean activeOnly) { 692 return getUsersWithProjectPermission(projectId, permissions, false, activeOnly); 693 } 694 695 public UserModel[] getUsersWithProjectPermission(Integer projectId, int[] permissions, boolean requireAll, boolean activeOnly) { 696 UserModel[] userArray = new UserModel[0]; 697 PermissionModel[] reqPermissions = new PermissionModel[permissions.length]; 698 for(int i = 0; i < permissions.length; i++) { 699 reqPermissions[i] = new PermissionModel(projectId, permissions[i]); 700 } 701 702 try { 703 PluggableAuthenticator authenticator = (PluggableAuthenticator) authenticatorClass.newInstance(); 704 if(authenticator != null) { 705 HashMap values = new HashMap(); 706 values.put("userHandler", context.getEJBLocalObject()); 707 values.put("systemConfiguration", scHome.create()); 708 authenticator.initialize(values); 709 userArray = authenticator.getUsersWithProjectPermission(reqPermissions, requireAll, activeOnly, AuthenticationConstants.REQ_SOURCE_UNKNOWN); 710 } 711 if(Logger.isLoggingDebug()) { 712 StringBuffer permissionsString = new StringBuffer ("{ "); 713 for(int i = 0; i < permissions.length; i++) { 714 permissionsString.append(permissions[i] + " "); 715 } 716 permissionsString.append("}"); 717 Logger.logDebug("Found " + userArray.length + " users with project " + projectId + " permissions " + permissionsString.toString() + (requireAll ? "[AllReq," : "[AnyReq,") + (activeOnly ? "ActiveUsersOnly]" : "AllUsers]")); 718 } 719 } catch(IllegalAccessException iae) { 720 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 721 } catch(InstantiationException ie) { 722 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 723 } catch(ClassCastException cce) { 724 Logger.logError("Authenticator class " + authenticatorClassName + " does not extend the PluggableAuthenticator class."); 725 } catch(CreateException ce) { 726 Logger.logError("Error creating new SystemConfiguration. " + ce.getMessage()); 727 } catch(AuthenticatorException ae) { 728 Logger.logError("Authenticator exception: " + ae.getMessage()); 729 Logger.logDebug("Authenticator exception: ", ae); 730 } 731 732 return userArray; 733 } 734 735 public UserModel[] getPossibleOwners(IssueModel issue, Integer projectId, Integer userId) { 736 HashSet users = new HashSet(); 737 738 UserModel[] editUsers = getUsersWithProjectPermission(projectId, UserUtilities.PERMISSION_EDIT, true); 739 for(int i = 0; i < editUsers.length; i++) { 740 users.add(editUsers[i]); 741 } 742 UserModel[] otherUsers = getUsersWithProjectPermission(projectId, new int[] {UserUtilities.PERMISSION_EDIT_USERS, UserUtilities.PERMISSION_ASSIGNABLE}, true, true); 743 for(int i = 0; i < otherUsers.length; i++) { 744 users.add(otherUsers[i]); 745 } 746 747 if(issue != null) { 748 UserModel creator = getUser(issue.getCreatorId()); 750 if(UserUtilities.hasPermission(getUserPermissions(creator, 0), projectId, UserUtilities.PERMISSION_EDIT_USERS)) { 751 users.add(creator); 752 } 753 UserModel owner = getUser(issue.getOwnerId()); 754 if(owner != null) { 755 users.add(owner); 756 } 757 } else if(userId != null) { 758 UserModel creator = getUser(userId); 760 if(UserUtilities.hasPermission(getUserPermissions(creator, 0), projectId, UserUtilities.PERMISSION_EDIT_USERS)) { 761 users.add(creator); 762 } 763 } 764 765 int i = 0; 766 UserModel[] userArray = new UserModel[users.size()]; 767 for(Iterator iter = users.iterator(); iter.hasNext(); i++) { 768 userArray[i] = (UserModel) iter.next(); 769 } 770 return userArray; 771 } 772 773 public UserModel checkLogin(String login, Object authentication, int authType, int reqSource) throws AuthenticatorException { 774 try { 775 PluggableAuthenticator authenticator = (PluggableAuthenticator) authenticatorClass.newInstance(); 776 if(authenticator != null) { 777 HashMap values = new HashMap(); 778 values.put("userHandler", context.getEJBLocalObject()); 779 values.put("systemConfiguration", scHome.create()); 780 authenticator.initialize(values); 781 return authenticator.checkLogin(login, authentication, authType, (reqSource == 0 ? AuthenticationConstants.REQ_SOURCE_UNKNOWN : reqSource)); 782 } 783 784 Logger.logError("Unable to create new authenticator."); 785 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 786 } catch(IllegalAccessException iae) { 787 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 788 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 789 } catch(InstantiationException ie) { 790 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 791 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 792 } catch(ClassCastException cce) { 793 Logger.logError("Authenticator class " + authenticatorClassName + " does not extend the PluggableAuthenticator class."); 794 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 795 } catch(CreateException ce) { 796 Logger.logError("Error creating new SystemConfiguration. " + ce.getMessage()); 797 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 798 } 799 } 800 801 public boolean allowRegistration(UserModel user, Object authentication, int authType, int reqSource) throws AuthenticatorException { 802 try { 803 PluggableAuthenticator authenticator = (PluggableAuthenticator) authenticatorClass.newInstance(); 804 if(authenticator != null) { 805 HashMap values = new HashMap(); 806 values.put("userHandler", context.getEJBLocalObject()); 807 values.put("systemConfiguration", scHome.create()); 808 authenticator.initialize(values); 809 if(authenticator.allowProfileCreation(user, authentication, authType, (reqSource == 0 ? AuthenticationConstants.REQ_SOURCE_UNKNOWN : reqSource))) { 810 return authenticator.allowRegistration(user, authentication, authType, (reqSource == 0 ? AuthenticationConstants.REQ_SOURCE_UNKNOWN : reqSource)); 811 } 812 return false; 813 } 814 815 Logger.logError("Unable to create new authenticator."); 816 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 817 } catch(IllegalAccessException iae) { 818 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 819 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 820 } catch(InstantiationException ie) { 821 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 822 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 823 } catch(ClassCastException cce) { 824 Logger.logError("Authenticator class " + authenticatorClassName + " does not extend the PluggableAuthenticator class."); 825 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 826 } catch(CreateException ce) { 827 Logger.logError("Error creating new SystemConfiguration. " + ce.getMessage()); 828 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 829 } 830 } 831 832 public boolean allowProfileCreation(UserModel user, Object authentication, int authType, int reqSource) throws AuthenticatorException { 833 try { 834 PluggableAuthenticator authenticator = (PluggableAuthenticator) authenticatorClass.newInstance(); 835 if(authenticator != null) { 836 HashMap values = new HashMap(); 837 values.put("userHandler", context.getEJBLocalObject()); 838 values.put("systemConfiguration", scHome.create()); 839 authenticator.initialize(values); 840 return authenticator.allowProfileCreation(user, authentication, authType, (reqSource == 0 ? AuthenticationConstants.REQ_SOURCE_UNKNOWN : reqSource)); 841 } 842 843 Logger.logError("Unable to create new authenticator."); 844 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 845 } catch(IllegalAccessException iae) { 846 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 847 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 848 } catch(InstantiationException ie) { 849 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 850 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 851 } catch(ClassCastException cce) { 852 Logger.logError("Authenticator class " + authenticatorClassName + " does not extend the PluggableAuthenticator class."); 853 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 854 } catch(CreateException ce) { 855 Logger.logError("Error creating new SystemConfiguration. " + ce.getMessage()); 856 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 857 } 858 } 859 860 public boolean allowProfileUpdates(UserModel user, Object authentication, int authType, int reqSource) throws AuthenticatorException { 861 try { 862 PluggableAuthenticator authenticator = (PluggableAuthenticator) authenticatorClass.newInstance(); 863 if(authenticator != null) { 864 HashMap values = new HashMap(); 865 values.put("userHandler", context.getEJBLocalObject()); 866 values.put("systemConfiguration", scHome.create()); 867 authenticator.initialize(values); 868 return authenticator.allowProfileUpdates(user, authentication, authType, (reqSource == 0 ? AuthenticationConstants.REQ_SOURCE_UNKNOWN : reqSource)); 869 } 870 871 Logger.logError("Unable to create new authenticator."); 872 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 873 } catch(IllegalAccessException iae) { 874 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 875 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 876 } catch(InstantiationException ie) { 877 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 878 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 879 } catch(ClassCastException cce) { 880 Logger.logError("Authenticator class " + authenticatorClassName + " does not extend the PluggableAuthenticator class."); 881 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 882 } catch(CreateException ce) { 883 Logger.logError("Error creating new SystemConfiguration. " + ce.getMessage()); 884 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 885 } 886 } 887 888 public boolean allowPasswordUpdates(UserModel user, Object authentication, int authType, int reqSource) throws AuthenticatorException { 889 try { 890 PluggableAuthenticator authenticator = (PluggableAuthenticator) authenticatorClass.newInstance(); 891 if(authenticator != null) { 892 HashMap values = new HashMap(); 893 values.put("userHandler", context.getEJBLocalObject()); 894 values.put("systemConfiguration", scHome.create()); 895 authenticator.initialize(values); 896 return authenticator.allowPasswordUpdates(user, authentication, authType, (reqSource == 0 ? AuthenticationConstants.REQ_SOURCE_UNKNOWN : reqSource)); 897 } 898 899 Logger.logError("Unable to create new authenticator."); 900 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 901 } catch(IllegalAccessException iae) { 902 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 903 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 904 } catch(InstantiationException ie) { 905 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 906 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 907 } catch(ClassCastException cce) { 908 Logger.logError("Authenticator class " + authenticatorClassName + " does not extend the PluggableAuthenticator class."); 909 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 910 } catch(CreateException ce) { 911 Logger.logError("Error creating new SystemConfiguration. " + ce.getMessage()); 912 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 913 } 914 } 915 916 public boolean allowPermissionUpdates(UserModel user, Object authentication, int authType, int reqSource) throws AuthenticatorException { 917 try { 918 PluggableAuthenticator authenticator = (PluggableAuthenticator) authenticatorClass.newInstance(); 919 if(authenticator != null) { 920 HashMap values = new HashMap(); 921 values.put("userHandler", context.getEJBLocalObject()); 922 values.put("systemConfiguration", scHome.create()); 923 authenticator.initialize(values); 924 return authenticator.allowPermissionUpdates(user, authentication, authType, (reqSource == 0 ? AuthenticationConstants.REQ_SOURCE_UNKNOWN : reqSource)); 925 } 926 927 Logger.logError("Unable to create new authenticator."); 928 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 929 } catch(IllegalAccessException iae) { 930 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 931 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 932 } catch(InstantiationException ie) { 933 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 934 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 935 } catch(ClassCastException cce) { 936 Logger.logError("Authenticator class " + authenticatorClassName + " does not extend the PluggableAuthenticator class."); 937 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 938 } catch(CreateException ce) { 939 Logger.logError("Error creating new SystemConfiguration. " + ce.getMessage()); 940 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 941 } 942 } 943 944 public boolean allowPreferenceUpdates(UserModel user, Object authentication, int authType, int reqSource) throws AuthenticatorException { 945 try { 946 PluggableAuthenticator authenticator = (PluggableAuthenticator) authenticatorClass.newInstance(); 947 if(authenticator != null) { 948 HashMap values = new HashMap(); 949 values.put("userHandler", context.getEJBLocalObject()); 950 values.put("systemConfiguration", scHome.create()); 951 authenticator.initialize(values); 952 return authenticator.allowPreferenceUpdates(user, authentication, authType, (reqSource == 0 ? AuthenticationConstants.REQ_SOURCE_UNKNOWN : reqSource)); 953 } 954 955 Logger.logError("Unable to create new authenticator."); 956 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 957 } catch(IllegalAccessException iae) { 958 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 959 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 960 } catch(InstantiationException ie) { 961 Logger.logError("Authenticator class " + authenticatorClassName + " can not be instantiated."); 962 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 963 } catch(ClassCastException cce) { 964 Logger.logError("Authenticator class " + authenticatorClassName + " does not extend the PluggableAuthenticator class."); 965 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 966 } catch(CreateException ce) { 967 Logger.logError("Error creating new SystemConfiguration. " + ce.getMessage()); 968 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR); 969 } 970 } 971 972 public void sendNotification(String login, String email, String baseURL) { 973 try { 974 QueueConnectionFactory factory = (QueueConnectionFactory) ic.lookup("java:comp/env/" + notificationFactoryName); 975 Queue notificationQueue = (Queue) ic.lookup("java:comp/env/" + notificationQueueName); 976 QueueConnection connect = factory.createQueueConnection(); 977 QueueSession session = connect.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); 978 979 QueueSender sender = session.createSender(notificationQueue); 980 981 MapMessage message = session.createMapMessage(); 982 message.setInt("type", NotificationUtilities.TYPE_SELF_REGISTER); 983 if(systemBaseURL != null && ! systemBaseURL.equals("")) { 984 message.setString("baseURL", systemBaseURL); 985 } else if(baseURL != null) { 986 message.setString("baseURL", baseURL); 987 } 988 message.setString("toAddress", email); 989 message.setString("login", login); 990 991 sender.send(message); 992 } catch(NamingException ne) { 993 Logger.logError("Error looking up ConnectionFactory/Queue " + notificationFactoryName + "/" + notificationQueueName + ".", ne); 994 } catch(JMSException jmse) { 995 Logger.logWarn("Error sending notification message", jmse); 996 } 997 } 998 999 public void ejbCreate() { 1000 try { 1001 ic = new InitialContext(); 1002 Object idRef = ic.lookup("java:comp/env/" + IDGenerator.JNDI_NAME); 1003 idHome = (IDGeneratorHome) PortableRemoteObject.narrow(idRef, IDGeneratorHome.class); 1004 1005 Object phRef = ic.lookup("java:comp/env/" + ProjectHandler.JNDI_NAME); 1006 phHome = (ProjectHandlerHome) PortableRemoteObject.narrow(phRef, ProjectHandlerHome.class); 1007 1008 nHome = (NotificationLocalHome) ic.lookup("java:comp/env/" + NotificationLocal.JNDI_NAME); 1009 pHome = (PermissionLocalHome) ic.lookup("java:comp/env/" + PermissionLocal.JNDI_NAME); 1010 projectHome = (ProjectLocalHome) ic.lookup("java:comp/env/" + ProjectLocal.JNDI_NAME); 1011 uHome = (UserLocalHome) ic.lookup("java:comp/env/" + UserLocal.JNDI_NAME); 1012 upHome = (UserPreferencesLocalHome) ic.lookup("java:comp/env/" + UserPreferencesLocal.JNDI_NAME); 1013 1014 Object scRef = ic.lookup("java:comp/env/" + SystemConfiguration.JNDI_NAME); 1015 scHome = (SystemConfigurationHome) PortableRemoteObject.narrow(scRef, SystemConfigurationHome.class); 1016 SystemConfiguration sc = scHome.create(); 1017 allowSelfRegister = sc.getBooleanProperty("allow_self_register", false); 1018 systemBaseURL = sc.getProperty("system_base_url", ""); 1019 1020 authenticatorClassName = sc.getProperty("authenticator_class", DEFAULT_AUTHENTICATOR); 1021 authenticatorClass = Class.forName(authenticatorClassName); 1022 } catch(NamingException ne) { 1023 Logger.logError("Exception while looking up home interfaces.", ne); 1024 } catch(CreateException ce) { 1025 Logger.logError("Exception while accessing application properties.", ce); 1026 } catch(ClassNotFoundException cnfe) { 1027 Logger.logError("Exception while creating authenticator class.", cnfe); 1028 } 1029 } 1030 1031 public void setSessionContext(SessionContext value) { 1032 context = value; 1033 } 1034 1035 public void ejbActivate() {} 1036 public void ejbPassivate() {} 1037 public void ejbRemove() {} 1038} 1039 | Popular Tags |