1414
1515import org .cloudfoundry .identity .uaa .authentication .Origin ;
1616import org .cloudfoundry .identity .uaa .authentication .UaaPrincipal ;
17+ import org .cloudfoundry .identity .uaa .error .UaaException ;
1718import org .cloudfoundry .identity .uaa .user .UaaAuthority ;
19+ import org .hibernate .validator .constraints .Email ;
1820import org .springframework .http .HttpStatus ;
1921import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
2022import org .springframework .security .core .context .SecurityContextHolder ;
2123import org .springframework .stereotype .Controller ;
2224import org .springframework .ui .Model ;
25+ import org .springframework .validation .BindingResult ;
26+ import org .springframework .web .bind .annotation .ModelAttribute ;
2327import org .springframework .web .bind .annotation .RequestMapping ;
2428import org .springframework .web .bind .annotation .RequestMethod ;
2529import org .springframework .web .bind .annotation .RequestParam ;
2630import org .springframework .web .client .HttpClientErrorException ;
2731
2832import javax .servlet .http .HttpServletResponse ;
33+ import javax .validation .Valid ;
34+
35+ import java .io .IOException ;
2936
3037import static org .springframework .web .bind .annotation .RequestMethod .GET ;
3138import static org .springframework .web .bind .annotation .RequestMethod .POST ;
3239
3340@ Controller
34- @ RequestMapping ("/accounts" )
3541public class AccountsController {
3642
3743 private final AccountCreationService accountCreationService ;
@@ -40,58 +46,54 @@ public AccountsController(AccountCreationService accountCreationService) {
4046 this .accountCreationService = accountCreationService ;
4147 }
4248
43- @ RequestMapping (value = "/new " , method = GET )
49+ @ RequestMapping (value = "/create_account " , method = GET )
4450 public String activationEmail (Model model ,
4551 @ RequestParam (value = "client_id" , defaultValue = "login" ) String clientId ) {
4652 model .addAttribute ("client_id" , clientId );
4753 return "accounts/new_activation_email" ;
4854 }
4955
50- @ RequestMapping (value = "/new" , method = GET , params = {"code" , "email" })
51- public String newAccount () {
52- return "accounts/new" ;
53- }
54-
55- @ RequestMapping (method = POST , params = {"email" , "client_id" })
56- public String sendActivationEmail (@ RequestParam ("email" ) String email ,
57- @ RequestParam ("client_id" ) String clientId ) {
58- accountCreationService .beginActivation (email , clientId );
56+ @ RequestMapping (value = "/create_account.do" , method = POST )
57+ public String sendActivationEmail (Model model , HttpServletResponse response ,
58+ @ RequestParam ("client_id" ) String clientId ,
59+ @ Valid @ ModelAttribute ("email" ) ValidEmail email , BindingResult result ,
60+ @ RequestParam ("password" ) String password ,
61+ @ RequestParam ("password_confirmation" ) String passwordConfirmation ) {
62+ if (result .hasErrors ()) {
63+ return handleUnprocessableEntity (model , response , "invalid_email" );
64+ }
65+ ChangePasswordValidation validation = new ChangePasswordValidation (password , passwordConfirmation );
66+ if (!validation .valid ()) {
67+ return handleUnprocessableEntity (model , response , validation .getMessageCode ());
68+ }
69+ try {
70+ accountCreationService .beginActivation (email .getEmail (), password , clientId );
71+ } catch (UaaException e ) {
72+ return handleUnprocessableEntity (model , response , "username_exists" );
73+ }
5974 return "redirect:accounts/email_sent" ;
6075 }
6176
62- @ RequestMapping (value = "/email_sent" , method = RequestMethod .GET )
77+ @ RequestMapping (value = "/accounts/ email_sent" , method = RequestMethod .GET )
6378 public String emailSent () {
6479 return "accounts/email_sent" ;
6580 }
6681
67- @ RequestMapping (method = POST , params = { "email " , "code" , "password" , "password_confirmation" } )
68- public String createAccount (Model model ,
82+ @ RequestMapping (value = "/verify_user " , method = GET )
83+ public String verifyUser (Model model ,
6984 @ RequestParam ("code" ) String code ,
70- @ RequestParam ("password" ) String password ,
71- @ RequestParam ("password_confirmation" ) String passwordConfirmation ,
72- HttpServletResponse response ) {
85+ HttpServletResponse response ) throws IOException {
7386
74- ChangePasswordValidation validation = new ChangePasswordValidation (password , passwordConfirmation );
75- if (!validation .valid ()) {
76- model .addAttribute ("message_code" , validation .getMessageCode ());
77- response .setStatus (HttpStatus .UNPROCESSABLE_ENTITY .value ());
78- return "accounts/new" ;
79- }
80-
81- AccountCreationService .AccountCreation accountCreation ;
87+ AccountCreationService .AccountCreationResponse accountCreation ;
8288 try {
83- accountCreation = accountCreationService .completeActivation (code , password );
89+ accountCreation = accountCreationService .completeActivation (code );
8490 } catch (HttpClientErrorException e ) {
85- if (e .getStatusCode ().equals (HttpStatus .CONFLICT )) {
86- model .addAttribute ("message_code" , "email_already_taken" );
87- } else {
88- model .addAttribute ("message_code" , "code_expired" );
89- }
91+ model .addAttribute ("error_message_code" , "code_expired" );
9092 response .setStatus (HttpStatus .UNPROCESSABLE_ENTITY .value ());
91- return "accounts/new " ;
93+ return "accounts/new_activation_email " ;
9294 }
9395
94- UaaPrincipal uaaPrincipal = new UaaPrincipal (accountCreation .getUserId (), accountCreation .getUsername (), accountCreation .getUsername (), Origin .UAA , null );
96+ UaaPrincipal uaaPrincipal = new UaaPrincipal (accountCreation .getUserId (), accountCreation .getUsername (), accountCreation .getEmail (), Origin .UAA , null );
9597 UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken (uaaPrincipal , null , UaaAuthority .USER_AUTHORITIES );
9698 SecurityContextHolder .getContext ().setAuthentication (token );
9799
@@ -101,4 +103,23 @@ public String createAccount(Model model,
101103 }
102104 return "redirect:" + redirectLocation ;
103105 }
106+
107+ private String handleUnprocessableEntity (Model model , HttpServletResponse response , String errorMessage ) {
108+ model .addAttribute ("error_message_code" , errorMessage );
109+ response .setStatus (HttpStatus .UNPROCESSABLE_ENTITY .value ());
110+ return "accounts/new_activation_email" ;
111+ }
112+
113+ public static class ValidEmail {
114+ @ Email
115+ String email ;
116+
117+ public String getEmail () {
118+ return email ;
119+ }
120+
121+ public void setEmail (String email ) {
122+ this .email = email ;
123+ }
124+ }
104125}
0 commit comments