-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #262 from dbarzin/dev
Add history
- Loading branch information
Showing
21 changed files
with
744 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\AuditLog; | ||
use App\Http\Controllers\Controller; | ||
use Gate; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Response; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Auth; | ||
|
||
class AuditLogsController extends Controller | ||
{ | ||
public function index(Request $request) | ||
{ | ||
// Only for admin and users | ||
abort_if( | ||
(Auth::User()->role !== 1) && (Auth::User()->role !== 2), | ||
Response::HTTP_FORBIDDEN, | ||
'403 Forbidden' | ||
); | ||
|
||
$logs = DB::table('audit_logs') | ||
->select( | ||
'audit_logs.id', | ||
'description', | ||
'subject_type', | ||
'subject_id', | ||
'users.name', | ||
'user_id', | ||
'host', | ||
'audit_logs.created_at' | ||
) | ||
->join('users', 'users.id', '=', 'user_id') | ||
->orderBy('audit_logs.id', 'desc')->paginate(100); | ||
|
||
return view('logs.index', ['logs' => $logs]); | ||
} | ||
|
||
public function show(int $id) | ||
{ | ||
// Only for admin and users | ||
abort_if( | ||
(Auth::User()->role !== 1) && (Auth::User()->role !== 2), | ||
Response::HTTP_FORBIDDEN, | ||
'403 Forbidden' | ||
); | ||
|
||
// Get audit Log | ||
$auditLog = AuditLog::find($id); | ||
|
||
// Control not found | ||
abort_if($auditLog === null, Response::HTTP_NOT_FOUND, '404 Not Found'); | ||
|
||
return view('logs.show', compact('auditLog')); | ||
} | ||
|
||
public function history(int $id) | ||
{ | ||
// Only for admin and users | ||
abort_if( | ||
(Auth::User()->role !== 1) && (Auth::User()->role !== 2), | ||
Response::HTTP_FORBIDDEN, | ||
'403 Forbidden' | ||
); | ||
|
||
// Get audit Log | ||
$auditLog = AuditLog::find($id); | ||
|
||
abort_if($auditLog === null, 400, '400 log not found'); | ||
|
||
// Get the list | ||
$auditLogs = | ||
DB::table('audit_logs') | ||
->select( | ||
'audit_logs.id', | ||
'description', | ||
'subject_type', | ||
'subject_id', | ||
'users.name', | ||
'user_id', | ||
'host', | ||
'properties', | ||
'audit_logs.created_at' | ||
) | ||
->join('users', 'users.id', '=', 'user_id') | ||
->where('subject_id', $auditLog->subject_id) | ||
->where('subject_type', $auditLog->subject_type) | ||
->orderBy('audit_logs.id') | ||
->get(); | ||
|
||
abort_if($auditLogs->isEmpty(), 404, 'Not found'); | ||
|
||
// JSON decode all properties | ||
foreach ($auditLogs as $auditLog) { | ||
$auditLog->properties = json_decode(trim(stripslashes($auditLog->properties), '"')); | ||
} | ||
|
||
return view('logs.history', compact('auditLogs')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use DateTimeInterface; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Str; | ||
|
||
/** | ||
* App\AuditLog | ||
*/ | ||
class AuditLog extends Model | ||
{ | ||
public $table = 'audit_logs'; | ||
|
||
protected $fillable = [ | ||
'description', | ||
'subject_id', | ||
'subject_type', | ||
'user_id', | ||
'properties', | ||
'host', | ||
]; | ||
|
||
protected $casts = [ | ||
'properties' => 'collection', | ||
]; | ||
|
||
public function user() | ||
{ | ||
return $this->belongsTo(User::class, 'user_id'); | ||
} | ||
|
||
public function subjectURL() | ||
{ | ||
// Trouver la dernière occurrence de "\" | ||
$position = strrpos($this->subject_type, "\\"); | ||
|
||
// Extraire ce qui suit si "\" est trouvé | ||
$resultat = ($position !== false) ? substr($this->subject_type, $position + 1) : $this->subject_type; | ||
|
||
return $resultat; | ||
} | ||
|
||
protected function serializeDate(DateTimeInterface $date) | ||
{ | ||
return $date->format('Y-m-d H:i:s'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace App\Traits; | ||
|
||
use App\Models\AuditLog; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
trait Auditable | ||
{ | ||
public static function bootAuditable() | ||
{ | ||
static::created(function (Model $model) { | ||
static::audit('created', $model); | ||
}); | ||
|
||
static::updated(function (Model $model) { | ||
static::audit('updated', $model); | ||
}); | ||
|
||
static::deleted(function (Model $model) { | ||
static::audit('deleted', $model); | ||
}); | ||
} | ||
|
||
protected static function audit($description, $model) | ||
{ | ||
AuditLog::create([ | ||
'description' => $description, | ||
'subject_id' => $model->id ?? null, | ||
'subject_type' => $model::class ?? null, | ||
'user_id' => auth()->id() ?? null, | ||
'properties' => substr($model, 0, 65534) ?? null, | ||
'host' => request()->ip() ?? null, | ||
]); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
database/migrations/2025_02_04_064646_create_audit_logs_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('audit_logs', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->text('description'); | ||
$table->unsignedInteger('subject_id')->nullable(); | ||
$table->string('subject_type')->nullable(); | ||
$table->unsignedInteger('user_id')->nullable(); | ||
$table->text('properties')->nullable(); | ||
$table->string('host', 45)->nullable(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('audit_logs'); | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::table('controls', function (Blueprint $table) { | ||
$table->dropColumn("site"); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::table('controls', function (Blueprint $table) { | ||
$table->string('site')->nullable(); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.