UserObjectVoter::getSupportedClasses()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of Dedipanel project
5
 *
6
 * (c) 2010-2015 Dedipanel <http://www.dedicated-panel.net>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace DP\Core\UserBundle\Security;
13
14
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
15
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
16
17
/**
18
 * Can vote against other user objects,
19
 * based on the roles and group of current user
20
 */
21
class UserObjectVoter extends AbstractObjectVoter
22
{
23
    protected function getSupportedClasses()
24
    {
25
        return ['DP\Core\UserBundle\Entity\User'];
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function vote(TokenInterface $token, $object, array $attributes)
32
    {
33
        if ($this->supportsClass(get_class($object))) {
34
            // Deny access if the user try to edit/delete himself (except for super admin)
35
            if ($object === $token->getUser()
36
                && array_intersect(['ROLE_DP_ADMIN_USER_UPDATE', 'ROLE_DP_ADMIN_USER_DELETE'], $attributes) !== array()
37
                && !$token->getUser()->isSuperAdmin()) {
38
                return VoterInterface::ACCESS_DENIED;
39
            }
40
41
            /** @var \DP\Core\UserBundle\Entity\User $user */
42
            $user  = $token->getUser();
43
            $accessibleGroups = $this->getUserAccessibleGroups($user);
44
45
            /** @var \DP\Core\UserBundle\Entity\Group|null $group Direct group of the user against which we are voting */
46
            $group = $object->getGroup();
47
48
            if (($group !== null && in_array($group, $accessibleGroups))
49
                || $user->isSuperAdmin()) {
50
                return VoterInterface::ACCESS_GRANTED;
51
            }
52
53
            return VoterInterface::ACCESS_DENIED;
54
        }
55
56
        return VoterInterface::ACCESS_ABSTAIN;
57
    }
58
}
59