Completed
Push — master ( 559831...ac302a )
by Jan
04:24
created

ValidPermissionValidator::validate()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 9
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 17
rs 8.8333
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\Parts\PartLot;
36
use App\Entity\UserSystem\PermissionsEmbed;
37
use App\Security\Interfaces\HasPermissionsInterface;
38
use App\Services\PermissionResolver;
39
use Symfony\Component\Form\Exception\UnexpectedTypeException;
40
use Symfony\Component\Validator\Constraint;
41
use Symfony\Component\Validator\ConstraintValidator;
42
43
class ValidPermissionValidator extends ConstraintValidator
44
{
45
46
    protected $resolver;
47
    protected $perm_structure;
48
49
    public function __construct(PermissionResolver $resolver)
50
    {
51
        $this->resolver = $resolver;
52
        $this->perm_structure = $resolver->getPermissionStructure();
53
    }
54
55
    /**
56
     * Checks if the passed value is valid.
57
     *
58
     * @param mixed $value The value that should be validated
59
     * @param Constraint $constraint The constraint for the validation
60
     */
61
    public function validate($value, Constraint $constraint)
62
    {
63
        if (!$constraint instanceof ValidPermission) {
64
            throw new UnexpectedTypeException($constraint, ValidPermission::class);
65
        }
66
67
        /** @var HasPermissionsInterface $perm_holder */
68
        $perm_holder = $this->context->getObject();
69
70
        //Check for each permission and operation, for an alsoSet attribute
71
        foreach ($this->perm_structure['perms'] as $perm_key => $permission) {
72
            foreach ($permission['operations'] as $op_key => $op) {
73
                if (!empty($op['alsoSet']) &&
74
                    $this->resolver->dontInherit($perm_holder, $perm_key, $op_key) === true) {
75
                    //Set every op listed in also Set
76
                    foreach ($op['alsoSet'] as $set_also) {
77
                        $this->resolver->setPermission($perm_holder, $perm_key, $set_also, true);
78
                    }
79
                }
80
            }
81
        }
82
    }
83
}