1 23 24 package org.infoglue.cms.controllers.kernel.impl.simple; 25 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 31 import org.apache.log4j.Logger; 32 import org.exolab.castor.jdo.Database; 33 import org.exolab.castor.jdo.OQLQuery; 34 import org.exolab.castor.jdo.QueryResults; 35 import org.infoglue.cms.entities.kernel.BaseEntityVO; 36 import org.infoglue.cms.entities.management.Group; 37 import org.infoglue.cms.entities.management.Role; 38 import org.infoglue.cms.entities.management.SystemUser; 39 import org.infoglue.cms.entities.management.SystemUserVO; 40 import org.infoglue.cms.entities.management.impl.simple.SystemUserImpl; 41 import org.infoglue.cms.exception.Bug; 42 import org.infoglue.cms.exception.ConstraintException; 43 import org.infoglue.cms.exception.SystemException; 44 import org.infoglue.cms.util.CmsPropertyHandler; 45 import org.infoglue.cms.util.ConstraintExceptionBuffer; 46 import org.infoglue.cms.util.PasswordGenerator; 47 import org.infoglue.cms.util.mail.MailServiceFactory; 48 49 57 public class SystemUserController extends BaseController 58 { 59 private final static Logger logger = Logger.getLogger(SystemUserController.class.getName()); 60 61 64 65 public static SystemUserController getController() 66 { 67 return new SystemUserController(); 68 } 69 70 81 82 public SystemUserVO getSystemUserVOWithName(String name) throws SystemException, Bug 83 { 84 SystemUserVO systemUserVO = null; 85 Database db = CastorDatabaseService.getDatabase(); 86 87 try 88 { 89 beginTransaction(db); 90 91 systemUserVO = getReadOnlySystemUserWithName(name, db).getValueObject(); 92 93 commitTransaction(db); 94 } 95 catch (Exception e) 96 { 97 logger.info("An error occurred so we should not complete the transaction:" + e); 98 rollbackTransaction(db); 99 throw new SystemException(e.getMessage()); 100 } 101 102 return systemUserVO; 103 } 104 105 106 107 110 111 public SystemUser getReadOnlySystemUserWithName(String userName, Database db) throws SystemException, Bug 112 { 113 SystemUser systemUser = null; 114 OQLQuery oql; 115 try 116 { 117 oql = db.getOQLQuery( "SELECT u FROM org.infoglue.cms.entities.management.impl.simple.SystemUserImpl u WHERE u.userName = $1"); 118 oql.bind(userName); 119 120 QueryResults results = oql.execute(Database.ReadOnly); 121 122 if (results.hasMore()) 123 { 124 systemUser = (SystemUser)results.next(); 125 logger.info("found one:" + systemUser.getFirstName()); 126 } 127 } 128 catch(Exception e) 129 { 130 throw new SystemException("An error occurred when we tried to fetch " + userName + " Reason:" + e.getMessage(), e); 131 } 132 133 return systemUser; 134 } 135 136 137 140 141 public SystemUser getSystemUserWithName(String userName, Database db) throws SystemException, Bug 142 { 143 SystemUser systemUser = null; 144 OQLQuery oql; 145 try 146 { 147 oql = db.getOQLQuery( "SELECT u FROM org.infoglue.cms.entities.management.impl.simple.SystemUserImpl u WHERE u.userName = $1"); 148 oql.bind(userName); 149 150 QueryResults results = oql.execute(); 151 this.logger.info("Fetching entity in read/write mode" + userName); 152 153 if (results.hasMore()) 154 { 155 systemUser = (SystemUser)results.next(); 156 logger.info("found one:" + systemUser.getFirstName()); 157 } 158 } 159 catch(Exception e) 160 { 161 throw new SystemException("An error occurred when we tried to fetch " + userName + " Reason:" + e.getMessage(), e); 162 } 163 164 return systemUser; 165 } 166 167 168 169 public SystemUserVO getSystemUserVO(String userName, String password) throws SystemException, Bug 170 { 171 SystemUserVO systemUserVO = null; 172 173 Database db = CastorDatabaseService.getDatabase(); 174 175 try 176 { 177 beginTransaction(db); 178 179 systemUserVO = getSystemUserVO(db, userName, password); 180 181 commitTransaction(db); 182 } 183 catch (Exception e) 184 { 185 logger.info("An error occurred so we should not complete the transaction:" + e); 186 rollbackTransaction(db); 187 throw new SystemException(e.getMessage()); 188 } 189 190 return systemUserVO; 191 } 192 193 194 public SystemUserVO getSystemUserVO(Database db, String userName, String password) throws SystemException, Exception 195 { 196 SystemUserVO systemUserVO = null; 197 198 OQLQuery oql = db.getOQLQuery( "SELECT u FROM org.infoglue.cms.entities.management.impl.simple.SystemUserImpl u WHERE u.userName = $1 AND u.password = $2"); 199 oql.bind(userName); 200 oql.bind(password); 201 202 QueryResults results = oql.execute(Database.ReadOnly); 203 204 if (results.hasMore()) 205 { 206 SystemUser systemUser = (SystemUser)results.next(); 207 systemUserVO = systemUser.getValueObject(); 208 logger.info("found one:" + systemUserVO.getFirstName()); 209 } 210 211 results.close(); 212 oql.close(); 213 214 return systemUserVO; 215 } 216 217 public SystemUser getSystemUser(Database db, String userName, String password) throws SystemException, Exception 218 { 219 SystemUser systemUser = null; 220 221 OQLQuery oql = db.getOQLQuery( "SELECT u FROM org.infoglue.cms.entities.management.impl.simple.SystemUserImpl u WHERE u.userName = $1 AND u.password = $2"); 222 oql.bind(userName); 223 oql.bind(password); 224 225 QueryResults results = oql.execute(); 226 this.logger.info("Fetching entity in read/write mode" + userName); 227 228 if (results.hasMore()) 229 { 230 systemUser = (SystemUser)results.next(); 231 logger.info("found one:" + systemUser.getFirstName()); 232 } 233 234 results.close(); 235 oql.close(); 236 237 return systemUser; 238 } 239 240 public List getSystemUserVOList() throws SystemException, Bug 241 { 242 return getAllVOObjects(SystemUserImpl.class, "userName"); 243 } 244 245 public List getSystemUserVOList(Database db) throws SystemException, Bug 246 { 247 return getAllVOObjects(SystemUserImpl.class, "userName", db); 248 } 249 250 251 public List getFilteredSystemUserVOList(String firstName, String lastName, String userName, String email, String [] roleNames) throws SystemException, Bug 252 { 253 List filteredList = new ArrayList (); 254 255 Database db = CastorDatabaseService.getDatabase(); 256 257 try 258 { 259 beginTransaction(db); 260 261 filteredList = getFilteredSystemUserList(firstName, lastName, userName, email, roleNames, db); 262 263 commitTransaction(db); 264 } 265 catch (Exception e) 266 { 267 logger.info("An error occurred so we should not complete the transaction:" + e); 268 rollbackTransaction(db); 269 throw new SystemException("An error occurred so we should not complete the transaction:" + e, e); 270 } 271 272 return toVOList(filteredList); 273 } 274 275 public List getFilteredSystemUserList(String firstName, String lastName, String userName, String email, String [] roleNames, Database db) throws SystemException, Bug, Exception 276 { 277 List filteredList = new ArrayList (); 278 279 OQLQuery oql = db.getOQLQuery( "SELECT u FROM org.infoglue.cms.entities.management.impl.simple.SystemUserImpl u"); 280 281 QueryResults results = oql.execute(Database.ReadOnly); 282 283 while (results.hasMore()) 284 { 285 SystemUser extranetUser = (SystemUser)results.next(); 286 boolean include = true; 287 288 if(firstName != null && !firstName.equals("") && extranetUser.getFirstName().toLowerCase().indexOf(firstName.toLowerCase()) == -1) 289 include = false; 290 291 if(lastName != null && !lastName.equals("") && extranetUser.getLastName().toLowerCase().indexOf(lastName.toLowerCase()) == -1) 292 include = false; 293 294 if(userName != null && !userName.equals("") && extranetUser.getUserName().toLowerCase().indexOf(userName.toLowerCase()) == -1) 295 include = false; 296 297 if(email != null && !email.equals("") && extranetUser.getEmail().toLowerCase().indexOf(email.toLowerCase()) == -1) 298 include = false; 299 300 boolean hasRoles = true; 301 if(roleNames != null && roleNames.length > 0) 302 { 303 for(int i=0; i < roleNames.length; i++) 304 { 305 String roleName = roleNames[i]; 306 if(roleName != null && !roleName.equals("")) 307 { 308 Collection roles = extranetUser.getRoles(); 309 Iterator rolesIterator = roles.iterator(); 310 boolean hasRole = false; 311 while(rolesIterator.hasNext()) 312 { 313 Role role = (Role)rolesIterator.next(); 314 if(role.getRoleName().equalsIgnoreCase(roleName)) 315 { 316 hasRole = true; 317 break; 318 } 319 } 320 321 if(!hasRole) 322 { 323 hasRoles = false; 324 break; 325 } 326 } 327 } 328 } 329 330 if(include && hasRoles) 331 filteredList.add(extranetUser); 332 } 333 334 results.close(); 335 oql.close(); 336 337 return filteredList; 338 } 339 340 344 public SystemUserVO create(SystemUserVO systemUserVO) throws ConstraintException, SystemException 345 { 346 SystemUser systemUser = new SystemUserImpl(); 347 systemUser.setValueObject(systemUserVO); 348 systemUser = (SystemUser) createEntity(systemUser); 349 return systemUser.getValueObject(); 350 } 351 352 356 public SystemUser create(SystemUserVO systemUserVO, Database db) throws ConstraintException, SystemException, Exception 357 { 358 SystemUser systemUser = new SystemUserImpl(); 359 systemUser.setValueObject(systemUserVO); 360 systemUser = (SystemUser) createEntity(systemUser, db); 361 return systemUser; 362 } 363 364 368 369 public void delete(String userName) throws ConstraintException, SystemException 370 { 371 Database db = CastorDatabaseService.getDatabase(); 372 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 373 374 SystemUser systemUser = null; 375 376 beginTransaction(db); 377 378 try 379 { 380 382 delete(userName, db); 383 384 ceb.throwIfNotEmpty(); 386 387 commitTransaction(db); 388 } 389 catch(ConstraintException ce) 390 { 391 logger.warn("An error occurred so we should not completes the transaction:" + ce, ce); 392 rollbackTransaction(db); 393 throw ce; 394 } 395 catch(Exception e) 396 { 397 logger.error("An error occurred so we should not completes the transaction:" + e, e); 398 rollbackTransaction(db); 399 throw new SystemException(e.getMessage()); 400 } 401 402 } 403 404 408 409 public void delete(String userName, Database db) throws ConstraintException, SystemException, Exception 410 { 411 SystemUser systemUser = getSystemUserWithName(userName, db); 412 413 Collection roles = systemUser.getRoles(); 414 Iterator rolesIterator = roles.iterator(); 415 while(rolesIterator.hasNext()) 416 { 417 Role role = (Role)rolesIterator.next(); 418 role.getSystemUsers().remove(systemUser); 419 } 420 421 Collection groups = systemUser.getGroups(); 422 Iterator groupsIterator = groups.iterator(); 423 while(groupsIterator.hasNext()) 424 { 425 Group group = (Group)groupsIterator.next(); 426 group.getSystemUsers().remove(systemUser); 427 } 428 429 db.remove(systemUser); 430 } 431 432 433 public SystemUserVO update(SystemUserVO systemUserVO) throws ConstraintException, SystemException 434 { 435 return (SystemUserVO) updateEntity(SystemUserImpl.class, (BaseEntityVO) systemUserVO); 436 } 437 438 439 public SystemUserVO update(SystemUserVO systemUserVO, String [] roleNames, String [] groupNames) throws ConstraintException, SystemException 440 { 441 Database db = CastorDatabaseService.getDatabase(); 442 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 443 444 SystemUser systemUser = null; 445 446 beginTransaction(db); 447 448 try 449 { 450 systemUser = update(systemUserVO, roleNames, groupNames, db); 451 452 commitTransaction(db); 453 } 454 catch(ConstraintException ce) 455 { 456 logger.warn("An error occurred so we should not completes the transaction:" + ce, ce); 457 rollbackTransaction(db); 458 throw ce; 459 } 460 catch(Exception e) 461 { 462 logger.error("An error occurred so we should not completes the transaction:" + e, e); 463 rollbackTransaction(db); 464 throw new SystemException(e.getMessage()); 465 } 466 467 return systemUser.getValueObject(); 468 } 469 470 public SystemUser update(SystemUserVO systemUserVO, String [] roleNames, String [] groupNames, Database db) throws ConstraintException, SystemException 471 { 472 SystemUser systemUser = systemUser = getSystemUserWithName(systemUserVO.getUserName(), db); 473 systemUser.getRoles().clear(); 474 475 if(roleNames != null) 476 { 477 for (int i=0; i < roleNames.length; i++) 478 { 479 Role role = RoleController.getController().getRoleWithName(roleNames[i], db); 480 systemUser.getRoles().add(role); 481 role.getSystemUsers().add(systemUser); 482 } 483 } 484 485 systemUser.getGroups().clear(); 486 487 if(groupNames != null) 488 { 489 for (int i=0; i < groupNames.length; i++) 490 { 491 Group group = GroupController.getController().getGroupWithName(groupNames[i], db); 492 systemUser.getGroups().add(group); 493 group.getSystemUsers().add(systemUser); 494 } 495 } 496 497 systemUserVO.setPassword(systemUser.getPassword()); 498 systemUser.setValueObject(systemUserVO); 499 500 return systemUser; 501 } 502 503 public void updatePassword(String userName) throws ConstraintException, SystemException 504 { 505 Database db = CastorDatabaseService.getDatabase(); 506 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 507 508 beginTransaction(db); 509 510 try 511 { 512 updatePassword(userName, db); 513 514 commitTransaction(db); 515 } 516 catch(Exception e) 517 { 518 logger.error("An error occurred so we should not completes the transaction:" + e, e); 519 rollbackTransaction(db); 520 throw new SystemException(e.getMessage()); 521 } 522 } 523 524 public void updatePassword(String userName, Database db) throws ConstraintException, SystemException 525 { 526 SystemUser systemUser = getSystemUserWithName(userName, db); 527 528 String newPassword = PasswordGenerator.generate(); 529 530 systemUser.setPassword(newPassword); 531 532 StringBuffer sb = new StringBuffer (); 533 sb.append("CMS notification: You or an administrator have requested a new password for your account (" + userName + "). \n"); 534 sb.append("\n"); 535 sb.append("The new password is '" + newPassword + "'.\n"); 536 sb.append("\n"); 537 sb.append("Please notify the administrator if this does not work. \n"); 538 sb.append("\n"); 539 sb.append("-----------------------------------------------------------------------\n"); 540 sb.append("This email was automatically generated and the sender is the CMS-system. \n"); 541 sb.append("Do not reply to this email. \n"); 542 543 try 544 { 545 String systemEmailSender = CmsPropertyHandler.getSystemEmailSender(); 546 if(systemEmailSender == null || systemEmailSender.equalsIgnoreCase("")) 547 systemEmailSender = "InfoGlueCMS@" + CmsPropertyHandler.getMailSmtpHost(); 548 549 MailServiceFactory.getService().send(systemEmailSender, systemUser.getEmail(), null, "InfoGlue Information - Password changed!!", sb.toString()); 550 } 551 catch(Exception e) 552 { 553 logger.error("The notification was not sent. Reason:" + e.getMessage(), e); 554 } 555 } 556 557 public void updatePassword(String userName, String oldPassword, String newPassword) throws ConstraintException, SystemException 558 { 559 Database db = CastorDatabaseService.getDatabase(); 560 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 561 562 beginTransaction(db); 563 564 try 565 { 566 ceb.throwIfNotEmpty(); 568 569 updatePassword(userName, oldPassword, newPassword, db); 570 571 commitTransaction(db); 572 } 573 catch(ConstraintException ce) 574 { 575 rollbackTransaction(db); 576 throw ce; 577 } 578 catch(Exception e) 579 { 580 logger.error("An error occurred so we should not completes the transaction:" + e, e); 581 rollbackTransaction(db); 582 throw new SystemException(e.getMessage()); 583 } 584 } 585 586 public void updatePassword(String userName, String oldPassword, String newPassword, Database db) throws ConstraintException, SystemException, Exception 587 { 588 if(newPassword == null) 589 throw new ConstraintException("SystemUser.newPassword", "301"); 590 591 SystemUser systemUser = getSystemUser(db, userName, oldPassword); 592 if(systemUser == null) 593 throw new ConstraintException("SystemUser.oldPassword", "310"); 594 595 systemUser.setPassword(newPassword); 596 } 597 598 599 603 604 public BaseEntityVO getNewVO() 605 { 606 return new SystemUserVO(); 607 } 608 609 } 610 611 | Popular Tags |