Completed
Push — master ( a1a590...8f8784 )
by Julito
18:16 queued 16s
created

GroupVoter::setGroupManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Security\Authorization\Voter;
6
7
use Chamilo\CoreBundle\Repository\CourseRepository;
8
use Chamilo\CourseBundle\Entity\CGroupInfo;
9
use Chamilo\CourseBundle\Repository\CGroupInfoRepository;
10
use Doctrine\ORM\EntityManager;
11
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
12
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
13
use Symfony\Component\Security\Core\Security;
14
use Symfony\Component\Security\Core\User\UserInterface;
15
16
/**
17
 * Class GroupVoter.
18
 */
19
class GroupVoter extends Voter
20
{
21
    public const VIEW = 'VIEW';
22
    public const EDIT = 'EDIT';
23
    public const DELETE = 'DELETE';
24
25
    private $entityManager;
26
    private $courseManager;
27
    private $groupManager;
28
    private $security;
29
30
    public function __construct(
31
        EntityManager $entityManager,
32
        CourseRepository $courseManager,
33
        CGroupInfoRepository $groupManager,
34
        Security $security
35
    ) {
36
        $this->entityManager = $entityManager;
37
        $this->courseManager = $courseManager;
38
        $this->groupManager = $groupManager;
39
        $this->security = $security;
40
    }
41
42
    protected function supports(string $attribute, $subject): bool
43
    {
44
        $options = [
45
            self::VIEW,
46
            self::EDIT,
47
            self::DELETE,
48
        ];
49
50
        // if the attribute isn't one we support, return false
51
        if (!in_array($attribute, $options)) {
52
            return false;
53
        }
54
55
        // only vote on Post objects inside this voter
56
        if (!$subject instanceof CGroupInfo) {
57
            return false;
58
        }
59
60
        return true;
61
    }
62
63
    protected function voteOnAttribute(string $attribute, $group, TokenInterface $token): bool
64
    {
65
        $user = $token->getUser();
66
67
        // make sure there is a user object (i.e. that the user is logged in)
68
        if (!$user instanceof UserInterface) {
69
            return false;
70
        }
71
72
        if (false == $group) {
73
            return false;
74
        }
75
76
        // Admins have access to everything
77
        if ($this->security->isGranted('ROLE_ADMIN')) {
78
            return true;
79
        }
80
81
        $groupInfo = [
82
            'id' => $group->getId(),
83
            'session_id' => 0,
84
            'status' => $group->getStatus(),
85
        ];
86
87
        // Legacy
88
        return \GroupManager::userHasAccessToBrowse($user->getId(), $groupInfo);
89
90
        switch ($attribute) {
0 ignored issues
show
Unused Code introduced by
SwitchNode is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
91
            case self::VIEW:
92
                if (!$group->hasUserInCourse($user, $course)) {
93
                    $user->addRole(ResourceNodeVoter::ROLE_CURRENT_SESSION_COURSE_STUDENT);
94
95
                    return true;
96
                }
97
98
                break;
99
            case self::EDIT:
100
            case self::DELETE:
101
                if (!$session->hasCoachInCourseWithStatus($user, $course)) {
102
                    $user->addRole(ResourceNodeVoter::ROLE_CURRENT_SESSION_COURSE_TEACHER);
103
104
                    return true;
105
                }
106
107
                break;
108
        }
109
        dump("You don't have access to this group!!");
0 ignored issues
show
Bug introduced by
The function dump was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
        /** @scrutinizer ignore-call */ 
110
        dump("You don't have access to this group!!");
Loading history...
110
111
        return false;
112
    }
113
}
114