feat: implementation des logs de connexion et correction du chemin de stockage des documents
This commit is contained in:
23
app/Console/Commands/CleanupLoginLogs.php
Normal file
23
app/Console/Commands/CleanupLoginLogs.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Attributes\Description;
|
||||
use Illuminate\Console\Attributes\Signature;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
use App\Models\LoginLog;
|
||||
|
||||
#[Signature('app:cleanup-login-logs')]
|
||||
#[Description('Supprime les logs de connexion datant de plus de 1 mois')]
|
||||
class CleanupLoginLogs extends Command
|
||||
{
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$count = LoginLog::where('login_at', '<', now()->subMonth())->delete();
|
||||
$this->info("{$count} logs de connexion supprimés.");
|
||||
}
|
||||
}
|
||||
27
app/Http/Controllers/Admin/LoginLogController.php
Normal file
27
app/Http/Controllers/Admin/LoginLogController.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
use App\Models\LoginLog;
|
||||
use Inertia\Inertia;
|
||||
|
||||
class LoginLogController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
if (!auth()->user()->isSuperAdmin()) {
|
||||
abort(403, 'Unauthorized. Super Admin only.');
|
||||
}
|
||||
|
||||
$logs = LoginLog::with('user.tenant')
|
||||
->orderBy('login_at', 'desc')
|
||||
->paginate(50);
|
||||
|
||||
return Inertia::render('Admin/Logs', [
|
||||
'logs' => $logs
|
||||
]);
|
||||
}
|
||||
}
|
||||
30
app/Listeners/LogSuccessfulLogin.php
Normal file
30
app/Listeners/LogSuccessfulLogin.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use Illuminate\Auth\Events\Login;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
|
||||
use App\Models\LoginLog;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class LogSuccessfulLogin
|
||||
{
|
||||
protected $request;
|
||||
|
||||
public function __construct(Request $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
public function handle(Login $event): void
|
||||
{
|
||||
LoginLog::create([
|
||||
'user_id' => $event->user->id,
|
||||
'ip_address' => $this->request->ip(),
|
||||
'user_agent' => $this->request->userAgent(),
|
||||
'login_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
16
app/Models/LoginLog.php
Normal file
16
app/Models/LoginLog.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
|
||||
#[Fillable(['user_id', 'ip_address', 'user_agent', 'login_at'])]
|
||||
class LoginLog extends Model
|
||||
{
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user