|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
|
4
|
|
|
|
|
5
|
|
|
namespace Chamilo\CoreBundle\Security\Authorization\Voter; |
|
6
|
|
|
|
|
7
|
|
|
use Chamilo\CoreBundle\Entity\AbstractResource; |
|
8
|
|
|
use Chamilo\CoreBundle\Entity\ResourceNode; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
10
|
|
|
use Symfony\Component\Security\Acl\Permission\MaskBuilder; |
|
11
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
12
|
|
|
use Symfony\Component\Security\Core\Authorization\Voter\Voter; |
|
13
|
|
|
use Symfony\Component\Security\Core\Security; |
|
14
|
|
|
|
|
15
|
|
|
class ResourceVoter extends Voter |
|
16
|
|
|
{ |
|
17
|
|
|
public const VIEW = 'VIEW'; |
|
18
|
|
|
public const CREATE = 'CREATE'; |
|
19
|
|
|
public const EDIT = 'EDIT'; |
|
20
|
|
|
public const DELETE = 'DELETE'; |
|
21
|
|
|
public const EXPORT = 'EXPORT'; |
|
22
|
|
|
|
|
23
|
|
|
private $requestStack; |
|
24
|
|
|
private $security; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(Security $security, RequestStack $requestStack) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->security = $security; |
|
29
|
|
|
$this->requestStack = $requestStack; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public static function getReaderMask(): int |
|
33
|
|
|
{ |
|
34
|
|
|
$builder = new MaskBuilder(); |
|
35
|
|
|
$builder |
|
36
|
|
|
->add(self::VIEW) |
|
37
|
|
|
; |
|
38
|
|
|
|
|
39
|
|
|
return $builder->get(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public static function getEditorMask(): int |
|
43
|
|
|
{ |
|
44
|
|
|
$builder = new MaskBuilder(); |
|
45
|
|
|
$builder |
|
46
|
|
|
->add(self::VIEW) |
|
47
|
|
|
->add(self::EDIT) |
|
48
|
|
|
; |
|
49
|
|
|
|
|
50
|
|
|
return $builder->get(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function supports(string $attribute, $subject): bool |
|
54
|
|
|
{ |
|
55
|
|
|
$options = [ |
|
56
|
|
|
self::VIEW, |
|
57
|
|
|
self::CREATE, |
|
58
|
|
|
self::EDIT, |
|
59
|
|
|
self::DELETE, |
|
60
|
|
|
self::EXPORT, |
|
61
|
|
|
]; |
|
62
|
|
|
error_log('resource supports'); |
|
63
|
|
|
// if the attribute isn't one we support, return false |
|
64
|
|
|
if (!in_array($attribute, $options)) { |
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// only vote on ResourceNode objects inside this voter |
|
69
|
|
|
if (!$subject instanceof AbstractResource) { |
|
70
|
|
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool |
|
77
|
|
|
{ |
|
78
|
|
|
error_log('resource voteOnAttribute'); |
|
79
|
|
|
$user = $token->getUser(); |
|
80
|
|
|
|
|
81
|
|
|
return true; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|