Completed
Pull Request — master (#343)
by Leny
09:31
created

PageOwnerVoter::vote()   B

Complexity

Conditions 8
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 16
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 16
loc 16
rs 7.7777
cc 8
eloc 9
nc 4
nop 3
1
<?php
2
3
namespace Victoire\Bundle\PageBundle\Security\Voter;
4
5
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
6
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
7
use Victoire\Bundle\PageBundle\Entity\Page;
8
9
/**
10
 * This class decides yes or no if the user is granted to do some action on a given page.
11
 */
12
class PageOwnerVoter extends Voter
13
{
14
    private $userClass;
15
16
    /**
17
     * PageOwnerVoter constructor.
18
     *
19
     * @param $userClass
20
     */
21
    public function __construct($userClass)
22
    {
23
        $this->userClass = $userClass;
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    protected function supports($attribute, $subject)
30
    {
31
        return $subject instanceof Page && 'PAGE_OWNER' === $attribute;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 View Code Duplication
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39
        return $token->getUser() instanceof $this->userClass
40
            && (
41
                $token->getUser()->hasRole('ROLE_VICTOIRE')
42
             || $token->getUser()->hasRole('ROLE_VICTOIRE_DEVELOPER')
43
             || $subject->getAuthor() === $token->getUser()
44
            );
45
    }
46
}
47