Skip to content

Commit 7818d03

Browse files
Fixed Broken Sign in/Sign up. Enhanced Validations. Username/Email made readonly during edit on Web.
1 parent adee210 commit 7818d03

File tree

6 files changed

+26
-22
lines changed

6 files changed

+26
-22
lines changed

src/ApiBundle/Controller/Admin/UserController.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,17 @@ public function indexAction()
4848
*/
4949
public function newAction(Request $request)
5050
{
51-
$user = new User();
51+
$userManager = $this->container->get('fos_user.user_manager');
52+
$user = $userManager->createUser();
5253
$user->setRoles(['ROLE_USER', 'ROLE_API']);
54+
5355
$form = $this->createForm(UserType::class, $user);
56+
5457
$locale = $request->getLocale();
5558

5659
$form->handleRequest($request);
5760

5861
if ($form->isSubmitted() && $form->isValid()) {
59-
$userManager = $this->container->get('fos_user.user_manager');
60-
$user = $userManager->createUser();
61-
6262
$this->setUserData($user, $form);
6363

6464
$userManager->updateUser($user);
@@ -163,7 +163,7 @@ private function setUserData(User $user, \Symfony\Component\Form\Form $form)
163163
$user->setDob($form['dob']->getData());
164164
$user->setEmail($form['email']->getData());
165165
$user->setUsername($form['username']->getData());
166-
$user->setPlainPassword($form['password']->getData());
166+
$user->setPlainPassword($form['plainPassword']->getData());
167167
$user->setRoles($form['roles']->getData());
168168
$user->setConfirmationToken(null);
169169
$user->setEnabled(true);

src/ApiBundle/Controller/AuthController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public function postRegisterAction()
251251

252252
// Validate user data
253253
$validator = $this->get('validator');
254-
$errors = $validator->validate($user, null, array('Default', 'Registration', 'profile_edit'));
254+
$errors = $validator->validate($user, null, array('Registration', 'profile_edit'));
255255

256256
if (count($errors) > 0) {
257257
return $this->reportValidationErrors($errors, $request->getLocale());

src/ApiBundle/Form/UserType.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
2828
->add('email', EmailType::class)
2929
->add('dob', DateType::class, array('widget' => 'single_text', 'format' => 'M/d/y'))
3030
->add('username', TextType::class)
31-
->add('password', PasswordType::class, array('data' => ''))
31+
->add('plainPassword', PasswordType::class, array('data' => ''))
3232
->add('roles', CollectionType::class, array(
3333
'entry_type' => ChoiceType::class,
3434
'entry_options' => array(
@@ -48,7 +48,8 @@ public function configureOptions(OptionsResolver $resolver)
4848
{
4949
$resolver->setDefaults(array(
5050
'data_class' => 'ApiBundle\Entity\User',
51-
'csrf_protection' => true
51+
'csrf_protection' => true,
52+
'validation_groups' => array('Registration', 'profile_edit')
5253
));
5354
}
5455

src/ApiBundle/Resources/config/validation.yml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,56 +14,57 @@ ApiBundle\Entity\User:
1414
properties:
1515
firstname:
1616
- NotBlank:
17-
groups: [profile_edit, Default]
17+
groups: [profile_edit]
1818
message: "user.show_error_firstname"
1919
payload:
2020
api_error: 'api.show_error_firstname'
2121
dob:
2222
- Date:
23-
groups: [profile_edit, Default]
23+
groups: [profile_edit]
2424
message: "user.show_error_dob"
2525
payload:
2626
api_error: 'api.show_error_dob'
2727
username:
2828
- NotBlank:
29-
groups: [Registration, Profile, Default]
29+
groups: [Registration, Profile]
3030
message: "user.show_error_username_policy"
3131
payload:
3232
api_error: 'api.show_error_username_policy'
3333
- Regex:
34-
groups: [Registration, Profile, Default]
34+
groups: [Registration, Profile]
3535
pattern: '/^[a-z0-9_-]{3,16}$/'
3636
match: true
3737
message: "user.show_error_username_policy"
3838
payload:
3939
api_error: 'api.show_error_username_policy'
4040
email:
4141
- NotBlank:
42-
groups: [Registration, Profile, Default]
42+
groups: [Registration, Profile]
4343
message: "user.show_error_email"
4444
payload:
4545
api_error: 'api.show_error_email'
4646
- Email:
47-
groups: [Registration, Profile, Default]
47+
groups: [Registration, Profile]
4848
checkMX: false
4949
message: "user.show_error_email"
5050
payload:
5151
api_error: 'api.show_error_email'
5252
plainPassword:
5353
- NotBlank:
54-
groups: [profile_edit_password, Registration, Default]
54+
groups: [profile_edit_password, Registration]
5555
message: "user.show_error_password_policy"
5656
payload:
5757
api_error: 'api.show_error_password_policy'
5858
- Regex:
59-
groups: [profile_edit_password, Registration, Profile, Default]
59+
groups: [profile_edit_password, Registration, Profile]
6060
pattern: '/^([a-zA-Z0-9@*#]{8,15})$/'
6161
match: true
6262
message: "user.show_error_password_policy"
6363
payload:
6464
api_error: 'api.show_error_password_policy'
6565
roles:
6666
- Choice:
67+
groups: [profile_roles]
6768
choices: ['ROLE_USER', 'ROLE_API']
6869
multiple: true
6970
message: "user.show_error_role"

src/ApiBundle/Resources/views/admin/user/edit.html.twig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<h1>{{ 'title.edit_user'|trans({'%id%': user.id}) }}</h1>
77

88
{{ form_start(edit_form, { attr: attr|default({}) }) }}
9+
{{ form_errors(edit_form) }}
910
<table class="table">
1011
<tbody>
1112
<tr>
@@ -33,21 +34,21 @@
3334
<th scope="row">{{ 'label.user_email'|trans }}</th>
3435
<td>
3536
{{ form_errors(edit_form.email) }}
36-
{{ form_widget(edit_form.email) }}
37+
{{ form_widget(edit_form.email, { 'attr':{'readonly':'readonly'} }) }}
3738
</td>
3839
</tr>
3940
<tr>
4041
<th scope="row">{{ 'label.user_username'|trans }}</th>
4142
<td>
4243
{{ form_errors(edit_form.username) }}
43-
{{ form_widget(edit_form.username) }}
44+
{{ form_widget(edit_form.username, { 'attr':{'readonly':'readonly'} }) }}
4445
</td>
4546
</tr>
4647
<tr>
4748
<th scope="row">{{ 'label.user_password'|trans }}</th>
4849
<td>
49-
{{ form_errors(edit_form.password) }}
50-
{{ form_widget(edit_form.password) }}
50+
{{ form_errors(edit_form.plainPassword) }}
51+
{{ form_widget(edit_form.plainPassword) }}
5152
</td>
5253
</tr>
5354
<tr>

src/ApiBundle/Resources/views/admin/user/new.html.twig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<h1>{{ 'title.user_new'|trans }}</h1>
77

88
{{ form_start(form, { attr: attr|default({}) }) }}
9+
{{ form_errors(form) }}
910
<table class="table">
1011
<tbody>
1112
<tr>
@@ -46,8 +47,8 @@
4647
<tr>
4748
<th scope="row">{{ 'label.user_password'|trans }}</th>
4849
<td>
49-
{{ form_errors(form.password) }}
50-
{{ form_widget(form.password) }}
50+
{{ form_errors(form.plainPassword) }}
51+
{{ form_widget(form.plainPassword) }}
5152
</td>
5253
</tr>
5354
<tr>

0 commit comments

Comments
 (0)