Passed
Push — issue#767 ( 376594...d97dbe )
by Guilherme
08:41
created

SecurityHelper::getTargetPersonLevel()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 12
ccs 0
cts 8
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace LoginCidadao\CoreBundle\Helper;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\HttpFoundation\Response;
7
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
8
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
9
use Symfony\Component\Routing\RouterInterface;
10
use LoginCidadao\APIBundle\Entity\ActionLogRepository;
11
use LoginCidadao\CoreBundle\Model\PersonInterface;
12
13
class SecurityHelper
14
{
15
    /** @var AuthorizationCheckerInterface */
16
    private $authChecker;
17
18
    /** @var TokenStorage */
19
    private $tokenStorage;
20
21
    /** @var ActionLogRepository */
22
    private $actionLogRepo;
23
24
    /** @var ExtremeNotificationsHelper */
25
    private $extremeNotificationsHelper;
26
27
    /** @var RouterInterface */
28
    private $router;
29
30
    /** @var string */
31
    private $cookieRememberMeName;
32
33 14
    public function __construct(
34
        AuthorizationCheckerInterface $authChecker,
35
        TokenStorage $tokenStorage,
36
        ActionLogRepository $actionLogRepo,
37
        ExtremeNotificationsHelper $extremeNotificationsHelper,
38
        RouterInterface $router,
39
        $cookieRememberMeName
40
    ) {
41 14
        $this->authChecker = $authChecker;
42 14
        $this->tokenStorage = $tokenStorage;
43 14
        $this->actionLogRepo = $actionLogRepo;
44 14
        $this->extremeNotificationsHelper = $extremeNotificationsHelper;
45 14
        $this->router = $router;
46 14
        $this->cookieRememberMeName = $cookieRememberMeName;
47 14
    }
48
49
    public function getLoggedInUserLevel()
50
    {
51
        $level = 0;
52
        foreach ($this->getRoleMapping() as $role => $lvl) {
53
            if ($this->authChecker->isGranted($role)) {
54
                $level = $lvl;
55
                break;
56
            }
57
        }
58
59
        return $level;
60
    }
61
62
    public function getTargetPersonLevel(PersonInterface $person)
63
    {
64
        $roles = $person->getRoles();
65
        $level = 0;
66
        foreach ($this->getRoleMapping() as $role => $lvl) {
67
            if (in_array($role, $roles)) {
68
                $level = $lvl;
69
                break;
70
            }
71
        }
72
73
        return $level;
74
    }
75
76
    public function getRoleLevel($role)
77
    {
78
        $map = $this->getRoleMapping();
79
        if (array_key_exists($role, $map)) {
80
            return $map[$role];
81
        } else {
82
            return max(array_values($map));
83
        }
84
    }
85
86
    private function getRoleMapping()
87
    {
88
        $map = array(
89
            'ROLE_SUPER_ADMIN' => 4,
90
            'ROLE_ADMIN' => 3,
91
            'ROLE_SUPER_USER' => 2,
92
            'ROLE_DEV' => 1,
93
            'ROLE_USER' => 0,
94
        );
95
        arsort($map);
96
97
        return $map;
98
    }
99
100
    public function checkPendingImpersonateReport(PersonInterface $impersonator)
101
    {
102
        $count = $this->actionLogRepo->countImpersonatonsWithoutReports($impersonator);
103
104
        if ($count <= 0) {
105
            return;
106
        }
107
108
        $url = $this->router->generate('lc_admin_impersonation_report_index');
109
110
        $parameters = array('%url%' => $url, '%count%' => $count);
111
        $message = 'admin.impersonation_report.pending.notification';
112
        $this->extremeNotificationsHelper
113
            ->addTransChoice($message, $count, $parameters);
114
    }
115
116
    /**
117
     * @param Request $request
118
     * @param Response $response
119
     * @return Response
120
     */
121
    public function logout(Request $request, Response $response)
122
    {
123
        $this->tokenStorage->setToken(null);
124
        $request->getSession()->invalidate();
125
126
        $cookieNames = [
127
            $this->cookieRememberMeName,
128
        ];
129
        foreach ($cookieNames as $cookieName) {
130
            $response->headers->clearCookie($cookieName);
131
        }
132
133
        return $response;
134
    }
135
136
    /**
137
     * Checks if the attributes are granted against the current authentication token and optionally supplied object.
138
     *
139
     * @param mixed $attributes
140
     * @param mixed $object
141
     *
142
     * @return bool
143
     */
144
    public function isGranted($attributes, $object = null)
145
    {
146
        return $this->authChecker->isGranted($attributes, $object);
147
    }
148
149
    /**
150
     * @return PersonInterface|null
151
     */
152
    public function getUser()
153
    {
154
        try {
155
            /** @var PersonInterface $user */
156
            $user = $this->tokenStorage->getToken()->getUser();
157
158
            if (!$user instanceof PersonInterface) {
0 ignored issues
show
introduced by
$user is always a sub-type of LoginCidadao\CoreBundle\Model\PersonInterface.
Loading history...
159
                $user = null;
160
            }
161
        } catch (\Exception $e) {
162
            $user = null;
163
        }
164
165
        return $user;
166
    }
167
}
168