KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > appfuse > webapp > action > SignupForm


1 package org.appfuse.webapp.action;
2
3 import java.io.Serializable JavaDoc;
4
5 import org.acegisecurity.Authentication;
6 import org.acegisecurity.context.SecurityContextHolder;
7 import org.acegisecurity.providers.ProviderManager;
8 import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
9 import org.appfuse.Constants;
10 import org.appfuse.model.User;
11 import org.appfuse.service.RoleManager;
12 import org.appfuse.service.UserExistsException;
13 import org.appfuse.util.StringUtil;
14 import org.appfuse.webapp.util.RequestUtil;
15 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
16 import org.springframework.context.ApplicationContext;
17 import org.springframework.web.context.support.WebApplicationContextUtils;
18
19 /**
20  * JSF Page class to handle signing up a new user.
21  *
22  * @author mraible
23  */

24 public class SignupForm extends BasePage implements Serializable JavaDoc {
25     private static final long serialVersionUID = 3524937486662786265L;
26     private User user = new User();
27     private RoleManager roleManager;
28
29     public User getUser() {
30         return user;
31     }
32
33     public void setUser(User user) {
34         this.user = user;
35     }
36     
37     public void setRoleManager(RoleManager roleManager) {
38         this.roleManager = roleManager;
39     }
40     
41     public String JavaDoc save() throws Exception JavaDoc {
42         Boolean JavaDoc encrypt = (Boolean JavaDoc) getConfiguration().get(Constants.ENCRYPT_PASSWORD);
43         
44         if (encrypt != null && encrypt.booleanValue()) {
45             String JavaDoc algorithm = (String JavaDoc) getConfiguration().get(Constants.ENC_ALGORITHM);
46     
47             if (algorithm == null) { // should only happen for test case
48
if (log.isDebugEnabled()) {
49                     log.debug("assuming testcase, setting algorithm to 'SHA'");
50                 }
51                 algorithm = "SHA";
52             }
53             
54             user.setPassword(StringUtil.encodePassword(user.getPassword(), algorithm));
55         }
56         
57         user.setEnabled(true);
58         
59         // Set the default user role on this new user
60
user.addRole(roleManager.getRole(Constants.USER_ROLE));
61
62         try {
63             userManager.saveUser(user);
64         } catch (UserExistsException e) {
65             log.warn(e.getMessage());
66             addMessage("errors.existing.user",
67                     new Object JavaDoc[] { user.getUsername(), user.getEmail() });
68
69             // redisplay the unencrypted passwords
70
user.setPassword(user.getConfirmPassword());
71             return null;
72         }
73
74         addMessage("user.registered");
75         getSession().setAttribute(Constants.REGISTERED, Boolean.TRUE);
76
77         // log user in automatically
78
Authentication auth = new UsernamePasswordAuthenticationToken(user.getUsername(), user.getConfirmPassword());
79         try {
80             ApplicationContext ctx =
81                 WebApplicationContextUtils.getWebApplicationContext(getSession().getServletContext());
82             if (ctx != null) {
83                 ProviderManager authenticationManager = (ProviderManager) ctx.getBean("authenticationManager");
84                 SecurityContextHolder.getContext().setAuthentication(authenticationManager.doAuthentication(auth));
85             }
86         } catch (NoSuchBeanDefinitionException n) {
87             // ignore, should only happen when testing
88
}
89         
90         // Send an account information e-mail
91
message.setSubject(getText("signup.email.subject"));
92         sendUserMessage(user, getText("signup.email.message"),
93                         RequestUtil.getAppURL(getRequest()));
94         
95         return "mainMenu";
96     }
97     
98     public String JavaDoc getCountry() {
99         return getUser().getAddress().getCountry();
100     }
101     
102     // for some reason, the country drop-down won't do
103
// getUser().getAddress().setCountry(value)
104
public void setCountry(String JavaDoc country) {
105         getUser().getAddress().setCountry(country);
106     }
107 }
108
Popular Tags