Skip to content

Commit

Permalink
Merge pull request #925 from dbarzin/dev
Browse files Browse the repository at this point in the history
improve audit logs
  • Loading branch information
dbarzin authored Oct 28, 2024
2 parents 43e7049 + c5183ae commit 5154d07
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 27 deletions.
23 changes: 8 additions & 15 deletions app/Http/Controllers/Admin/AuditLogsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,7 @@ public function history(Request $request)
{
abort_if(Gate::denies('audit_log_show'), Response::HTTP_FORBIDDEN, '403 Forbidden');

// Get all the input as an InputBag object
$input = $request->input();

// Check if input is not empty
if (!empty($input)) {
// Get the first key
$firstKey = array_keys($input)[0];
} else {
return "404";
}

// Get the audit log
$auditLog = AuditLog::find($firstKey);
abort_if(($request->id==null)||($request->type==null), 500, '500 missing parameters');

// Get the list
$auditLogs =
Expand All @@ -72,8 +60,13 @@ public function history(Request $request)
'audit_logs.created_at'
)
->join('users', 'users.id', '=', 'user_id')
->where('subject_id',$auditLog->subject_id)
->orderBy('id')->get();
->where('subject_id', $request->id)
->where('subject_type', ($request->type))
->orderBy('audit_logs.id')
->get();


abort_if($auditLogs->isEmpty(), 404, 'Not found');

// JSON decode all properties
foreach($auditLogs as $auditLog) {
Expand Down
116 changes: 116 additions & 0 deletions resources/views/admin/auditLogs/history.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
@extends('layouts.admin')
@section('content')
<div class="form-group">
<a class="btn btn-default" href="{{ route('admin.audit-logs.index') }}">
{{ trans('global.back_to_list') }}
</a>
</div>
<div class="card">
<div class="card-header">
{{ trans('global.show') }} {{ trans('cruds.auditLog.title') }}
</div>

<div class="card-body table-responsive">
<table class="table table-bordered table-striped" style="table-layout: fixed; width: {{ $auditLogs->count() * 300 + 150 }}px;">
<tbody>
<tr>
<th style="width: 150px;">
{{ trans('cruds.auditLog.fields.subject_id') }}
</th>
<td>
<a href="{{ \App\AuditLog::subject_url($auditLogs->first()->subject_type) }}/{{ $auditLogs->first()->subject_id }}">
{{ $auditLogs->first()->subject_id }}
</a>
</td>
</tr>
<tr>
<th>
id
</th>
@foreach($auditLogs as $auditLog)
<td>
<a href="{{ route('admin.audit-logs.show', $auditLog->id) }}">
{{ $auditLog->id }}
</a>
</td>
@endforeach
</tr>
<tr>
<th>
{{ trans('cruds.auditLog.fields.description') }}
</th>
@foreach($auditLogs as $auditLog)
<td>
{{ $auditLog->description }}
</td>
@endforeach
</tr>
<tr>
<th>
{{ trans('cruds.auditLog.fields.user_id') }}
</th>
@foreach($auditLogs as $auditLog)
<td>
<a href="{{ route('admin.users.show', $auditLog->user_id) }}">
{{ $auditLog->name }}
</a>
</td>
@endforeach
</tr>
@foreach($auditLog->properties as $key => $value)
<tr>
<th>[{{ $key }}]</th>
@php $previous = null; @endphp
@foreach($auditLogs as $auditLog2)
@php
$value = $auditLog2->properties->{$key};
@endphp
<td {!! (($loop->first)||($value==$previous)) ? "" : "class='bg-success'" !!}>
@if ((gettype($value)=="string")&&(strlen($value)>100))
{{ substr($value,0,100) }}
@else
{{ $value }}
@endif
</td>
@php $previous = $value; @endphp
@endforeach
</tr>
@endforeach
<tr>
<th>
{{ trans('cruds.auditLog.fields.host') }}
</th>
@foreach($auditLogs as $auditLog)
<td>
{{ $auditLog->host }}
</td>
@endforeach
</tr>
<tr>
<th>
{{ trans('global.created_at') }}
</th>
@foreach($auditLogs as $auditLog)
<td>
{{ $auditLog->created_at }}
</td>
@endforeach
</tr>
</tbody>
</table>
</div>
</div>
<div class="form-group">
<a class="btn btn-default" href="{{ route('admin.audit-logs.index') }}">
{{ trans('global.back_to_list') }}
</a>
</div>
@endsection
@section('styles')
<style>
/* Style to enable horizontal scroll */
.table-responsive {
overflow-x: auto;
}
</style>
@endsection
3 changes: 2 additions & 1 deletion resources/views/admin/auditLogs/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
<a class="btn btn-xs btn-primary" href="{{ route('admin.audit-logs.show', $log->id) }}">
{{ trans('global.view') }}
</a>
<a class="btn btn-xs btn-secondary" href="{{ route('admin.history', $log->id) }}">
<a class="btn btn-xs btn-secondary" href="{{ route('admin.history',
['type' => $log->subject_type, 'id' => $log->subject_id]) }}">
{{ trans('global.history') }}
</a>
</td>
Expand Down
17 changes: 7 additions & 10 deletions resources/views/admin/auditLogs/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,28 @@
</tr>
<tr>
<th>
{{ trans('cruds.auditLog.fields.description') }}
{{ trans('cruds.auditLog.fields.subject_type') }}
</th>
<td>
{{ $auditLog->description }}
{{ $auditLog->subject_type }}
</td>
</tr>
<tr>
<th>
{{ trans('cruds.auditLog.fields.subject_id') }}
</th>
<td>
{{ $auditLog->subject_id }}
<a href="{{ \App\AuditLog::subject_url($auditLog->subject_type) }}/{{ $auditLog->subject_id }}">
{{ $auditLog->subject_id }}
</a>
</td>
</tr>
<tr>
<th>
{{ trans('cruds.auditLog.fields.subject_type') }}
{{ trans('cruds.auditLog.fields.description') }}
</th>
<td>
{{ $auditLog->subject_type }}
{{ $auditLog->description }}
</td>
</tr>
<tr>
Expand All @@ -61,11 +63,6 @@
</th>
<td>
{{ $auditLog->properties }}
<table>
@foreach($auditLog->properties as $key => $value)
<tr><td>{{ $key }}</td><td>{{ $value }}</td>
@endforeach
</table>
</td>
</tr>
<tr>
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@

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

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

0 comments on commit 5154d07

Please sign in to comment.