Failed Conditions
Push — issue#666 ( f415d0...521a08 )
by Guilherme
12:02
created

SecurityHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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