Я сомневаюсь, как ограничить в Routes.php на основе роли из этой таблицы:
Код: Выделить всё
class CreateUserRolesTable extends Migration
{
public function up()
{
Schema::create('user_roles', function (Blueprint $table) {
$table->id();
$table->string('email')->unique();
$table->string('role');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('user_roles');
}
}
Код: Выделить всё
class CheckUserRole
{
public function handle($request, Closure $next, ...$roles)
{
// Check if the user is authenticated
if (Auth::check()) {
// Retrieve the user's role from the user_roles table
$user = Auth::user();
$userRole = DB::table('user_roles')->where('email', $user->email)->value('role');
// Check if the user's role is one of the allowed roles
if (in_array($userRole, $roles)) {
return $next($request);
}
}
// Redirect or handle unauthorized access
return redirect()->route('login'); // Redirect to login route if the user is not authorized
}
}
Код: Выделить всё
Route::group(['middleware' => ['role:Supervisor']], function () {
// Define routes for the supervisor role...
});
я ожидаю чтобы иметь возможность ограничивать маршруты, поскольку у меня нет доступа к пользовательской модели или промежуточному программному обеспечению, он просто привязывается и, если соответствует, извлекает электронную почту и сохраняет ее в сеансе
Подробнее здесь: https://stackoverflow.com/questions/784 ... on-laravel
Мобильная версия