1 53 54 106 107 package com.Yasna.forum.tags; 108 109 import java.util.*; 110 import javax.servlet.*; 111 import javax.servlet.jsp.*; 112 import javax.servlet.jsp.tagext.*; 113 import javax.servlet.http.*; 114 import com.Yasna.forum.*; 115 import com.Yasna.forum.tags.*; 116 117 198 199 public class CreateTag extends TagSupport implements YazdProperty 200 { 201 private Authorization auth = null; 202 private YazdState js = null; 203 private YazdRequest jr = null; 204 private User user = null; 205 private String email = null; 207 private String username = null; 208 private String password = null; 209 private String confirm = null; 211 private String name = null; 212 private String nameVisible = null; 213 private String emailVisible = null; 214 private Map properties = new HashMap(); 216 private Map required = new HashMap(); 218 private boolean confirm_password = false; 221 private boolean generate_password = false; 224 private boolean auto_login = false; 227 private boolean created = false; 229 230 237 238 public final int doStartTag() throws JspException 239 { 240 boolean extended_required = true; 241 242 js = (YazdState)pageContext.getAttribute("yazdUserState", 244 PageContext.SESSION_SCOPE); 245 if( js == null ) { 246 throw new JspException("Yazd create tag could not get yazd state."); 247 } 248 249 jr = (YazdRequest)pageContext.getAttribute("yazdUserRequest", 251 PageContext.REQUEST_SCOPE); 252 if( js == null ) { 253 throw new JspException("Yazd create tag could not get yazd request."); 254 } 255 256 pageContext.setAttribute(id,this,PageContext.PAGE_SCOPE); 258 259 ServletRequest req = pageContext.getRequest(); 262 Enumeration enume = TagPropertyManager.getUserPropertyNames(); 263 String tmp; 264 String prop; 265 String propname; 266 while( enume.hasMoreElements() ) { 267 prop = (String )enume.nextElement(); 268 if( prop.startsWith( "required." ) ) { 270 propname = prop.substring(9); 271 required.put(propname,TagPropertyManager.getUserProperty(prop)); 272 } else { 273 properties.put(prop,TagPropertyManager.getUserProperty(prop)); 274 } 275 } 276 277 tmp = req.getParameter("create"); 279 if( tmp == null || tmp.length() == 0 ) { 280 return SKIP_BODY; 283 } 284 285 287 email = req.getParameter("email"); 289 username = req.getParameter("username"); 290 password = req.getParameter("password"); 291 confirm = req.getParameter("confirm"); 292 name = req.getParameter("name"); 293 nameVisible = req.getParameter("nameVisible"); 294 emailVisible = req.getParameter("emailVisible"); 295 296 for( Iterator it=required.keySet().iterator(); it.hasNext(); ) { 298 prop = (String )it.next(); 299 if( (tmp = req.getParameter(prop)) != null && tmp.length() > 0 ) { 300 required.put(prop,tmp); 301 } else { 302 required.put(prop,""); 303 extended_required = false; 304 } 305 } 306 307 for( Iterator it=properties.keySet().iterator(); it.hasNext(); ) { 309 prop = (String )it.next(); 310 if( (tmp = req.getParameter(prop)) != null ) { 311 properties.put(prop,tmp); 312 } 313 } 314 315 if( email == null || email.length() == 0 || !extended_required || 317 username == null || username.length() == 0 || 318 ((password == null || password.length() == 0) && !generate_password) ) { 319 jr.addError(TagPropertyManager.getTagProperty("yazd.tag.create.required")); 320 return SKIP_BODY; 321 } 322 323 if( confirm_password && (confirm == null || !password.equals(confirm)) ) { 326 password = null; 327 confirm = null; 328 jr.addError(TagPropertyManager.getTagProperty("yazd.tag.create.confirm_password")); 329 return SKIP_BODY; 330 } 331 332 if( generate_password ) { 334 password = jr.GeneratePassword(); 335 properties.put("password",password); 336 } 337 338 ForumFactory ff = jr.getForumFactory(); 340 ProfileManager pm = jr.getProfileManager(); 341 try { 342 user = pm.createUser( username, password, email ); 343 } catch( UserAlreadyExistsException e ) { 344 jr.addError(TagPropertyManager.getTagProperty("yazd.tag.create.userexists")); 346 username = null; 347 return SKIP_BODY; 348 } 349 350 try { 352 if( name != null && name.length() > 0 ) { 354 user.setName( name ); 355 } 356 if( nameVisible != null && nameVisible.length() > 0) { 357 user.setNameVisible( true ); 358 } else { 359 user.setNameVisible( false ); 360 } 361 if( emailVisible != null && emailVisible.length() > 0 ) { 362 user.setEmailVisible( true ); 363 } else { 364 user.setEmailVisible( false ); 365 } 366 367 for( Iterator it=properties.keySet().iterator(); it.hasNext(); ) { 371 tmp = (String )it.next(); 372 user.setProperty( tmp, (String )properties.get(tmp)); 373 if( tmp.equals(YazdState.MESSAGE_DEPTH) ) 374 js.setMessageDepth(Integer.valueOf((String )properties.get(tmp)).intValue()); 375 if( tmp.equals(YazdState.THREAD_DEPTH) ) 376 js.setThreadDepth(Integer.valueOf((String )properties.get(tmp)).intValue()); 377 if( tmp.equals(YazdState.ITEMS_PER_PAGE) ) 378 js.setItemsPerPage(Integer.valueOf((String )properties.get(tmp)).intValue()); 379 } 380 381 for( Iterator it=required.keySet().iterator(); it.hasNext(); ) { 385 tmp = (String )it.next(); 386 user.setProperty( tmp, (String )required.get(tmp)); 387 if( tmp.equals(YazdState.MESSAGE_DEPTH) ) 388 js.setMessageDepth(Integer.valueOf((String )required.get(tmp)).intValue()); 389 if( tmp.equals(YazdState.THREAD_DEPTH) ) 390 js.setThreadDepth(Integer.valueOf((String )required.get(tmp)).intValue()); 391 if( tmp.equals(YazdState.ITEMS_PER_PAGE) ) 392 js.setItemsPerPage(Integer.valueOf((String )required.get(tmp)).intValue()); 393 } 394 auth = AuthorizationFactory.getAuthorization( username, password ); 396 } catch(UnauthorizedException e) { 397 throw new JspException("Create tag not authorized to set user values."); 398 } 399 400 created = true; 402 403 if( auto_login ) { 405 js.setAuthorization(auth); 407 js.setLoggedIn(true); 408 } 409 410 return EVAL_BODY_INCLUDE; 413 } 414 415 420 421 public final int doEndTag() throws JspException 422 { 423 if( created ) 424 return SKIP_PAGE; 425 return EVAL_PAGE; 426 } 427 428 432 public final void setConfirm(String name) 433 { 434 if( name.equals("true") ) 435 confirm_password = true; 436 } 437 438 442 public final void setPassword(String name) 443 { 444 if( name.equals("true") ) 445 generate_password = true; 446 } 447 448 453 public final void setLogin(String name) 454 { 455 if( name.equals("true") ) 456 auto_login = true; 457 } 458 459 462 468 public final String getEmail() 469 { 470 if( email == null ) 471 return ""; 472 return email; 473 } 474 475 481 public final String getUsername() 482 { 483 if( username == null ) 484 return ""; 485 return username; 486 } 487 488 495 public final String getPassword() 496 { 497 if( password == null ) 498 return ""; 499 return password; 500 } 501 502 509 public final String getConfirm() 510 { 511 if( confirm == null ) 512 return ""; 513 return confirm; 514 } 515 516 522 public final String getName() 523 { 524 if( name == null ) 525 return ""; 526 return name; 527 } 528 529 535 public final String getNameVisible() 536 { 537 if( nameVisible != null ) 538 return "checked"; 539 return ""; 540 } 541 542 548 public final String getEmailVisible() 549 { 550 if( emailVisible != null ) 551 return "checked"; 552 return ""; 553 } 554 555 561 public final String getProperty(String name) 562 { 563 String tmp = (String )properties.get(name); 564 if( tmp != null ) 565 return tmp; 566 tmp = (String )required.get(name); 567 if( tmp != null ) 568 return tmp; 569 return ""; 570 } 571 572 576 public final void setProperty(String name, String value) 577 { 578 user.setProperty(name,value); 579 if( properties.containsKey(name) ) 580 properties.put(name,value); 581 else if( required.containsKey(name) ) 582 properties.put(name,value); 583 } 584 } 585 | Popular Tags |