33namespace App \controllers ;
44
55use App \auth ;
6+ use App \forms ;
67use App \mailers ;
78use App \models ;
89use App \services ;
@@ -21,118 +22,73 @@ class Registrations extends BaseController
2122 /**
2223 * Show the registration form.
2324 *
24- * @response 302 / if connected
25- * @response 302 /login if registrations are closed
25+ * @response 302 /
26+ * If the user is connected.
27+ * @response 302 /login
28+ * If the registrations are closed.
2629 * @response 200
27- *
28- * @return \Minz\Response
30+ * On sucess.
2931 */
3032 public function new (): Response
3133 {
3234 if (auth \CurrentUser::get ()) {
3335 return Response::redirect ('home ' );
3436 }
3537
36- $ app_conf = \App \Configuration::$ application ;
37- if (!$ app_conf ['registrations_opened ' ]) {
38+ if (!\App \Configuration::areRegistrationsOpened ()) {
3839 return Response::redirect ('login ' );
3940 }
4041
41- $ app_path = \App \Configuration::$ app_path ;
42- $ terms_path = $ app_path . '/policies/terms.html ' ;
43- $ has_terms = file_exists ($ terms_path );
42+ $ form = new forms \Registration ();
4443
4544 return Response::ok ('registrations/new.phtml ' , [
46- 'has_terms ' => $ has_terms ,
47- 'username ' => '' ,
48- 'email ' => '' ,
49- 'password ' => '' ,
50- 'subscriptions_enabled ' => $ app_conf ['subscriptions_enabled ' ],
51- 'subscriptions_host ' => $ app_conf ['subscriptions_host ' ],
45+ 'form ' => $ form ,
5246 ]);
5347 }
5448
5549 /**
5650 * Create a user.
5751 *
58- * @request_param string csrf
5952 * @request_param string email
6053 * @request_param string username
6154 * @request_param string password
6255 * @request_param bool accept_terms
6356 * @request_param bool accept_contact
57+ * @request_param string csrf_token
6458 *
65- * @response 302 / if already connected
66- * @response 302 /login if registrations are closed
67- * @response 400 if CSRF token is wrong
68- * @response 400 if email, username or password is missing/invalid
69- * @response 400 if the service has terms of service and accept_terms is false
70- * @response 400 if email already exists
59+ * @response 302 /
60+ * If the user is connected.
61+ * @response 302 /login
62+ * If the registrations are closed.
63+ * @response 400
64+ * If at least one of the parameters is invalid.
7165 * @response 302 /onboarding
66+ * On sucess.
7267 */
7368 public function create (Request $ request ): Response
7469 {
7570 if (auth \CurrentUser::get ()) {
7671 return Response::redirect ('home ' );
7772 }
7873
79- $ app_conf = \App \Configuration::$ application ;
80- if (!$ app_conf ['registrations_opened ' ]) {
74+ if (!\App \Configuration::areRegistrationsOpened ()) {
8175 return Response::redirect ('login ' );
8276 }
8377
84- $ app_path = \App \Configuration::$ app_path ;
85- $ terms_path = $ app_path . '/policies/terms.html ' ;
86- $ has_terms = file_exists ($ terms_path );
87-
88- $ username = $ request ->parameters ->getString ('username ' , '' );
89- $ email = $ request ->parameters ->getString ('email ' , '' );
90- $ password = $ request ->parameters ->getString ('password ' , '' );
91- $ accept_terms = $ request ->parameters ->getBoolean ('accept_terms ' );
92- $ accept_contact = $ request ->parameters ->getBoolean ('accept_contact ' );
93- $ csrf = $ request ->parameters ->getString ('csrf ' , '' );
94-
95- if (!\App \Csrf::validate ($ csrf )) {
96- return Response::badRequest ('registrations/new.phtml ' , [
97- 'has_terms ' => $ has_terms ,
98- 'username ' => $ username ,
99- 'email ' => $ email ,
100- 'password ' => $ password ,
101- 'subscriptions_enabled ' => $ app_conf ['subscriptions_enabled ' ],
102- 'subscriptions_host ' => $ app_conf ['subscriptions_host ' ],
103- 'error ' => _ ('A security verification failed: you should retry to submit the form. ' ),
104- ]);
105- }
78+ $ user = new models \User ();
79+ $ form = new forms \Registration (model: $ user );
80+ $ form ->handleRequest ($ request );
10681
107- if ($ has_terms && ! $ accept_terms ) {
82+ if (! $ form -> validate () ) {
10883 return Response::badRequest ('registrations/new.phtml ' , [
109- 'has_terms ' => $ has_terms ,
110- 'username ' => $ username ,
111- 'email ' => $ email ,
112- 'password ' => $ password ,
113- 'subscriptions_enabled ' => $ app_conf ['subscriptions_enabled ' ],
114- 'subscriptions_host ' => $ app_conf ['subscriptions_host ' ],
115- 'errors ' => [
116- 'accept_terms ' => _ ('You must accept the terms of service. ' ),
117- ],
84+ 'form ' => $ form ,
11885 ]);
11986 }
12087
121- try {
122- $ user = services \UserCreator::create ($ username , $ email , $ password );
123- } catch (services \UserCreatorError $ e ) {
124- return Response::badRequest ('registrations/new.phtml ' , [
125- 'has_terms ' => $ has_terms ,
126- 'username ' => $ username ,
127- 'email ' => $ email ,
128- 'password ' => $ password ,
129- 'subscriptions_enabled ' => $ app_conf ['subscriptions_enabled ' ],
130- 'subscriptions_host ' => $ app_conf ['subscriptions_host ' ],
131- 'errors ' => $ e ->errors (),
132- ]);
133- }
88+ $ user = $ form ->model ();
89+ $ user ->save ();
13490
135- $ user-> accept_contact = $ accept_contact ;
91+ services \UserService:: initializeData ( $ user) ;
13692
13793 // Initialize the validation token
13894 $ validation_token = new models \Token (1 , 'day ' , 16 );
0 commit comments