From e91b2dbe632e3ec48c5feacf87347d3d0284c8fd Mon Sep 17 00:00:00 2001 From: Alfonso Rafael Solis Rangel Date: Mon, 10 Jun 2024 09:01:24 -0600 Subject: [PATCH] Middleware de roles --- app/Http/Middleware/CheckRoles.php | 32 ++++++++++++++++++++++++++++++ bootstrap/app.php | 2 ++ 2 files changed, 34 insertions(+) create mode 100644 app/Http/Middleware/CheckRoles.php diff --git a/app/Http/Middleware/CheckRoles.php b/app/Http/Middleware/CheckRoles.php new file mode 100644 index 0000000..6fc699e --- /dev/null +++ b/app/Http/Middleware/CheckRoles.php @@ -0,0 +1,32 @@ +user(); + + foreach($roles as $role){ + if($user->hasRole($role)){ + return $next($request); + } + } + + return abort(401); + }else{ + return $next($request); + } + } +} diff --git a/bootstrap/app.php b/bootstrap/app.php index a4ae0f1..85966f9 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -5,6 +5,7 @@ use Illuminate\Foundation\Configuration\Middleware; use App\Http\Middleware\CheckBanned; +use App\Http\Middleware\CheckRoles; return Application::configure(basePath: dirname(__DIR__)) ->withRouting( @@ -15,6 +16,7 @@ ) ->withMiddleware(function (Middleware $middleware) { $middleware->append(CheckBanned::class); + $middleware->append(CheckRoles::class); }) ->withExceptions(function (Exceptions $exceptions) { // -- GitLab