Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
30 / 30 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
LogsController | |
100.00% |
30 / 30 |
|
100.00% |
2 / 2 |
6 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
index | |
100.00% |
29 / 29 |
|
100.00% |
1 / 1 |
5 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Engelsystem\Controllers\Admin; |
6 | |
7 | use Engelsystem\Controllers\BaseController; |
8 | use Engelsystem\Helpers\Authenticator; |
9 | use Engelsystem\Http\Request; |
10 | use Engelsystem\Http\Response; |
11 | use Engelsystem\Models\LogEntry; |
12 | use Engelsystem\Models\User\User; |
13 | use Illuminate\Support\Collection; |
14 | use Illuminate\Support\Str; |
15 | use Psr\Log\LogLevel; |
16 | |
17 | class LogsController extends BaseController |
18 | { |
19 | /** @var array<string> */ |
20 | protected array $permissions = [ |
21 | 'admin_log', |
22 | ]; |
23 | |
24 | protected array $levels = [ |
25 | LogLevel::ALERT, |
26 | LogLevel::CRITICAL, |
27 | LogLevel::DEBUG, |
28 | LogLevel::EMERGENCY, |
29 | LogLevel::ERROR, |
30 | LogLevel::INFO, |
31 | LogLevel::NOTICE, |
32 | LogLevel::WARNING, |
33 | ]; |
34 | |
35 | public function __construct(protected LogEntry $log, protected Response $response, protected Authenticator $auth) |
36 | { |
37 | } |
38 | |
39 | public function index(Request $request): Response |
40 | { |
41 | $searchUserId = (int) $request->input('search_user_id') ?: null; |
42 | $search = $request->input('search'); |
43 | $level = $request->input('level'); |
44 | $userId = $this->auth->user()?->id; |
45 | |
46 | if ($this->auth->can('logs.all')) { |
47 | $userId = $searchUserId; |
48 | } |
49 | |
50 | if (!in_array($level, $this->levels)) { |
51 | $level = null; |
52 | } |
53 | |
54 | $entries = $this->log->filter($search, $userId, $level); |
55 | |
56 | /** @var Collection $users */ |
57 | $users = User::with('personalData') |
58 | ->orderBy('name') |
59 | ->get() |
60 | ->mapWithKeys(function (User $u) { |
61 | return [$u->id => $u->displayName]; |
62 | }); |
63 | |
64 | $levels = array_combine($this->levels, $this->levels); |
65 | foreach ($levels as $k => $v) { |
66 | $levels[$k] = Str::ucfirst($v); |
67 | } |
68 | |
69 | return $this->response->withView( |
70 | 'admin/log.twig', |
71 | [ |
72 | 'entries' => $entries, |
73 | 'search' => $search, |
74 | 'users' => $users, |
75 | 'search_user_id' => $searchUserId, |
76 | 'level' => $level, |
77 | 'levels' => $levels, |
78 | ] |
79 | ); |
80 | } |
81 | } |