1 package org.appfuse.webapp.action; 2 3 import java.util.Locale ; 4 5 import javax.servlet.http.HttpServletRequest ; 6 import javax.servlet.http.HttpServletResponse ; 7 8 import org.appfuse.Constants; 9 import org.appfuse.model.User; 10 import org.appfuse.service.RoleManager; 11 import org.appfuse.service.UserExistsException; 12 import org.appfuse.util.StringUtil; 13 import org.appfuse.webapp.util.RequestUtil; 14 15 import org.springframework.beans.factory.NoSuchBeanDefinitionException; 16 import org.springframework.context.ApplicationContext; 17 import org.springframework.validation.BindException; 18 import org.springframework.web.context.support.WebApplicationContextUtils; 19 import org.springframework.web.servlet.ModelAndView; 20 21 import org.acegisecurity.context.SecurityContextHolder; 22 import org.acegisecurity.Authentication; 23 import org.acegisecurity.providers.UsernamePasswordAuthenticationToken; 24 import org.acegisecurity.providers.ProviderManager; 25 26 35 public class SignupController extends BaseFormController { 36 private RoleManager roleManager; 37 38 public void setRoleManager(RoleManager roleManager) { 39 this.roleManager = roleManager; 40 } 41 42 public SignupController() { 43 setCommandName("user"); 44 setCommandClass(User.class); 45 } 46 47 public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, 48 Object command, BindException errors) 49 throws Exception { 50 if (log.isDebugEnabled()) { 51 log.debug("entering 'onSubmit' method..."); 52 } 53 54 User user = (User) command; 55 Locale locale = request.getLocale(); 56 57 Boolean encrypt = (Boolean ) getConfiguration().get(Constants.ENCRYPT_PASSWORD); 58 59 if (encrypt != null && encrypt.booleanValue()) { 60 String algorithm = (String ) getConfiguration().get(Constants.ENC_ALGORITHM); 61 62 if (algorithm == null) { log.debug("assuming testcase, setting algorithm to 'SHA'"); 64 algorithm = "SHA"; 65 } 66 67 user.setPassword(StringUtil.encodePassword(user.getPassword(), algorithm)); 68 } 69 70 user.setEnabled(true); 71 72 user.addRole(roleManager.getRole(Constants.USER_ROLE)); 74 75 try { 76 this.getUserManager().saveUser(user); 77 } catch (UserExistsException e) { 78 log.warn(e.getMessage()); 79 80 errors.rejectValue("username", "errors.existing.user", 81 new Object [] { 82 user.getUsername(), user.getEmail() 83 }, "duplicate user"); 84 85 user.setPassword(user.getConfirmPassword()); 87 return showForm(request, response, errors); 88 } 89 90 saveMessage(request, getText("user.registered", user.getUsername(), locale)); 91 request.getSession().setAttribute(Constants.REGISTERED, Boolean.TRUE); 92 93 Authentication auth = new UsernamePasswordAuthenticationToken(user.getUsername(), user.getConfirmPassword()); 95 try { 96 ApplicationContext ctx = 97 WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext()); 98 if (ctx != null) { 99 ProviderManager authenticationManager = (ProviderManager) ctx.getBean("authenticationManager"); 100 SecurityContextHolder.getContext().setAuthentication(authenticationManager.doAuthentication(auth)); 101 } 102 } catch (NoSuchBeanDefinitionException n) { 103 } 105 106 if (log.isDebugEnabled()) { 108 log.debug("Sending user '" + user.getUsername() + "' an account information e-mail"); 109 } 110 111 message.setSubject(getText("signup.email.subject", locale)); 113 sendUserMessage(user, getText("signup.email.message", locale), RequestUtil.getAppURL(request)); 114 115 return new ModelAndView(getSuccessView()); 116 } 117 } 118 | Popular Tags |