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

BusinessEntityOwnerVoter::supports()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 3
eloc 4
nc 3
nop 2
1
<?php
2
3
namespace Victoire\Bundle\BusinessEntityBundle\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\BusinessEntityBundle\Helper\BusinessEntityHelper;
8
9
/**
10
 * This class decides yes or no if the user is granted to do some action on a given entity.
11
 */
12
class BusinessEntityOwnerVoter extends Voter
13
{
14
    private $userClass;
15
    private $businessEntityHelper;
16
17
    /**
18
     * @param $userClass
19
     * @param BusinessEntityHelper $businessEntityHelper
20
     */
21
    public function __construct($userClass, BusinessEntityHelper $businessEntityHelper)
22
    {
23
        $this->userClass = $userClass;
24
        $this->businessEntityHelper = $businessEntityHelper;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected function supports($attribute, $subject)
31
    {
32
        return method_exists($subject, 'getAuthor')
33
            && $this->businessEntityHelper->findByEntityInstance($subject)
34
            && 'BUSINESS_ENTITY_OWNER' === $attribute;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 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...
41
    {
42
        return $token->getUser() instanceof $this->userClass
43
            && ($token->getUser()->hasRole('ROLE_VICTOIRE')
44
                || $token->getUser()->hasRole('ROLE_VICTOIRE_DEVELOPER')
45
                || $subject->getAuthor() === $token->getUser()
46
            );
47
    }
48
}
49