1 64 65 70 package com.jcorporate.expresso.services.controller; 71 72 import com.jcorporate.expresso.core.controller.ControllerException; 73 import com.jcorporate.expresso.core.controller.ControllerRequest; 74 import com.jcorporate.expresso.core.controller.ControllerResponse; 75 import com.jcorporate.expresso.core.controller.ServletControllerRequest; 76 import com.jcorporate.expresso.core.db.DBException; 77 import com.jcorporate.expresso.core.dbobj.DBObject; 78 import com.jcorporate.expresso.core.dbobj.SecuredDBObject; 79 import com.jcorporate.expresso.core.misc.ConfigManager; 80 import com.jcorporate.expresso.core.misc.StringUtil; 81 import com.jcorporate.expresso.core.security.DelayThread; 82 import com.jcorporate.expresso.core.security.User; 83 import com.jcorporate.expresso.services.dbobj.RegistrationDomain; 84 import com.jcorporate.expresso.services.dbobj.RegistrationObjectMap; 85 import com.jcorporate.expresso.services.validation.AuthValidationException; 86 import com.jcorporate.expresso.services.validation.ValidationEntry; 87 import org.apache.log4j.Logger; 88 89 import javax.servlet.http.HttpServletRequest ; 90 import java.text.NumberFormat ; 91 import java.util.ArrayList ; 92 import java.util.Iterator ; 93 94 106 107 public abstract class Registration 108 extends com.jcorporate.expresso.core.controller.DBController { 109 110 private static Logger log = Logger.getLogger(Registration.class.getName()); 111 112 public Registration() { 113 super(); 114 this.setSchema(com.jcorporate.expresso.core.ExpressoSchema.class); 115 } 116 117 118 125 protected User getRegUser(ControllerRequest request) 126 throws ControllerException { 127 128 String loginName = request.getUser(); 129 130 if (loginName.equals("")) { 131 throw new ControllerException("You are not logged-in"); 132 } 133 134 User user = null; 135 136 try { 137 user = new User(); 138 user.setDataContext(request.getDataContext()); 139 user.setLoginName(loginName); 140 141 if (!user.find()) { 142 throw new ControllerException("Account \"" + loginName + 143 "\" not found"); 144 } 145 146 if (user.getAccountStatus().equals("D")) { 147 throw new ControllerException("Account \"" + loginName + 148 "\" has been disabled"); 149 } else if (user.getAccountStatus().equals("I")) { 150 throw new ControllerException("Account \"" + loginName + 151 "\" has not been activated yet"); 152 } 153 } catch (DBException dbe) { 154 throw new ControllerException("Database access error for login \"" + 155 loginName + "\"", dbe); 156 } 157 158 return user; 159 } 160 161 162 170 protected boolean checkRegComplete(ControllerRequest request, int uid) 171 throws DBException, ControllerException { 172 User myUser = new User(); 173 myUser.setDataContext(request.getDataContext()); 174 myUser.setUid(uid); 175 myUser.retrieve(); 176 177 int regDomId = 1; 178 RegistrationDomain dom = new RegistrationDomain(); 179 dom.setDataContext(request.getDataContext()); 180 dom.setField("Name", myUser.getRegistrationDomain()); 181 182 if (dom.find()) { 183 regDomId = dom.getFieldInt("RegDomId"); 184 } 185 186 RegistrationObjectMap rm = new RegistrationObjectMap(); 187 rm.setDataContext(request.getDataContext()); 188 rm.setField("RegDomId", regDomId); 189 190 boolean regComplete = true; 191 RegistrationObjectMap oneMap = null; 192 193 for (Iterator i = rm.searchAndRetrieveList().iterator(); i.hasNext();) { 194 oneMap = (RegistrationObjectMap) i.next(); 195 196 int min = oneMap.getFieldInt("RecMin"); 197 SecuredDBObject oneObj = loadDBObject(request, 198 oneMap.getField("RegObj")); 199 oneObj.setField(oneMap.getField("UidField"), uid); 200 201 if (oneObj.count() < min) { 202 regComplete = false; 203 break; 204 } 205 } 206 207 myUser.setRegComplete(regComplete); 208 myUser.update(); 209 210 return regComplete; 211 } 212 213 220 protected void delayLogin() { 221 DelayThread.delay(); 222 } 223 224 225 234 protected String displayValue(String fieldType, String fieldValue) 235 throws ControllerException { 236 try { 237 if (fieldType.equalsIgnoreCase("money")) { 238 if (!fieldValue.equals("")) { 239 return NumberFormat.getCurrencyInstance().format(new Double (fieldValue).doubleValue()); 240 } 241 } else { 242 return fieldValue; 243 } 244 } catch (NumberFormatException ne) { 245 throw new ControllerException("Number for field not in a " + 246 "valid numeric format:" + 247 fieldValue, ne); 248 } 249 250 return null; 251 } 252 253 254 260 public String getDB(ControllerRequest request) 261 throws ControllerException { 262 String dbobj = StringUtil.notNull(request.getParameter("dbobj")); 263 264 if (dbobj.equals("")) { 265 throw new ControllerException("dbobj parameter was not specified"); 266 } 267 268 return dbobj; 269 } 270 271 277 protected String getDefaultValue(String fieldName) { 278 return null; 279 } 280 281 282 290 protected RegistrationDomain getRegDomain(ControllerRequest request, 291 User user) 292 throws ControllerException { 293 RegistrationDomain rd = null; 294 295 try { 296 String domain = user.getRegistrationDomain(); 297 rd = new RegistrationDomain(); 298 rd.setDataContext(request.getDataContext()); 299 rd.setField("Name", domain); 300 301 if (!rd.find()) { 302 throw new ControllerException("Domain " + domain + 303 " not created yet"); 304 } 305 } catch (DBException dbe) { 306 throw new ControllerException("Database error", dbe); 307 } 308 309 return rd; 310 } 311 312 313 320 protected SecuredDBObject loadDBObject(ControllerRequest request, 321 String dbobj) 322 throws ControllerException { 323 SecuredDBObject db = null; 324 325 try { 326 db = (SecuredDBObject) Class.forName(dbobj).newInstance(); 327 db.setRequestingUid(SecuredDBObject.SYSTEM_ACCOUNT); 328 db.setDataContext(request.getDataContext()); 329 db.setLocale(request.getLocale()); 330 } catch (Exception e) { 331 throw new ControllerException("Instantiate failed for database object " + dbobj, e); 332 } 333 334 return db; 335 } 336 337 338 346 public String nextToAdd(ControllerRequest request) 347 throws ControllerException { 348 if (log.isDebugEnabled()) { 349 log.debug("Checking if user '" + request.getUser() + 350 "' is fully registered, db '" + request.getDataContext() + 351 "'"); 352 } 353 try { 354 User user = new User(); 355 user.setDataContext(request.getDataContext()); 356 user.setUid(request.getUid()); 357 user.retrieve(); 358 if (log.isDebugEnabled()) { 359 log.debug("Checking registration for used id '" + user.getUid() + 360 "'"); 361 } 362 363 RegistrationDomain rd = new RegistrationDomain(); 364 rd.setDataContext(request.getDataContext()); 365 rd.setField("Name", user.getRegistrationDomain()); 366 367 if (!rd.find()) { 368 throw new ControllerException("Domain " + 369 user.getRegistrationDomain() + 370 " not created yet"); 371 } 372 373 RegistrationObjectMap rom = new RegistrationObjectMap(); 374 rom.setDataContext(request.getDataContext()); 375 rom.setField("RegDomId", rd.getField("RegDomId")); 376 377 RegistrationObjectMap oneRom = null; 378 379 for (Iterator e = rom.searchAndRetrieveList("RegOrder").iterator(); 380 e.hasNext();) { 381 oneRom = (RegistrationObjectMap) e.next(); 382 383 384 DBObject db = isRegistrationObjectNeeded(request, user, oneRom); 385 386 if (db != null) { 387 return db.getClass().getName(); 388 } 389 } 390 } catch (DBException de) { 391 throw new ControllerException(de); 392 } 393 394 return null; 395 } 396 397 398 406 protected DBObject isRegistrationObjectNeeded(ControllerRequest request, 407 User user, 408 RegistrationObjectMap rom) 409 throws DBException { 410 411 int min; 412 int records = 0; 413 414 DBObject returnObject = null; 415 rom.setDataContext(request.getDataContext()); 416 min = rom.getFieldInt("RecMin"); 417 418 419 if (log.isDebugEnabled()) { 420 log.debug("Need at least " + min + " records for '" + 421 rom.getField("RegObj") + "'"); 422 } 423 424 try { 425 returnObject = (DBObject) Class.forName(rom.getField("RegObj")).newInstance(); 426 } catch (Exception ex) { 427 log.error("isRegistrationObjectNeeded() Dynanamic load failed.", ex); 428 throw new DBException("Dynamic load failed for " + 429 rom.getField("RegObj"), 430 ex); 431 } 432 433 returnObject.setDataContext(request.getDataContext()); 434 435 if (user != null) { 436 String fieldName = rom.getField("UidField"); 437 returnObject.setField(fieldName, user.getUid()); 438 if (log.isDebugEnabled()) { 439 log.debug("Looking for '" + returnObject.getClass().getName() + 440 "' record with " + fieldName + " = " + 441 user.getUid() + " in db " + request.getDataContext()); 442 } 443 444 records = returnObject.count(); 445 if (log.isDebugEnabled()) { 446 log.debug("Found " + records); 447 } 448 } 449 450 if (records < min) { 451 if (log.isDebugEnabled()) { 452 log.debug("There were '" + records + "' records, but " + 453 min + " are required for '" + 454 returnObject.getClass().getName() + "'"); 455 } 456 457 return returnObject; 458 459 } else { 460 if (log.isDebugEnabled()) { 461 log.debug("There were '" + records + "' records for " + 462 returnObject.getClass().getName() + 463 ", so we don't need any more."); 464 } 465 466 return null; 467 } 468 } 469 470 471 483 protected DBObject[] getRequiredDBObjects(ControllerRequest request, 484 RegistrationDomain rd, 485 User user) 486 throws DBException { 487 488 ArrayList dbobjects = new ArrayList (); 489 490 if (log.isDebugEnabled()) { 491 log.debug("Getting the registration objects that user '" + request.getUser() + 492 "' needs, db='" + request.getDataContext() + 493 "'"); 494 } 495 496 506 510 if (!rd.find()) { 511 throw new DBException("Domain " + 512 user.getRegistrationDomain() + 513 " not created yet"); 514 } 515 516 RegistrationObjectMap rom = new RegistrationObjectMap(SecuredDBObject.SYSTEM_ACCOUNT); 517 rom.setDataContext(request.getDataContext()); 518 rom.setField("RegDomId", rd.getField("RegDomId")); 519 520 RegistrationObjectMap oneRom = null; 521 522 for (Iterator e = rom.searchAndRetrieveList("RegOrder").iterator(); 523 e.hasNext();) { 524 oneRom = (RegistrationObjectMap) e.next(); 525 526 527 DBObject db = isRegistrationObjectNeeded(request, user, oneRom); 528 529 if (db != null) { 530 if (user != null) { 531 db.setField(oneRom.getField("UidField"), user.getUid()); 532 } 533 dbobjects.add(db); 534 535 } 536 } 537 538 return (DBObject[]) dbobjects.toArray(new DBObject[dbobjects.size()]); 539 } 540 541 542 551 protected String getLoginController(ControllerRequest request) { 552 final String defaultController = com.jcorporate.expresso.services.controller 553 .LoginController.class.getName(); 554 boolean defaultValue = false; 555 String controller = request.getParameter("login"); 556 if (controller == null) { 557 defaultValue = true; 558 try { 559 controller = this.getSchemaInstance() 560 .getLoginController().getClass().getName(); 561 } catch (ControllerException ex) { 562 controller = defaultController; 563 } 564 } 565 566 if (!defaultValue) { 570 try { 571 ConfigManager.getControllerFactory() 572 .getController(controller); 573 } catch (Exception e) { 574 log.error("Unable to instantiate login controller: " 575 + controller + 576 " . Using default login controller instead", e); 577 578 controller = defaultController; 579 } 580 } 581 582 return controller; 583 } 584 585 596 protected void setupEmailValidation(ControllerRequest request, 597 ControllerResponse response, User user, 598 RegistrationDomain rd, 599 String loginControllerName) 600 throws DBException, ControllerException { 601 602 String emailAuthCode = user.getEmailAuthCode(); 603 String loginName = user.getLoginName(); 604 user.setEmailValCode(emailAuthCode); 605 user.update(); 606 607 HttpServletRequest hreq = (HttpServletRequest ) ((ServletControllerRequest) request).getServletRequest(); 608 609 try { 610 611 ValidationEntry ve = new ValidationEntry(request.getDataContext()); 612 ve.expiresAfter(72, 0, 0); ve.setValidationHandler(com.jcorporate.expresso.services.validation.LoginEmailValidator.class.getName()); 614 ve.setTitle("Registration Email Validation"); 615 ve.setDesc("user=" + loginName + ", db=" + request.getDataContext()); 616 ve.setServer(hreq.getServerName()); 617 ve.setPort(Integer.toString(hreq.getServerPort())); 618 ve.setContextPath(hreq.getContextPath()); 619 ve.addParam("db", request.getDataContext()); 620 ve.addParam("UserName", loginName); 621 ve.addParam("RegistrationController", this.getClass().getName()); 622 ve.addParam("LoginController", loginControllerName); 623 ve.submit(); 624 } catch (AuthValidationException avex) { 625 delayLogin(); 626 throw new ControllerException("Validation framework exception", 627 avex); 628 } 629 } 630 631 632 } | Popular Tags |