-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EZP-31017: Added Password Validator to change password form (#70)
- Loading branch information
Showing
13 changed files
with
301 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace EzSystems\EzPlatformUser\Validator\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
|
||
/** | ||
* @Annotation | ||
*/ | ||
class Password extends Constraint | ||
{ | ||
/** @var string */ | ||
public $message = 'ez.user.password.invalid'; | ||
|
||
/** @var \eZ\Publish\API\Repository\Values\ContentType\ContentType|null */ | ||
public $contentType; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getTargets(): array | ||
{ | ||
return [self::CLASS_CONSTRAINT, self::PROPERTY_CONSTRAINT]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace EzSystems\EzPlatformUser\Validator\Constraints; | ||
|
||
use eZ\Publish\API\Repository\UserService; | ||
use eZ\Publish\API\Repository\Values\User\PasswordValidationContext; | ||
use EzSystems\EzPlatformContentForms\Validator\ValidationErrorsProcessor; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
|
||
class PasswordValidator extends ConstraintValidator | ||
{ | ||
/** @var \eZ\Publish\API\Repository\UserService */ | ||
private $userService; | ||
|
||
public function __construct(UserService $userService) | ||
{ | ||
$this->userService = $userService; | ||
} | ||
|
||
public function validate($value, Constraint $constraint): void | ||
{ | ||
if (!\is_string($value) || empty($value)) { | ||
return; | ||
} | ||
|
||
$passwordValidationContext = new PasswordValidationContext([ | ||
'contentType' => $constraint->contentType, | ||
]); | ||
|
||
$validationErrors = $this->userService->validatePassword($value, $passwordValidationContext); | ||
if (!empty($validationErrors)) { | ||
$validationErrorsProcessor = $this->createValidationErrorsProcessor(); | ||
$validationErrorsProcessor->processValidationErrors($validationErrors); | ||
} | ||
} | ||
|
||
protected function createValidationErrorsProcessor(): ValidationErrorsProcessor | ||
{ | ||
return new ValidationErrorsProcessor($this->context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace EzSystems\EzPlatformUser\Tests\Validator\Constraint; | ||
|
||
use EzSystems\EzPlatformUser\Validator\Constraints\Password; | ||
use EzSystems\EzPlatformUser\Validator\Constraints\PasswordValidator; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class PasswordTest extends TestCase | ||
{ | ||
/** @var \EzSystems\EzPlatformUser\Validator\Constraints\Password */ | ||
private $constraint; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->constraint = new Password(); | ||
} | ||
|
||
public function testConstruct(): void | ||
{ | ||
$this->assertSame('ez.user.password.invalid', $this->constraint->message); | ||
} | ||
|
||
public function testValidatedBy(): void | ||
{ | ||
$this->assertSame(PasswordValidator::class, $this->constraint->validatedBy()); | ||
} | ||
|
||
public function testGetTargets(): void | ||
{ | ||
$this->assertSame([Password::CLASS_CONSTRAINT, Password::PROPERTY_CONSTRAINT], $this->constraint->getTargets()); | ||
} | ||
} |
Oops, something went wrong.