一个用户对应一个角色,一个角色对应多个权限,一个用户对应用户组,一个用户组对应多个权限
角色
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name',50)->default('')->comment('角色名称');
$table->softDeletes();
$table->timestamps();
});
}
节点
public function up()
{
Schema::create('nodes', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name',30)->default('')->comment('节点名称');
$table->enum('is_menu',['0','1'])->default('0')->comment('是否菜单节点1是,0否');
$table->string('router_name')->default('')->comment('路由别名,权限认证标识');
$table->unsignedInteger('pid')->default(0)->comment('上级id');
$table->timestamps();
//软删除
$table->softDeletes();
});
}
文章
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->id('id');
$table->string('title',50)->default('')->comment('标题');
$table->string('desn',255)->default('')->comment('文章摘要');
$table->string('pic',100)->default('')->comment('文章封面');
$table->text('body')->comment('文章内容');
$table->softDeletes();
$table->timestamps();
});
}
分配权限
用户和角色之间是关系: 属于关系 beLongsTo
User.php模型model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
//继承可以使用 auth登录的模型类
use Illuminate\Foundation\Auth\User as AuthUser;
//软删除类
use Illuminate\Database\Eloquent\SoftDeletes;
//按钮组
use App\Models\Traits\Btn;
class User extends AuthUser
{
//调用定义trait类 和 继承效果一样
use SoftDeletes;
//软删除表示字段
protected $dates = ['delete_at'];
//拒绝不添加的字段
protected $guarded = [];
//引入按钮
use Btn;
//给管理员分配权限
public function role(){
return $this->belongsTo(Role::class,'role_id','id');
}
}
RoleController控制器定义方法
// An highlighted block
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Node;
use App\Models\Role;
use Illuminate\Http\Request;
class RoleController extends BaseController
{
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
return view('admin.role.create');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show(Request $request)
{
//获取搜索框
$name = $request->get('name');
$data = Role::when($name,function ($query) use ($name) {
$query->where('name','like',"%{$name}%");
})->paginate($this->pagesize);
// $data = Role::paginate($this->pagesize);
return view('admin.role.show',compact('data'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$data = $this->validate($request,[
'name'=>'required'
]);
//获取表单数据
$post = $request->except(['_token']);
Role::create($post);
//跳转到角色列表页
return redirect(route('admin.role.show'))->with('success','新增角色成员成功');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function delete(int $id)
{
Role::find($id)->delete();
//强制删除 在配置了软删除的时候 真实的删除操作
//User::find($id)->forceDelete();
return ['status'=>0,'msg'=>'删除成功'];
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$role = Role::find($id);
return view('admin.role.edit',compact('role'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request,[
'name'=>'required',
]);
$post = Role::find($id);
// $pwd = $request->get('password');
if($post){
$posts = $request->only([
'name',
]);
// dd($request->all());
// $posts['password']=bcrypt($post);
$post->update($posts);
return redirect(route('admin.role.show'))->with('success','修改用户记录成功');
}
return redirect(route('admin.role.show'))->withErrors(['error'=>'修改用户记录失败']);
//
}
// 给角色分配权限
public function node(Role $role){
// 当前角色有的权限
$data = $role->nodes()->pluck('id')->toArray();
// 所有权限
$nodeAll=(new Node())->getAllList();
return view('admin.role.node',compact('role','nodeAll','data'));
}
// 保存角色权限
public function nodeSever(Request $request,Role $role){
$role->nodes()->sync($request->get('node'));
return redirect(route('admin.role.show',$role));
}
}
IndexController分配角色和处理
//分配角色和处理
public function role(Request $request,User $user){
//判断是否是post提示
if ($request->isMethod('post')){
$post = $this->validate($request,[
'role_id'=>'required'],['role_id.required'=>'必须选择']);
$user->update($post);
//dd($user);
return redirect(route('user.lists'));
}
//读取所有的角色
$roleAll = Role::all();
return view('user.role',compact('user','roleAll'));
}
管理员列表模板添加分配权限按钮
//lists.php
<table class="table table-border table-bordered table-hover table-bg table-sort">
<thead>
<tr class="text-c">
<th width="25"><input type="checkbox" name="" value=""></th>
<th width="40">ID</th>
<th width="70">用户名</th>
<th width="50">角色</th>
<th width="100">邮箱</th>
<th width="90">手机</th>
<th width="40">性别</th>
<th width="70">加入时间</th>
<th width="40">状态</th>
<th width="100">操作</th>
</tr>
</thead>
<tbody>
@foreach($data as $v)
<tr class="text-c">
{
{
--批量删除--}}
<td>
@if(auth()->id() != $v->id)
@if($v->deleted_at == null)
<input type="checkbox" value="{
{$v->id}}" name="id[]">
@endif
@endif
</td>
<td>{
{
$v->id}}</td>
<td>{
{
$v->username}}</td>
<td>
@if($v->role)
{
{
$v->role->name }}
@endif
</td>
<td>{
{
$v->email}}</td>
<td>{
{
$v->phone}}</td>
<td>{
{
$v->sex}}</td>
<td>{
{
$v->created_at}}</td>
<td class="user-status"><span class="label label-success">已启用