Passed
Push — master ( 9d0dde...4d39d5 )
by Jan
08:02
created

PartVoter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A voteOnUser() 0 9 2
A supports() 0 12 2
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 ExtendedVoter
22
{
23
    const READ = "read";
24
25
26
    protected function supports($attribute, $subject)
27
    {
28
        // replace with your own logic
29
        // https://symfony.com/doc/current/security/voters.html
30
        //return ($subject instanceof Part || in_array($subject, ['PERM_parts', 'PERM_parts_name']));
31
32
        if ($subject instanceof Part)
33
        {
34
           return in_array($attribute, $this->resolver->listOperationsForPermission('parts'), false);
35
        }
36
37
        return false;
38
    }
39
40
41
    protected function voteOnUser($attribute, $subject, User $user): bool
42
    {
43
        if($subject instanceof Part) {
44
            //Null concealing operator means, that no
45
            return $this->resolver->inherit($user, 'parts', $attribute) ?? false;
46
        }
47
48
        //Deny access by default.
49
        return false;
50
    }
51
}
52