Skip to content

Commit 5154d07

Browse files
authored
Merge pull request #925 from dbarzin/dev
improve audit logs
2 parents 43e7049 + c5183ae commit 5154d07

File tree

5 files changed

+134
-27
lines changed

5 files changed

+134
-27
lines changed

app/Http/Controllers/Admin/AuditLogsController.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,7 @@ public function history(Request $request)
4343
{
4444
abort_if(Gate::denies('audit_log_show'), Response::HTTP_FORBIDDEN, '403 Forbidden');
4545

46-
// Get all the input as an InputBag object
47-
$input = $request->input();
48-
49-
// Check if input is not empty
50-
if (!empty($input)) {
51-
// Get the first key
52-
$firstKey = array_keys($input)[0];
53-
} else {
54-
return "404";
55-
}
56-
57-
// Get the audit log
58-
$auditLog = AuditLog::find($firstKey);
46+
abort_if(($request->id==null)||($request->type==null), 500, '500 missing parameters');
5947

6048
// Get the list
6149
$auditLogs =
@@ -72,8 +60,13 @@ public function history(Request $request)
7260
'audit_logs.created_at'
7361
)
7462
->join('users', 'users.id', '=', 'user_id')
75-
->where('subject_id',$auditLog->subject_id)
76-
->orderBy('id')->get();
63+
->where('subject_id', $request->id)
64+
->where('subject_type', ($request->type))
65+
->orderBy('audit_logs.id')
66+
->get();
67+
68+
69+
abort_if($auditLogs->isEmpty(), 404, 'Not found');
7770

7871
// JSON decode all properties
7972
foreach($auditLogs as $auditLog) {
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
@extends('layouts.admin')
2+
@section('content')
3+
<div class="form-group">
4+
<a class="btn btn-default" href="{{ route('admin.audit-logs.index') }}">
5+
{{ trans('global.back_to_list') }}
6+
</a>
7+
</div>
8+
<div class="card">
9+
<div class="card-header">
10+
{{ trans('global.show') }} {{ trans('cruds.auditLog.title') }}
11+
</div>
12+
13+
<div class="card-body table-responsive">
14+
<table class="table table-bordered table-striped" style="table-layout: fixed; width: {{ $auditLogs->count() * 300 + 150 }}px;">
15+
<tbody>
16+
<tr>
17+
<th style="width: 150px;">
18+
{{ trans('cruds.auditLog.fields.subject_id') }}
19+
</th>
20+
<td>
21+
<a href="{{ \App\AuditLog::subject_url($auditLogs->first()->subject_type) }}/{{ $auditLogs->first()->subject_id }}">
22+
{{ $auditLogs->first()->subject_id }}
23+
</a>
24+
</td>
25+
</tr>
26+
<tr>
27+
<th>
28+
id
29+
</th>
30+
@foreach($auditLogs as $auditLog)
31+
<td>
32+
<a href="{{ route('admin.audit-logs.show', $auditLog->id) }}">
33+
{{ $auditLog->id }}
34+
</a>
35+
</td>
36+
@endforeach
37+
</tr>
38+
<tr>
39+
<th>
40+
{{ trans('cruds.auditLog.fields.description') }}
41+
</th>
42+
@foreach($auditLogs as $auditLog)
43+
<td>
44+
{{ $auditLog->description }}
45+
</td>
46+
@endforeach
47+
</tr>
48+
<tr>
49+
<th>
50+
{{ trans('cruds.auditLog.fields.user_id') }}
51+
</th>
52+
@foreach($auditLogs as $auditLog)
53+
<td>
54+
<a href="{{ route('admin.users.show', $auditLog->user_id) }}">
55+
{{ $auditLog->name }}
56+
</a>
57+
</td>
58+
@endforeach
59+
</tr>
60+
@foreach($auditLog->properties as $key => $value)
61+
<tr>
62+
<th>[{{ $key }}]</th>
63+
@php $previous = null; @endphp
64+
@foreach($auditLogs as $auditLog2)
65+
@php
66+
$value = $auditLog2->properties->{$key};
67+
@endphp
68+
<td {!! (($loop->first)||($value==$previous)) ? "" : "class='bg-success'" !!}>
69+
@if ((gettype($value)=="string")&&(strlen($value)>100))
70+
{{ substr($value,0,100) }}
71+
@else
72+
{{ $value }}
73+
@endif
74+
</td>
75+
@php $previous = $value; @endphp
76+
@endforeach
77+
</tr>
78+
@endforeach
79+
<tr>
80+
<th>
81+
{{ trans('cruds.auditLog.fields.host') }}
82+
</th>
83+
@foreach($auditLogs as $auditLog)
84+
<td>
85+
{{ $auditLog->host }}
86+
</td>
87+
@endforeach
88+
</tr>
89+
<tr>
90+
<th>
91+
{{ trans('global.created_at') }}
92+
</th>
93+
@foreach($auditLogs as $auditLog)
94+
<td>
95+
{{ $auditLog->created_at }}
96+
</td>
97+
@endforeach
98+
</tr>
99+
</tbody>
100+
</table>
101+
</div>
102+
</div>
103+
<div class="form-group">
104+
<a class="btn btn-default" href="{{ route('admin.audit-logs.index') }}">
105+
{{ trans('global.back_to_list') }}
106+
</a>
107+
</div>
108+
@endsection
109+
@section('styles')
110+
<style>
111+
/* Style to enable horizontal scroll */
112+
.table-responsive {
113+
overflow-x: auto;
114+
}
115+
</style>
116+
@endsection

resources/views/admin/auditLogs/index.blade.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@
7272
<a class="btn btn-xs btn-primary" href="{{ route('admin.audit-logs.show', $log->id) }}">
7373
{{ trans('global.view') }}
7474
</a>
75-
<a class="btn btn-xs btn-secondary" href="{{ route('admin.history', $log->id) }}">
75+
<a class="btn btn-xs btn-secondary" href="{{ route('admin.history',
76+
['type' => $log->subject_type, 'id' => $log->subject_id]) }}">
7677
{{ trans('global.history') }}
7778
</a>
7879
</td>

resources/views/admin/auditLogs/show.blade.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,28 @@
2323
</tr>
2424
<tr>
2525
<th>
26-
{{ trans('cruds.auditLog.fields.description') }}
26+
{{ trans('cruds.auditLog.fields.subject_type') }}
2727
</th>
2828
<td>
29-
{{ $auditLog->description }}
29+
{{ $auditLog->subject_type }}
3030
</td>
3131
</tr>
3232
<tr>
3333
<th>
3434
{{ trans('cruds.auditLog.fields.subject_id') }}
3535
</th>
3636
<td>
37-
{{ $auditLog->subject_id }}
37+
<a href="{{ \App\AuditLog::subject_url($auditLog->subject_type) }}/{{ $auditLog->subject_id }}">
38+
{{ $auditLog->subject_id }}
39+
</a>
3840
</td>
3941
</tr>
4042
<tr>
4143
<th>
42-
{{ trans('cruds.auditLog.fields.subject_type') }}
44+
{{ trans('cruds.auditLog.fields.description') }}
4345
</th>
4446
<td>
45-
{{ $auditLog->subject_type }}
47+
{{ $auditLog->description }}
4648
</td>
4749
</tr>
4850
<tr>
@@ -61,11 +63,6 @@
6163
</th>
6264
<td>
6365
{{ $auditLog->properties }}
64-
<table>
65-
@foreach($auditLog->properties as $key => $value)
66-
<tr><td>{{ $key }}</td><td>{{ $value }}</td>
67-
@endforeach
68-
</table>
6966
</td>
7067
</tr>
7168
<tr>

routes/web.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@
287287

288288
// Audit Logs
289289
Route::resource('audit-logs', Admin\AuditLogsController::class, ['except' => ['create', 'store', 'update', 'destroy']]);
290-
Route::get('history', [Admin\AuditLogsController::class,'history'])->name('history');
290+
Route::get('history/{type}/{id}', [Admin\AuditLogsController::class,'history'])->name('history');
291291

292292
// Macro Processuses
293293
Route::resource('macro-processuses', Admin\MacroProcessusController::class);

0 commit comments

Comments
 (0)