user roles, bug fixes
This commit is contained in:
28
app/Http/Middleware/CheckRole.php
Normal file
28
app/Http/Middleware/CheckRole.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class CheckRole
|
||||
{
|
||||
public function handle(Request $request, Closure $next, ...$roles)
|
||||
{
|
||||
// Check if the user is logged in
|
||||
if (!auth()->check()) {
|
||||
return redirect('login');
|
||||
}
|
||||
|
||||
$user = auth()->user(); // Assuming StaffMember acts as your Authenticatable user
|
||||
|
||||
// Check if the user has any of the allowed roles
|
||||
foreach ($roles as $role) {
|
||||
if ($user->hasRole($role)) {
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
|
||||
abort(403, 'Unauthorized action.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user