Passed
Push — master ( e157d2...b7d807 )
by Julito
12:22
created

MessageVoter   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
dl 0
loc 72
rs 10
c 1
b 0
f 0
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A supports() 0 15 2
B voteOnAttribute() 0 38 11
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 VIEW = 'VIEW';
20
    public const EDIT = 'EDIT';
21
    public const DELETE = 'DELETE';
22
23
    private EntityManagerInterface $entityManager;
24
    private Security $security;
25
26
    public function __construct(
27
        EntityManagerInterface $entityManager,
28
        Security $security
29
    ) {
30
        $this->entityManager = $entityManager;
31
        $this->security = $security;
32
    }
33
34
    protected function supports(string $attribute, $subject): bool
35
    {
36
        $options = [
37
            self::VIEW,
38
            self::EDIT,
39
            self::DELETE,
40
        ];
41
42
        // if the attribute isn't one we support, return false
43
        if (!\in_array($attribute, $options, true)) {
44
            return false;
45
        }
46
47
        // only vote on Post objects inside this voter
48
        return $subject instanceof Message;
49
    }
50
51
    protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
52
    {
53
        /** @var User $user */
54
        $user = $token->getUser();
55
56
        if (!$user instanceof UserInterface) {
57
            return false;
58
        }
59
60
        // Admins have access to everything
61
        if ($this->security->isGranted('ROLE_ADMIN')) {
62
            return true;
63
        }
64
65
        /** @var Message $message */
66
        $message = $subject;
67
68
        switch ($attribute) {
69
            case self::VIEW:
70
                if ($message->getUserReceiver() === $user) {
71
                    return true;
72
                }
73
74
                break;
75
            case self::EDIT:
76
            case self::DELETE:
77
                if ($message->getUserReceiver() === $user && Message::MESSAGE_TYPE_INBOX === $message->getMsgType()) {
78
                    return true;
79
                }
80
81
                if ($message->getUserSender() === $user && Message::MESSAGE_TYPE_OUTBOX === $message->getMsgType()) {
82
                    return true;
83
                }
84
85
                break;
86
        }
87
88
        return false;
89
    }
90
}
91