|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Zikula package. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright Zikula - https://ziku.la/ |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Zikula\UsersModule\Validator\Constraints; |
|
15
|
|
|
|
|
16
|
|
|
use Symfony\Component\Validator\Constraint; |
|
17
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
|
18
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
|
19
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
20
|
|
|
use Zikula\UsersModule\Constant as UsersConstant; |
|
21
|
|
|
use Zikula\UsersModule\Entity\Repository\UserRepository; |
|
22
|
|
|
use Zikula\UsersModule\Entity\RepositoryInterface\UserRepositoryInterface; |
|
23
|
|
|
use Zikula\UsersModule\Entity\UserEntity; |
|
24
|
|
|
|
|
25
|
|
|
class ValidUserFieldsValidator extends ConstraintValidator |
|
26
|
|
|
{ |
|
27
|
|
|
public const DUP_EMAIL_ALT_AUTH = 'DuplicateEmailOfAlternativeAuthMethod'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var TranslatorInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $translator; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var UserRepository |
|
36
|
|
|
*/ |
|
37
|
|
|
private $userRepository; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct( |
|
40
|
|
|
TranslatorInterface $translator, |
|
41
|
|
|
UserRepositoryInterface $userRepository |
|
42
|
|
|
) { |
|
43
|
|
|
$this->translator = $translator; |
|
44
|
|
|
$this->userRepository = $userRepository; |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function validate($data, Constraint $constraint) |
|
48
|
|
|
{ |
|
49
|
|
|
if (!$constraint instanceof ValidUserFields) { |
|
50
|
|
|
throw new UnexpectedTypeException($constraint, ValidUserFields::class); |
|
51
|
|
|
} |
|
52
|
|
|
// ensure unique uname |
|
53
|
|
|
$this->validateUniqueUname($data['uname'], $data['uid'] ?? null); |
|
54
|
|
|
|
|
55
|
|
|
// users registering duplicate email with different authentication method are invalid |
|
56
|
|
|
if ($data instanceof UserEntity && $data->hasAttribute(UsersConstant::AUTHENTICATION_METHOD_ATTRIBUTE_KEY)) { |
|
57
|
|
|
$this->validateEmailWithAuth($data); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private function validateUniqueUname(string $uname, ?int $uid = null): void |
|
62
|
|
|
{ |
|
63
|
|
|
if ($this->userRepository->countDuplicateUnames($uname, $uid) > 0) { |
|
64
|
|
|
$this->context->buildViolation($this->translator->trans('The user name you entered (%userName%) has already been registered.', ['%userName%' => $uname], 'validators')) |
|
65
|
|
|
->atPath('uname') |
|
66
|
|
|
->addViolation(); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
private function validateEmailWithAuth(UserEntity $data): void |
|
71
|
|
|
{ |
|
72
|
|
|
$authMethod = $data->getAttributeValue(UsersConstant::AUTHENTICATION_METHOD_ATTRIBUTE_KEY); |
|
73
|
|
|
$existing = $this->userRepository->getByEmailAndAuthMethod($data['email'], $authMethod); |
|
|
|
|
|
|
74
|
|
|
if (count($existing) > 0) { |
|
75
|
|
|
$this->context->buildViolation($this->translator->trans('This email is in use by another authentication method. Please login with that method instead.', [], 'validators')) |
|
76
|
|
|
->atPath('email') |
|
77
|
|
|
->setCode(self::DUP_EMAIL_ALT_AUTH) |
|
78
|
|
|
->addViolation(); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.