Passed
Push — issue#767 ( 0909e2...b3675b )
by Guilherme
05:08
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 Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\HttpFoundation\Response;
7
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
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 TokenStorageInterface */
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 11
    public function __construct(
34
        AuthorizationCheckerInterface $authChecker,
35
        TokenStorageInterface $tokenStorage,
36
        ActionLogRepository $actionLogRepo,
37
        ExtremeNotificationsHelper $extremeNotificationsHelper,
38
        RouterInterface $router,
39
        $cookieRememberMeName
40
    ) {
41 11
        $this->authChecker = $authChecker;
42 11
        $this->tokenStorage = $tokenStorage;
43 11
        $this->actionLogRepo = $actionLogRepo;
44 11
        $this->extremeNotificationsHelper = $extremeNotificationsHelper;
45 11
        $this->router = $router;
46 11
        $this->cookieRememberMeName = $cookieRememberMeName;
47 11
    }
48
49 2
    public function getLoggedInUserLevel()
50
    {
51 2
        foreach ($this->getRoleMapping() as $role => $lvl) {
52 2
            if ($this->authChecker->isGranted($role)) {
53 2
                return $lvl;
54
            }
55
        }
56
57 1
        return 0;
58
    }
59
60 1
    public function getTargetPersonLevel(PersonInterface $person)
61
    {
62 1
        $roles = $person->getRoles();
63 1
        foreach ($this->getRoleMapping() as $role => $lvl) {
64 1
            if (in_array($role, $roles)) {
65 1
                return $lvl;
66
            }
67
        }
68
69 1
        return 0;
70
    }
71
72 1
    public function getRoleLevel($role)
73
    {
74 1
        $map = $this->getRoleMapping();
75 1
        if (array_key_exists($role, $map)) {
76 1
            return $map[$role];
77
        } else {
78 1
            return max(array_values($map));
79
        }
80
    }
81
82 4
    private function getRoleMapping()
83
    {
84
        $map = [
85 4
            'ROLE_SUPER_ADMIN' => 4,
86
            'ROLE_ADMIN' => 3,
87
            'ROLE_SUPER_USER' => 2,
88
            'ROLE_DEV' => 1,
89
            'ROLE_USER' => 0,
90
        ];
91 4
        arsort($map);
92
93 4
        return $map;
94
    }
95
96 2
    public function checkPendingImpersonateReport(PersonInterface $impersonator)
97
    {
98 2
        $count = $this->actionLogRepo->countImpersonatonsWithoutReports($impersonator);
99
100 2
        if ($count <= 0) {
101 1
            return;
102
        }
103
104 1
        $url = $this->router->generate('lc_admin_impersonation_report_index');
105
106 1
        $parameters = ['%url%' => $url, '%count%' => $count];
107 1
        $message = 'admin.impersonation_report.pending.notification';
108 1
        $this->extremeNotificationsHelper
109 1
            ->addTransChoice($message, $count, $parameters);
110 1
    }
111
112
    /**
113
     * @param Request $request
114
     * @param Response $response
115
     * @return Response
116
     */
117 1
    public function logout(Request $request, Response $response)
118
    {
119 1
        $this->tokenStorage->setToken(null);
120 1
        $request->getSession()->invalidate();
121
122 1
        $cookieNames = [$this->cookieRememberMeName];
123 1
        foreach ($cookieNames as $cookieName) {
124 1
            $response->headers->clearCookie($cookieName);
125
        }
126
127 1
        return $response;
128
    }
129
130
    /**
131
     * Checks if the attributes are granted against the current authentication token and optionally supplied object.
132
     *
133
     * @param mixed $attributes
134
     * @param mixed $object
135
     *
136
     * @return bool
137
     */
138 1
    public function isGranted($attributes, $object = null)
139
    {
140 1
        return $this->authChecker->isGranted($attributes, $object);
141
    }
142
143
    /**
144
     * @return PersonInterface|null
145
     */
146 3
    public function getUser()
147
    {
148
        try {
149
            /** @var PersonInterface $user */
150 3
            $user = $this->tokenStorage->getToken()->getUser();
151
152 2
            if (!$user instanceof PersonInterface) {
0 ignored issues
show
introduced by
$user is always a sub-type of LoginCidadao\CoreBundle\Model\PersonInterface.
Loading history...
153 2
                $user = null;
154
            }
155 1
        } catch (\Exception $e) {
156 1
            $user = null;
157
        }
158
159 3
        return $user;
160
    }
161
}
162