Passed
Push — master ( ab3f5d...01e1f2 )
by Jan
02:56
created

PartVoter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Security\Voter;
4
5
use App\Configuration\PermissionsConfiguration;
6
use App\Entity\Part;
7
use App\Entity\User;
8
use App\Services\PermissionResolver;
9
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
10
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
11
use Symfony\Component\Security\Core\User\UserInterface;
12
13
14
/**
15
 * A Voter that votes on Part entities.
16
 *
17
 * See parts permissions for valid operations.
18
 *
19
 * @package App\Security\Voter
20
 */
21
class PartVoter extends Voter
22
{
23
    const READ = "read";
24
25
    protected $resolver;
26
27
    public function __construct(PermissionResolver $resolver)
28
    {
29
        $this->resolver = $resolver;
30
    }
31
32
    protected function supports($attribute, $subject)
33
    {
34
        // replace with your own logic
35
        // https://symfony.com/doc/current/security/voters.html
36
        //return ($subject instanceof Part || in_array($subject, ['PERM_parts', 'PERM_parts_name']));
37
38
        if ($subject instanceof Part)
39
        {
40
           return in_array($attribute, $this->resolver->listOperationsForPermission('parts'), false);
41
        }
42
43
        return false;
44
    }
45
46
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
47
    {
48
        $user = $token->getUser();
49
        // if the user is anonymous, do not grant access
50
        if (!$user instanceof User) {
51
            return false;
52
        }
53
54
        if($subject instanceof Part) {
55
            //Null concealing operator means, that no
56
            return $this->resolver->inherit($user, 'parts', $attribute) ?? false;
57
        }
58
59
        //Deny access by default.
60
        return false;
61
    }
62
}
63