Completed
Push — master ( fdbeef...265a2d )
by jerome
25:01 queued 21:35
created

UserGroupResolver::getAccessibleGroups()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 19
rs 9.2
cc 4
eloc 12
nc 4
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\Service;
13
14
use DP\Core\UserBundle\Entity\User;
15
use Symfony\Component\Security\Core\SecurityContextInterface;
16
use DP\Core\UserBundle\Entity\GroupRepository;
17
18
class UserGroupResolver
19
{
20
    /**
21
     * @var GroupRepository
22
     */
23
    protected $groupRepo;
24
25
    /**
26
     * @var SecurityContextInterface
27
     */
28
    protected $context;
29
    
30
    public function __construct(GroupRepository $groupRepo, SecurityContextInterface $context)
31
    {
32
        $this->groupRepo = $groupRepo;
33
        $this->context   = $context;
34
    }
35
    
36
    public function getAccessibleGroups()
37
    {
38
        $groups = [];
39
        $user   = $this->context->getToken()->getUser();
40
41
        if ($this->context->isGranted(User::ROLE_SUPER_ADMIN)) {
42
            $groups = $this->groupRepo
43
                ->getChildren(null);
44
        }
45
        elseif ($this->context->isGranted(User::ROLE_ADMIN)) {
46
            $groups = $this->groupRepo
47
                ->getChildren($user->getGroup(), false, null, "asc", true);
48
        }
49
        elseif ($user->getGroup() !== null) {
50
            $groups = [$user->getGroup()];
51
        }
52
53
        return $groups;
54
    }
55
    
56
    public function getAccessibleGroupsId()
57
    {
58
        $ids = [];
59
        $groups = $this->getAccessibleGroups();
60
        
61
        foreach ($groups AS $group) {
62
            $ids[] = $group->getId();
63
        }
64
        
65
        return $ids;
66
    }
67
}
68