1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
4
|
|
|
|
5
|
|
|
declare(strict_types=1); |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\Security\Authorization\Voter; |
8
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\Entity\MessageRelUser; |
10
|
|
|
use Chamilo\CoreBundle\Entity\User; |
11
|
|
|
use Symfony\Bundle\SecurityBundle\Security; |
12
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
13
|
|
|
use Symfony\Component\Security\Core\Authorization\Voter\Voter; |
14
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
15
|
|
|
|
16
|
|
|
class MessageRelUserVoter extends Voter |
17
|
|
|
{ |
18
|
|
|
public const DELETE = 'DELETE'; |
19
|
|
|
public const VIEW = 'VIEW'; |
20
|
|
|
public const EDIT = 'EDIT'; |
21
|
|
|
|
22
|
|
|
public function __construct( |
23
|
|
|
private readonly Security $security |
24
|
|
|
) { |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
protected function supports(string $attribute, mixed $subject): bool |
28
|
|
|
{ |
29
|
|
|
return in_array($attribute, [self::DELETE, self::VIEW, self::EDIT]) |
30
|
|
|
&& $subject instanceof MessageRelUser; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool |
34
|
|
|
{ |
35
|
|
|
$user = $token->getUser(); |
36
|
|
|
|
37
|
|
|
if (!$user instanceof UserInterface) { |
38
|
|
|
return false; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if ($this->security->isGranted('ROLE_ADMIN')) { |
42
|
|
|
return true; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
assert($user instanceof User); |
46
|
|
|
assert($subject instanceof MessageRelUser); |
47
|
|
|
|
48
|
|
|
$message = $subject->getMessage(); |
49
|
|
|
$isReceiver = $message->hasUserReceiver($user); |
50
|
|
|
|
51
|
|
|
return match ($attribute) { |
52
|
|
|
self::VIEW, self::EDIT, self::DELETE => $isReceiver, |
53
|
|
|
default => false, |
54
|
|
|
}; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|