Completed
Push — master ( 856e20...90d449 )
by Jan
04:33
created

NoLockoutValidator::validate()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 10
nc 8
nop 2
dl 0
loc 22
rs 8.8333
c 1
b 0
f 0
1
<?php
2
/**
3
 *
4
 * part-db version 0.1
5
 * Copyright (C) 2005 Christoph Lechner
6
 * http://www.cl-projects.de/
7
 *
8
 * part-db version 0.2+
9
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
10
 * http://code.google.com/p/part-db/
11
 *
12
 * Part-DB Version 0.4+
13
 * Copyright (C) 2016 - 2019 Jan Böhmer
14
 * https://github.com/jbtronics
15
 *
16
 * This program is free software; you can redistribute it and/or
17
 * modify it under the terms of the GNU General Public License
18
 * as published by the Free Software Foundation; either version 2
19
 * of the License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
29
 *
30
 */
31
32
namespace App\Validator\Constraints;
33
34
35
use App\Entity\UserSystem\Group;
36
use App\Entity\UserSystem\User;
37
use App\Services\PermissionResolver;
38
use Doctrine\ORM\EntityManager;
39
use Doctrine\ORM\EntityManagerInterface;
40
use Doctrine\ORM\Mapping\Entity;
41
use Symfony\Component\Form\Exception\UnexpectedTypeException;
42
use Symfony\Component\Security\Core\Security;
43
use Symfony\Component\Validator\Constraint;
44
use Symfony\Component\Validator\ConstraintValidator;
45
46
class NoLockoutValidator extends ConstraintValidator
47
{
48
49
    protected $resolver;
50
    protected $perm_structure;
51
    protected $security;
52
    protected $entityManager;
53
54
    public function __construct(PermissionResolver $resolver, Security $security, EntityManagerInterface $entityManager)
55
    {
56
        $this->resolver = $resolver;
57
        $this->perm_structure = $resolver->getPermissionStructure();
58
        $this->security = $security;
59
        $this->entityManager = $entityManager;
60
    }
61
62
    /**
63
     * Checks if the passed value is valid.
64
     *
65
     * @param mixed $value The value that should be validated
66
     * @param Constraint $constraint The constraint for the validation
67
     */
68
    public function validate($value, Constraint $constraint)
69
    {
70
        if (!$constraint instanceof NoLockout) {
71
            throw new UnexpectedTypeException($constraint, NoLockout::class);
72
        }
73
74
75
        $perm_holder = $value;
76
77
        //Prevent that a user revokes its own change_permission perm (prevent the user to lock out itself)
78
        if ($perm_holder instanceof User || $perm_holder instanceof Group) {
79
80
            $user = $this->security->getUser();
81
82
            if ($user === null) {
83
                $user = $this->entityManager->getRepository(User::class)->getAnonymousUser();
84
            }
85
86
            if ($user instanceof User) {
87
                //Check if we the change_permission permission has changed from allow to disallow
88
                if (($this->resolver->inherit($user, 'users', 'edit_permissions') ?? false) === false) {
89
                    $this->context->addViolation($constraint->message);
90
                }
91
            }
92
        }
93
    }
94
}