Skip to content

Commit 3857004

Browse files
authored
EZP-31017: Added Password Validator to change password form (#70)
1 parent 0c2f08f commit 3857004

File tree

13 files changed

+301
-29
lines changed

13 files changed

+301
-29
lines changed

src/bundle/Controller/PasswordChangeController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ public function __construct(
6666
*/
6767
public function userPasswordChangeAction(Request $request)
6868
{
69-
$form = $this->formFactory->changeUserPassword();
69+
/** @var \eZ\Publish\API\Repository\Values\User\User $user */
70+
$user = $this->tokenStorage->getToken()->getUser()->getAPIUser();
71+
$form = $this->formFactory->changeUserPassword($user->getContentType());
7072
$form->handleRequest($request);
7173

7274
if ($form->isSubmitted() && $form->isValid()) {
@@ -76,7 +78,6 @@ public function userPasswordChangeAction(Request $request)
7678
$newPassword = $data->getNewPassword();
7779
$userUpdateStruct = $this->userService->newUserUpdateStruct();
7880
$userUpdateStruct->password = $newPassword;
79-
$user = $this->tokenStorage->getToken()->getUser()->getAPIUser();
8081
$this->userService->updateUser($user, $userUpdateStruct);
8182

8283
if ((new IsAdmin($this->siteAccessGroups))->isSatisfiedBy($request->attributes->get('siteaccess'))) {

src/bundle/Controller/PasswordResetController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ public function userResetPasswordAction(Request $request, string $hashKey)
166166

167167
return $view;
168168
}
169-
$userPasswordResetData = new UserPasswordResetData(null, $user->getContentType());
170-
$form = $this->formFactory->resetUserPassword($userPasswordResetData);
169+
$userPasswordResetData = new UserPasswordResetData();
170+
$form = $this->formFactory->resetUserPassword($userPasswordResetData, null, $user->getContentType());
171171
$form->handleRequest($request);
172172

173173
if ($form->isSubmitted() && $form->isValid()) {

src/bundle/Resources/config/routing.yaml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,6 @@ ezplatform.user.reset_password:
2222
defaults:
2323
_controller: 'EzSystems\EzPlatformUserBundle\Controller\PasswordResetController::userResetPasswordAction'
2424

25-
# Deprecated in v2.5 and will be removed in v3.0. Use ezplatform.user.register instead.
26-
ez_user_register:
27-
path: /register
28-
defaults:
29-
_controller: 'EzSystems\EzPlatformUserBundle\Controller\UserRegisterController::registerAction'
30-
31-
# Deprecated in v2.5 and will be removed in v3.0. Use ezplatform.user.register_confirmation instead.
32-
ez_user_register_confirmation:
33-
path: /register-confirm
34-
defaults:
35-
_controller: 'EzSystems\EzPlatformUserBundle\Controller\UserRegisterController::registerConfirmAction'
36-
3725
ezplatform.user.register: &user_register
3826
path: /register
3927
defaults:

src/bundle/Resources/config/services/validators.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ services:
1010
EzSystems\EzPlatformUser\Validator\Constraints\UserPasswordValidator:
1111
tags:
1212
- { name: validator.constraint_validator }
13+
14+
EzSystems\EzPlatformUser\Validator\Constraints\PasswordValidator:
15+
arguments:
16+
$userService: '@ezpublish.api.service.user'
17+
tags:
18+
- { name: validator.constraint_validator }

src/lib/Form/Data/UserPasswordResetData.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class UserPasswordResetData
2121
private $newPassword;
2222

2323
/**
24+
* @deprecated ContentType should be passed as option to FormType.
25+
*
2426
* @var \eZ\Publish\API\Repository\Values\ContentType\ContentType
2527
*/
2628
private $contentType;

src/lib/Form/Data/UserRegisterData.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
namespace EzSystems\EzPlatformUser\Form\Data;
1010

11-
use EzSystems\EzPlatformContentForms\Data as RepositoryFormsData;
11+
use EzSystems\EzPlatformContentForms\Data\User\UserCreateData;
1212

13-
class UserRegisterData extends RepositoryFormsData\User\UserRegisterData
13+
class UserRegisterData extends UserCreateData
1414
{
1515
}

src/lib/Form/Factory/FormFactory.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace EzSystems\EzPlatformUser\Form\Factory;
1010

11+
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
1112
use EzSystems\EzPlatformUser\Form\Data\UserPasswordForgotData;
1213
use EzSystems\EzPlatformUser\Form\Data\UserPasswordChangeData;
1314
use EzSystems\EzPlatformUser\Form\Data\UserSettingUpdateData;
@@ -41,19 +42,19 @@ public function __construct(FormFactoryInterface $formFactory, UrlGeneratorInter
4142
$this->urlGenerator = $urlGenerator;
4243
}
4344

44-
/**
45-
* @param \EzSystems\EzPlatformUser\Form\Data\UserPasswordChangeData|null $data
46-
* @param string|null $name
47-
*
48-
* @return \Symfony\Component\Form\FormInterface
49-
*/
5045
public function changeUserPassword(
46+
ContentType $contentType,
5147
UserPasswordChangeData $data = null,
5248
?string $name = null
5349
): FormInterface {
5450
$name = $name ?: StringUtil::fqcnToBlockPrefix(UserPasswordChangeType::class);
5551

56-
return $this->formFactory->createNamed($name, UserPasswordChangeType::class, $data);
52+
return $this->formFactory->createNamed(
53+
$name,
54+
UserPasswordChangeType::class,
55+
$data,
56+
['content_type' => $contentType]
57+
);
5758
}
5859

5960
/**
@@ -100,11 +101,14 @@ public function forgotUserPasswordWithLogin(
100101
*/
101102
public function resetUserPassword(
102103
UserPasswordResetData $data = null,
103-
?string $name = null
104+
?string $name = null,
105+
ContentType $contentType = null
104106
): FormInterface {
105107
$name = $name ?: StringUtil::fqcnToBlockPrefix(UserPasswordResetType::class);
106108

107-
return $this->formFactory->createNamed($name, UserPasswordResetType::class, $data, ['content_type' => $data->getContentType()]);
109+
$userContentType = $contentType ?? $data->getContentType();
110+
111+
return $this->formFactory->createNamed($name, UserPasswordResetType::class, $data, ['content_type' => $userContentType]);
108112
}
109113

110114
/**

src/lib/Form/Type/UserPasswordChangeType.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
namespace EzSystems\EzPlatformUser\Form\Type;
1010

11+
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
1112
use EzSystems\EzPlatformUser\Form\Data\UserPasswordChangeData;
13+
use EzSystems\EzPlatformUser\Validator\Constraints\Password;
1214
use Symfony\Component\Form\AbstractType;
1315
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
1416
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
@@ -31,6 +33,11 @@ public function buildForm(FormBuilderInterface $builder, array $options)
3133
'required' => true,
3234
'first_options' => ['label' => /** @Desc("New password") */ 'ezplatform.change_user_password.new_password'],
3335
'second_options' => ['label' => /** @Desc("Confirm password") */ 'ezplatform.change_user_password.confirm_new_password'],
36+
'constraints' => [
37+
new Password([
38+
'contentType' => $options['content_type'],
39+
]),
40+
],
3441
])
3542
->add(
3643
'change',
@@ -41,6 +48,8 @@ public function buildForm(FormBuilderInterface $builder, array $options)
4148

4249
public function configureOptions(OptionsResolver $resolver)
4350
{
51+
$resolver->setRequired('content_type');
52+
$resolver->setAllowedTypes('content_type', ContentType::class);
4453
$resolver->setDefaults([
4554
'data_class' => UserPasswordChangeData::class,
4655
'translation_domain' => 'forms',

src/lib/Form/Type/UserPasswordResetType.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
namespace EzSystems\EzPlatformUser\Form\Type;
1010

11+
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
1112
use EzSystems\EzPlatformUser\Form\Data\UserPasswordResetData;
12-
use EzSystems\EzPlatformContentForms\Validator\Constraints\Password;
13+
use EzSystems\EzPlatformUser\Validator\Constraints\Password;
1314
use Symfony\Component\Form\AbstractType;
1415
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
1516
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
@@ -41,10 +42,11 @@ public function buildForm(FormBuilderInterface $builder, array $options)
4142

4243
public function configureOptions(OptionsResolver $resolver)
4344
{
45+
$resolver->setRequired('content_type');
46+
$resolver->setAllowedTypes('content_type', ContentType::class);
4447
$resolver->setDefaults([
4548
'data_class' => UserPasswordResetData::class,
4649
'translation_domain' => 'forms',
47-
'content_type' => null,
4850
]);
4951
}
5052
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace EzSystems\EzPlatformUser\Validator\Constraints;
10+
11+
use Symfony\Component\Validator\Constraint;
12+
13+
/**
14+
* @Annotation
15+
*/
16+
class Password extends Constraint
17+
{
18+
/** @var string */
19+
public $message = 'ez.user.password.invalid';
20+
21+
/** @var \eZ\Publish\API\Repository\Values\ContentType\ContentType|null */
22+
public $contentType;
23+
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function getTargets(): array
28+
{
29+
return [self::CLASS_CONSTRAINT, self::PROPERTY_CONSTRAINT];
30+
}
31+
}

0 commit comments

Comments
 (0)