我想在我的项目中创建用户管理和超级管理角色。我正试着自己学习。
create_roles_table.php
class CreateRolesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('description');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('roles');
}
}create_roles_user_table.php
class CreateRoleUserTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('role_user', function (Blueprint $table) {
$table->increments('id');
$table->integer('role_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('role_user');
}
}App\User.php
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
public function roles()
{
return $this
->belongsToMany('App\Role')
->withTimestamps();
}
public function users()
{
return $this
->belongsToMany('App\User')
->withTimestamps();
}
public function authorizeRoles($roles)
{
if ($this->hasAnyRole($roles)) {
return true;
}
abort(401, 'This action is unauthorized.');
}
public function hasAnyRole($roles)
{
if (is_array($roles)) {
foreach ($roles as $role) {
if ($this->hasRole($role)) {
return true;
}
}
} else {
if ($this->hasRole($roles)) {
return true;
}
}
return false;
}
public function hasRole($role)
{
if ($this->roles()->where(‘name’, $role)->first()) {
return true;
}
return false;
}
}Middleware\CheckRole.php
class CheckRole
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, $role)
{
if (! $request->user()->hasRole($role)) {
abort(401, 'This action is unauthorized.');
}
return $next($request);
}
}AdminController
{
public function __construct()
{
$this->middleware('auth');
$this->middleware('role:ROLE_ADMIN');
}
public function index()
{
return view('admin.home');
}
}视图/admin/admin.blde.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Admin Dashboard</div>
<div class="panel-body">
@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif
This is Admin Dashboard. You must be privileged to be here !
</div>
</div>
</div>
</div>
</div>
@endsection 路由
Route::get('/', function () {
return view('auth/login');
});
Auth::routes(['verify' => true]);
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/admin', 'AdminController@index');
Route::get('/superadmin', 'SuperAdminController@index');
Route::resource('posts', PostController::class); 我试图在这个项目中做,以便有用户,管理员和超级管理员。我在代码中添加了您看到的内容,但是我觉得我缺少了一些东西,我不知道在用户和角色的迁移中添加什么,这样它就可以识别它是管理用户还是超级管理员。目前我注册,我注册为一个用户,但我想这样做,如果一个用户是管理员,他可以访问的网页管理员和其他页面,而用户可以访问作为用户空间。我的代码中遗漏了什么才能让它起作用?
发布于 2022-08-08 07:24:27
首先,让我解释一些基本的事情。
用户->将访问您的应用程序的用户。(即超级管理员名,adminname)
角色->的角色谁将在您的应用程序(超级管理员,管理)。在同一角色下将有多个用户。角色将与用户一起映射。
->是对特定角色(“创建用户”、“编辑用户”、“删除用户”)可访问的权限。超级管理员可以拥有所有的3个特权。但是管理员只能有创建和编辑选项,而不是删除选项。权限将映射到角色。
如果您只需要角色,迁移应该是角色和role_users。如果您也需要权限,那么您可以使用权限和permission_role。
// Schema to create roles table
Schema::create('roles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->unique();
$table->string('display_name')->nullable();
$table->string('description')->nullable();
$table->timestamps();
});
// Schema to create role_users table
Schema::create('role_user', function (Blueprint $table) {
$table->unsignedBigInteger('role_id');
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')
->onUpdate('cascade')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('roles')
->onUpdate('cascade')->onDelete('cascade');
$table->primary(['user_id', 'role_id']);
});
// Schema to create permissions table
Schema::create('permissions', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->unique();
$table->string('display_name')->nullable();
$table->string('description')->nullable();
$table->timestamps();
});
// Schema to create permission_role table
Schema::create('permission_role', function (Blueprint $table) {
$table->unsignedBigInteger('permission_id');
$table->unsignedBigInteger('role_id');
$table->foreign('permission_id')->references('id')->on('permissions')
->onUpdate('cascade')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('roles')
->onUpdate('cascade')->onDelete('cascade');
$table->primary(['permission_id', 'role_id']);
});https://stackoverflow.com/questions/73273404
复制相似问题