Passed
Branch main (b6a268)
by Iain
04:11
created

TeamVoter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A supports() 0 7 2
A voteOnAttribute() 0 16 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright Humbly Arrogant Ltd 2020-2022.
7
 *
8
 * Use of this software is governed by the Business Source License included in the LICENSE file and at https://getparthenon.com/docs/next/license.
9
 *
10
 * Change Date: TBD ( 3 years after 2.0.0 release )
11
 *
12
 * On the date above, in accordance with the Business Source License, use of this software will be governed by the open source license specified in the LICENSE file.
13
 */
14
15
namespace Parthenon\User\Security\Voter;
16
17
use Parthenon\User\Entity\MemberInterface;
18
use Parthenon\User\Entity\TeamOwnedInterface;
19
use Parthenon\User\Repository\TeamRepositoryInterface;
20
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
21
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
22
23
final class TeamVoter extends Voter
24
{
25
    private TeamRepositoryInterface $teamRepository;
26
27
    public function __construct(TeamRepositoryInterface $teamRepository)
28
    {
29
        $this->teamRepository = $teamRepository;
30
    }
31
32
    protected function supports(string $attribute, $subject): bool
33
    {
34
        if (!$subject instanceof TeamOwnedInterface) {
35
            return false;
36
        }
37
38
        return true;
39
    }
40
41
    protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
42
    {
43
        $user = $token->getUser();
44
45
        if (!$user instanceof MemberInterface) {
46
            return false;
47
        }
48
49
        if (!$subject instanceof TeamOwnedInterface) {
50
            return true;
51
        }
52
53
        // workaround for Doctrine. Get latest and not the one within the document
54
        $team = $this->teamRepository->findById($subject->getOwningTeam()->getId());
55
56
        return $team->hasMember($user);
57
    }
58
}
59