Passed
Push — master ( 4ed902...760dff )
by Julito
11:06 queued 11s
created

MessageVoter   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 80
rs 10
wmc 16

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A supports() 0 16 2
C voteOnAttribute() 0 44 13
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->getUserSender() === $user) {
73
                    return true;
74
                }
75
76
                break;
77
            case self::VIEW:
78
                if ($message->getUserReceiver() === $user) {
79
                    return true;
80
                }
81
82
                break;
83
            case self::EDIT:
84
            case self::DELETE:
85
                if ($message->getUserReceiver() === $user && Message::MESSAGE_TYPE_INBOX === $message->getMsgType()) {
86
                    return true;
87
                }
88
89
                if ($message->getUserSender() === $user && Message::MESSAGE_TYPE_OUTBOX === $message->getMsgType()) {
90
                    return true;
91
                }
92
93
                break;
94
        }
95
96
        return false;
97
    }
98
}
99