Passed
Push — master ( 262c23...1cc54c )
by Angel Fernando Quiroz
09:24
created

ResourceFileVoter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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\ResourceFile;
10
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
11
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
12
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
13
14
/**
15
 * @extends Voter<'DOWNLOAD', ResourceFile>
16
 */
17
final class ResourceFileVoter extends Voter
18
{
19
    public const DOWNLOAD = 'VIEW';
20
21
    public function __construct(
22
        private readonly AccessDecisionManagerInterface $accessDecisionManager,
23
    ) {}
24
25
    protected function supports(string $attribute, mixed $subject): bool
26
    {
27
        $options = [
28
            self::DOWNLOAD,
29
        ];
30
31
        if (!\in_array($attribute, $options, true)) {
32
            return false;
33
        }
34
35
        return $subject instanceof ResourceFile;
36
    }
37
38
    protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
39
    {
40
        $resourceFile = $subject;
41
        $resourceNode = $resourceFile->getResourceNode();
42
43
        if ($this->accessDecisionManager->decide($token, ['ROLE_ADMIN'])) {
44
            return true;
45
        }
46
47
        return match ($attribute) {
48
            self::DOWNLOAD => $this->accessDecisionManager->decide(
49
                $token,
50
                [ResourceNodeVoter::VIEW],
51
                $resourceNode
52
            ),
53
            default => false,
54
        };
55
    }
56
}
57