Laravel-admin管理前台(laravel-permission)权限

版本

  • laravel 5.7.28
  • Laravel-admin 1.6.12

背景:
有个小需求,前后台用户表分开,并且都需要角色权限控制,基于我们的前台也使用 laravel-permission 管理我们的角色权限,很简单 下面我们来看一下具体代码

先放一张效果图
file

添加路由

app/Admin/routes.php

.
.
.
    // 用户与权限
    // 用户与权限 - 用户
    $router->resource('users', 'UsersController', ['only' => ['index', 'edit', 'update']])->names('admin.users');
    // 用户与权限 - 角色
    $router->resource('roles', 'RolesController')->names('admin.roles');
    // 用户与权限 - 权限
    $router->resource('permissions', 'PermissionsController')->names('admin.permissions');
.
.
.

用户控制器

app/Controllers/UsersController.php

.
.
.
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
     protected function grid(){
         $grid = new Grid(new User);
         .
         .
         $grid->roles('角色')->pluck('name')->label();
         .
         .
     }
     .
     .
     .
     /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $permissionModel = "Spatie\Permission\Models\Permission";
        $roleModel = "Spatie\Permission\Models\Role";
        $form = new Form(new User);
        .
        .
        .
        $form->multipleSelect('roles', '角色')->options($roleModel::all()->pluck('name', 'id'));
        $form->multipleSelect('permissions', '权限')->options($permissionModel::all()->pluck('name', 'id'));

        return $form;
    }
.
.
.

角色控制器

app/Controllers/RolesController.php

<?php
/**
 * 前台角色权限管理
 * User: GucciLee
 * Date: 2019/5/5
 * Time: 10:49
 */

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;

class RolesController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('用户列表')
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Role);

        $grid->id('Id');
        $grid->name('角色名称');
        $grid->guard_name('guard_name');
        $grid->permissions('权限')->pluck('name')->label();

        $grid->actions(function ($actions) {
            // 不在每一行后面展示查看按钮
            $actions->disableView();
            // 不在每一行后面展示删除按钮
            // $actions->disableDelete();
            // 不在每一行后面展示编辑按钮
            // $actions->disableEdit();
        });

        $grid->tools(function ($tools) {
            // 禁用批量删除按钮
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });

        return $grid;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $permissionModel = "Spatie\Permission\Models\Permission";
        $roleModel = "Spatie\Permission\Models\Role";

        $form = new Form(new $roleModel());

        $form->display('id', 'ID');

        $form->text('name', '角色名称')->rules('required');

        $form->listbox('permissions', '权限')->options($permissionModel::all()->pluck('name', 'id'));

        return $form;
    }
}

权限控制器

app/Controllers/PermissionsController.php

<?php
/**
 * 前台权限管理
 * User: GucciLee
 * Date: 2019/5/5
 * Time: 10:49
 */

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;

class PermissionsController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('用户列表')
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Permission);

        $grid->id('Id');
        $grid->name('权限名称');
        $grid->guard_name('guard_name');

        $grid->actions(function ($actions) {
            // 不在每一行后面展示查看按钮
            $actions->disableView();
            // 不在每一行后面展示删除按钮
            // $actions->disableDelete();
            // 不在每一行后面展示编辑按钮
            // $actions->disableEdit();
        });

        $grid->tools(function ($tools) {
            // 禁用批量删除按钮
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });

        return $grid;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Permission);

        $form->text('name', '权限名称');

        return $form;
    }
}
讨论数量: 0

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!