| Code Coverage
| Lines | Functions and Methods | Classes and Traits
Total | 85.71% covered (warning) | 85.71% | 12 / 14 | 50.00% covered (danger) | 50.00% | 1 / 2 | CRAP | 0.00% covered (danger) | 0.00% | 0 / 1
AppRoleFilter | 85.71% covered (warning) | 85.71% | 12 / 14 | 50.00% covered (danger) | 50.00% | 1 / 2 | 6.10 | 0.00% covered (danger) | 0.00% | 0 / 1
before | 84.62% covered (warning) | 84.62% | 11 / 13 | 0.00% covered (danger) | 0.00% | 0 / 1 | 5.09 |
after | 100.00% covered (success) | 100.00% | 1 / 1 | 100.00% covered (success) | 100.00% | 1 / 1 | 1 |

1 | <?php
2 |
3 | namespace App\Filters;
4 |
5 | use CodeIgniter\Filters\FilterInterface;
6 | use CodeIgniter\HTTP\RequestInterface;
7 | use CodeIgniter\HTTP\ResponseInterface;
8 |
9 | class AppRoleFilter implements FilterInterface
10 | {
11 | public function before(RequestInterface $request, $arguments = null)
12 | {
13 | if (! auth()->loggedIn()) {
14 | return redirect()->route('login');
15 | }
16 |
17 | $roles = array_filter((array) $arguments);
18 | if ($roles === []) {
19 | return null;
20 | }
21 |
22 | $userId = auth()->id();
23 |
24 | if ($userId === null || ! service('authorization')->userHasRole((int) $userId, $roles)) {
25 | return service('response')
26 | ->setStatusCode(ResponseInterface::HTTP_FORBIDDEN)
27 | ->setBody(view('errors/html/error_403', [
28 | 'message' => 'Nu ai permisiunea necesara pentru aceasta sectiune.',
29 | ]));
30 | }
31 |
32 | return null;
33 | }
34 |
35 | public function after(RequestInterface $request, ResponseInterface $response, $arguments = null): void
36 | {
37 | }
38 | }