Skip to content

Commit

Permalink
Merge pull request #262 from dbarzin/dev
Browse files Browse the repository at this point in the history
Add history
  • Loading branch information
dbarzin authored Feb 6, 2025
2 parents 2a953d4 + 448eb0c commit 3b51955
Show file tree
Hide file tree
Showing 21 changed files with 744 additions and 55 deletions.
102 changes: 102 additions & 0 deletions app/Http/Controllers/AuditLogsController.php
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'));
}
}
4 changes: 4 additions & 0 deletions app/Http/Controllers/ControlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ public function index(Request $request)
);
}

// get action plan associated
$controls = $controls-> leftjoin("actions","actions.control_id","=","c1.id");

// Query DB
$controls = $controls
->select([
Expand All @@ -265,6 +268,7 @@ public function index(Request $request)
'c1.realisation_date',
'c1.score as score',
'c1.status',
'actions.id as action_id',
'c2.id as next_id',
'c2.plan_date as next_date',
])
Expand Down
3 changes: 3 additions & 0 deletions app/Models/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace App\Models;

use App\Traits\Auditable;
use Illuminate\Database\Eloquent\Model;

class Action extends Model
{
use Auditable;

public static $searchable = [
'reference',
'type',
Expand Down
49 changes: 49 additions & 0 deletions app/Models/AuditLog.php
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');
}
}
8 changes: 8 additions & 0 deletions app/Models/Control.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace App\Models;

use App\Traits\Auditable;
use Illuminate\Database\Eloquent\Model;

class Control extends Model
{
use Auditable;

public static $searchable = [
'name',
'objective',
Expand Down Expand Up @@ -46,6 +49,11 @@ public function measures()
return $this->belongsToMany(Measure::class)->orderBy('clause');
}

public function actionPlan()
{
return DB::table('actions')->select('id')->where("control_id",'=',$this->id)->get();
}

public function owners()
{
return $this->belongsToMany(User::class, 'control_user', 'control_id')->orderBy('name');
Expand Down
4 changes: 4 additions & 0 deletions app/Models/Measure.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

namespace App\Models;

use App\Traits\Auditable;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Measure extends Model
{
use Auditable;

public static $searchable = [
'name',
'clause',
Expand Down
4 changes: 3 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

namespace App\Models;

use App\Traits\Auditable;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;

class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
use HasApiTokens, HasFactory, Notifiable, Auditable;

/**
* The attributes that are mass assignable.
Expand Down
36 changes: 36 additions & 0 deletions app/Traits/Auditable.php
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 database/migrations/2025_02_04_064646_create_audit_logs_table.php
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');
}

};
28 changes: 28 additions & 0 deletions database/migrations/2025_02_05_121035_cleanup.php
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();
});
}
};
2 changes: 2 additions & 0 deletions resources/lang/en/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
'delete' => 'Delete',
'download' => 'Download',
'edit' => 'Edit',
'history' => 'History',
'import' => 'Import',
'make' => 'Make',
'new' => 'New',
'plan' => 'Plan',
'reject' => 'Reject',
'save' => 'Save',
'show' => 'Show',
'test' => 'Test',
'unplan' => 'Unplan',
'validate' => 'Validate',
Expand Down
12 changes: 12 additions & 0 deletions resources/lang/en/cruds.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,18 @@
'index' => 'Import',
'title' => 'Import security measures',
],
'log' => [
'index' => 'List of logs',
'title' => 'Log',
'history' => 'Change history',
'action' => 'Action',
'subject_type' => 'Type',
'subject_id' => 'ID',
'user' => 'User',
'host' => 'Host',
'timestamp' => 'Timestamp',
'properties' => 'Data'
],
'login' => [
'title' => 'Enter a password',
'identification' => 'Login',
Expand Down
2 changes: 2 additions & 0 deletions resources/lang/fr/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
'delete' => 'Supprimer',
'download' => 'Télécharger',
'edit' => 'Modifier',
'history' => 'Historique',
'import' => 'Importer',
'make' => 'Faire',
'new' => 'Nouveau',
'plan' => 'Planifier',
'reject' => "Rejeter",
'save' => 'Sauver',
'show' => 'Voir',
'unplan' => 'Déplanifier',
'validate' => 'Valider',

Expand Down
Loading

0 comments on commit 3b51955

Please sign in to comment.