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
|
|
|
|