1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* For licensing terms, see /license.txt */ |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\Security\Authorization\Voter; |
8
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\Entity\Message; |
10
|
|
|
use Chamilo\CoreBundle\Entity\User; |
11
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
12
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
13
|
|
|
use Symfony\Component\Security\Core\Authorization\Voter\Voter; |
14
|
|
|
use Symfony\Component\Security\Core\Security; |
15
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
16
|
|
|
|
17
|
|
|
class MessageVoter extends Voter |
18
|
|
|
{ |
19
|
|
|
public const CREATE = 'CREATE'; |
20
|
|
|
public const VIEW = 'VIEW'; |
21
|
|
|
public const EDIT = 'EDIT'; |
22
|
|
|
public const DELETE = 'DELETE'; |
23
|
|
|
|
24
|
|
|
private EntityManagerInterface $entityManager; |
25
|
|
|
private Security $security; |
26
|
|
|
|
27
|
|
|
public function __construct( |
28
|
|
|
EntityManagerInterface $entityManager, |
29
|
|
|
Security $security |
30
|
|
|
) { |
31
|
|
|
$this->entityManager = $entityManager; |
32
|
|
|
$this->security = $security; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function supports(string $attribute, $subject): bool |
36
|
|
|
{ |
37
|
|
|
$options = [ |
38
|
|
|
self::CREATE, |
39
|
|
|
self::VIEW, |
40
|
|
|
self::EDIT, |
41
|
|
|
self::DELETE, |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
// if the attribute isn't one we support, return false |
45
|
|
|
if (!\in_array($attribute, $options, true)) { |
46
|
|
|
return false; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// only vote on Post objects inside this voter |
50
|
|
|
return $subject instanceof Message; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool |
54
|
|
|
{ |
55
|
|
|
/** @var User $user */ |
56
|
|
|
$user = $token->getUser(); |
57
|
|
|
|
58
|
|
|
if (!$user instanceof UserInterface) { |
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Admins have access to everything. |
63
|
|
|
if ($this->security->isGranted('ROLE_ADMIN')) { |
64
|
|
|
return true; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** @var Message $message */ |
68
|
|
|
$message = $subject; |
69
|
|
|
|
70
|
|
|
switch ($attribute) { |
71
|
|
|
case self::CREATE: |
72
|
|
|
if ($message->getSender() === $user) { |
73
|
|
|
return true; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
break; |
77
|
|
|
case self::VIEW: |
78
|
|
|
if ($message->hasReceiver($user) || $message->getSender() === $user) { |
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
break; |
83
|
|
|
case self::EDIT: |
84
|
|
|
case self::DELETE: |
85
|
|
|
// @todo |
86
|
|
|
break; |
87
|
|
|
/*if ($message->hasReceiver($user) && |
88
|
|
|
Message::MESSAGE_TYPE_INBOX === $message->getMsgType() |
89
|
|
|
) { |
90
|
|
|
return true; |
91
|
|
|
}*/ |
92
|
|
|
|
93
|
|
|
// User cannot delete. |
94
|
|
|
/*if ($message->getSender() === $user && Message::MESSAGE_TYPE_OUTBOX === $message->getMsgType()) { |
95
|
|
|
return true; |
96
|
|
|
}*/ |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|