1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (C) 2020 Jan Böhmer |
4
|
|
|
* |
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Affero General Public License as published |
7
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU Affero General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
16
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace App\Validator; |
20
|
|
|
|
21
|
|
|
use App\Entity\User; |
22
|
|
|
use Symfony\Component\Security\Core\Security; |
23
|
|
|
use Symfony\Component\Validator\Constraint; |
24
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
25
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
26
|
|
|
|
27
|
|
|
class NoLockoutValidator extends ConstraintValidator |
28
|
|
|
{ |
29
|
|
|
protected $security; |
30
|
|
|
|
31
|
|
|
public function __construct(Security $security) |
32
|
|
|
{ |
33
|
|
|
$this->security = $security; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function validate($value, Constraint $constraint) |
37
|
|
|
{ |
38
|
|
|
/** @var NoLockout $constraint */ |
39
|
|
|
|
40
|
|
|
if (null === $value || '' === $value) { |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (!$constraint instanceof NoLockout) { |
|
|
|
|
45
|
|
|
throw new UnexpectedTypeException($value, NoLockout::class); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (!$value instanceof User) { |
49
|
|
|
throw new UnexpectedTypeException($value, User::class); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$current_user = $this->security->getUser(); |
53
|
|
|
|
54
|
|
|
//Perform checks only if the edited user is the one which is logged in |
55
|
|
|
if ($current_user instanceof User && $current_user->getId() === $value->getId() |
56
|
|
|
&& ( |
57
|
|
|
!in_array('ROLE_EDIT_USER', $value->getRoles(), true) |
58
|
|
|
|| $value->isDisabled() |
59
|
|
|
)) { |
60
|
|
|
$this->context->buildViolation($constraint->message) |
61
|
|
|
->addViolation(); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|