Completed
Push — master ( d2bae3...337415 )
by Jan
04:02
created

PermissionVoter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 38
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 10 2
A voteOnUser() 0 5 1
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\Security\Voter;
33
34
35
use App\Entity\UserSystem\User;
36
37
/**
38
 * This voter allows you to directly check permissions from the permission structure, without passing an object.
39
 * However you should use the "normal" object based voters if possible, because they are needed for a future ACL system.
40
 * @package App\Security\Voter
41
 */
42
class PermissionVoter extends ExtendedVoter
43
{
44
45
    /**
46
     * Similar to voteOnAttribute, but checking for the anonymous user is already done.
47
     * The current user (or the anonymous user) is passed by $user.
48
     *
49
     * @param $attribute
50
     * @param $subject
51
     * @param User $user
52
     *
53
     * @return bool
54
     */
55
    protected function voteOnUser($attribute, $subject, User $user): bool
56
    {
57
        $attribute = ltrim($attribute, '@');
58
        [$perm, $op] = explode('.', $attribute);
59
        return $this->resolver->inherit($user, $perm, $op);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->resolver->...erit($user, $perm, $op) could return the type null which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
60
    }
61
62
    /**
63
     * Determines if the attribute and subject are supported by this voter.
64
     *
65
     * @param string $attribute An attribute
66
     * @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
67
     *
68
     * @return bool True if the attribute and subject are supported, false otherwise
69
     */
70
    protected function supports($attribute, $subject)
71
    {
72
        //Check if the attribute has the form @permission.operation
73
        if (preg_match('/^@\\w+\\.\\w+$/', $attribute)) {
74
            $attribute = ltrim($attribute, '@');
75
            [$perm, $op] = explode('.', $attribute);
76
            return $this->resolver->isValidOperation($perm, $op);
77
        }
78
79
        return false;
80
    }
81
}