Skip to content

Commit adee210

Browse files
fixed broken signin/signup
1 parent fe6ec12 commit adee210

File tree

3 files changed

+69
-19
lines changed

3 files changed

+69
-19
lines changed

src/ApiBundle/Controller/AuthController.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,12 @@ public function postRegisterAction()
232232
$request = $this->container->get('request');
233233
$userManager = $this->get('fos_user.user_manager');
234234

235+
// TODO: Why this validation is not working in Validation.yml for dob
236+
$timestamp = strtotime($request->request->get('dob'));
237+
if (!$timestamp) {
238+
$this->logAndThrowError(400, 'Date of Birth should be in MM/DD/YYYY format.', $this->get('translator')->trans('api.show_error_dob', array(), 'messages', $request->getLocale()), $request->getLocale());
239+
}
240+
235241
$user = $userManager->createUser();
236242

237243
$user->setUsername($request->request->get('username'));
@@ -245,7 +251,7 @@ public function postRegisterAction()
245251

246252
// Validate user data
247253
$validator = $this->get('validator');
248-
$errors = $validator->validate($user);
254+
$errors = $validator->validate($user, null, array('Default', 'Registration', 'profile_edit'));
249255

250256
if (count($errors) > 0) {
251257
return $this->reportValidationErrors($errors, $request->getLocale());
@@ -341,9 +347,17 @@ public function editPasswordAction()
341347
}
342348

343349
$user->setPlainPassword($password);
344-
$msg = 'Password changed successfully';
350+
351+
// Validate user data
352+
$validator = $this->get('validator');
353+
$errors = $validator->validate($user, null, array('profile_edit_password'));
354+
355+
if (count($errors) > 0) {
356+
return $this->reportValidationErrors($errors, $request->getLocale());
357+
}
345358

346359
$userManager->updateUser($user);
360+
$msg = 'Password changed successfully';
347361

348362
$this->logMessage(200, $msg.' for '.$user->getUsername());
349363

@@ -436,6 +450,12 @@ public function editProfileAction()
436450
$dob = array_key_exists('dob', $data) ? $data['dob'] : $user->getDob();
437451
$user->setDob($dob);
438452

453+
// TODO: Why this validation is not working in Validation.yml for dob
454+
$timestamp = strtotime($dob);
455+
if ($dob && !$timestamp) {
456+
$this->logAndThrowError(400, 'Date of Birth should be in MM/DD/YYYY format.', $this->get('translator')->trans('api.show_error_dob', array(), 'messages', $request->getLocale()), $request->getLocale());
457+
}
458+
439459
// Validate user data
440460
$validator = $this->get('validator');
441461
$errors = $validator->validate($user, null, array('profile_edit'));

src/ApiBundle/Resources/config/config.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ fos_rest:
4141

4242
fos_user:
4343
db_driver: orm
44-
firewall_name: main # Seems to be used when registering user/reseting password,
45-
# but since there is no "login", as so it seems to be useless in
46-
# our particular context, but still required by "FOSUserBundle"
44+
firewall_name: main
4745
user_class: ApiBundle\Entity\User
4846

4947
fos_oauth_server:

src/ApiBundle/Resources/config/validation.yml

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,72 @@ ApiBundle\Entity\User:
1414
properties:
1515
firstname:
1616
- NotBlank:
17-
groups: [profile_edit]
17+
groups: [profile_edit, Default]
1818
message: "user.show_error_firstname"
1919
payload:
2020
api_error: 'api.show_error_firstname'
21+
dob:
22+
- Date:
23+
groups: [profile_edit, Default]
24+
message: "user.show_error_dob"
25+
payload:
26+
api_error: 'api.show_error_dob'
2127
username:
22-
- Regex:
23-
pattern: '/^[a-z0-9_-]{3,16}$/'
24-
match: true
28+
- NotBlank:
29+
groups: [Registration, Profile, Default]
2530
message: "user.show_error_username_policy"
2631
payload:
2732
api_error: 'api.show_error_username_policy'
28-
password:
2933
- Regex:
30-
pattern: '/^([a-zA-Z0-9@*#]{8,15})$/'
34+
groups: [Registration, Profile, Default]
35+
pattern: '/^[a-z0-9_-]{3,16}$/'
3136
match: true
32-
message: "user.show_error_password_policy"
37+
message: "user.show_error_username_policy"
3338
payload:
34-
api_error: 'api.show_error_password_policy'
39+
api_error: 'api.show_error_username_policy'
3540
email:
36-
- Email:
41+
- NotBlank:
42+
groups: [Registration, Profile, Default]
3743
message: "user.show_error_email"
44+
payload:
45+
api_error: 'api.show_error_email'
46+
- Email:
47+
groups: [Registration, Profile, Default]
3848
checkMX: false
49+
message: "user.show_error_email"
3950
payload:
4051
api_error: 'api.show_error_email'
41-
dob:
42-
- Date:
43-
groups: [profile_edit]
44-
message: "user.show_error_dob"
52+
plainPassword:
53+
- NotBlank:
54+
groups: [profile_edit_password, Registration, Default]
55+
message: "user.show_error_password_policy"
4556
payload:
46-
api_error: 'api.show_error_dob'
57+
api_error: 'api.show_error_password_policy'
58+
- Regex:
59+
groups: [profile_edit_password, Registration, Profile, Default]
60+
pattern: '/^([a-zA-Z0-9@*#]{8,15})$/'
61+
match: true
62+
message: "user.show_error_password_policy"
63+
payload:
64+
api_error: 'api.show_error_password_policy'
4765
roles:
4866
- Choice:
4967
choices: ['ROLE_USER', 'ROLE_API']
5068
multiple: true
5169
message: "user.show_error_role"
5270
payload:
5371
api_error: 'api.show_error_role'
72+
73+
FOS\UserBundle\Form\Model\ChangePassword:
74+
properties:
75+
new:
76+
- NotBlank:
77+
message: "user.show_error_password_policy"
78+
payload:
79+
api_error: 'api.show_error_password_policy'
80+
- Regex:
81+
pattern: '/^([a-zA-Z0-9@*#]{8,15})$/'
82+
match: true
83+
message: "user.show_error_password_policy"
84+
payload:
85+
api_error: 'api.show_error_password_policy'

0 commit comments

Comments
 (0)