webman_admin 数据权限设置

不败少龙

plugin/admin/app/model/Admin.php

<?php

namespace plugin\admin\app\model;

use plugin\admin\app\model\Base;

/**
 * @property integer $id ID(主键)
 * @property string $username 用户名
 * @property string $nickname 昵称
 * @property string $password 密码
 * @property string $avatar 头像
 * @property string $email 邮箱
 * @property string $mobile 手机
 * @property string $created_at 创建时间
 * @property string $updated_at 更新时间
 * @property string $login_at 登录时间
 * @property string $roles 角色
 * @property integer $status 状态 0正常 1禁用
 */
class Admin extends Base
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_admins';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    protected $appends=[
        'role_key'
    ];
     public function getRoleKeyAttribute($value): string
     {
         $role_key = "null";
         $user_id = $this->id;
         if ($user_id){
             $role_id = AdminRole::where('admin_id',$user_id)->value('role_id');
             if ($role_id){
                 $role_key = Role::where('id',$role_id)->value('role_key')??"null";
             }
         }
         return $role_key;
     }

}

plugin/admin/app/controller/Base.php

 /**
     * 数据限制
     * null 不做限制,任何管理员都可以查看该表的所有数据
     * auth 管理员能看到自己以及自己的子管理员插入的数据
     * personal 管理员只能看到自己插入的数据
     * @var string
     */
    protected $dataLimit = null;

    /**
     * 数据限制字段
     */
    protected $dataLimitField = 'admin_id';

    public function __construct()
    {
        $session = request()->session();
        $admin = $session->get('admin');
        if ($admin) {
            $this->dataLimit = $admin['role_key'];
        }
    }

在子类的构造函数中加上 parent::__construct();

例如:AdminController文件中

 /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new Admin;
        parent::__construct();
    }

plugin/admin/app/controller/TableController.php 第685行

/**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        \$this->model = new $model_class;
        parent::__construct();
    }

字典表插入

INSERT INTO `saas_worker`.`wa_options`(`id`, `name`, `value`, `created_at`, `updated_at`, `admin_id`) VALUES (41, 'dict_data_permissions', '[{\"value\":\"null\",\"name\":\"全部数据\"},{\"value\":\"auth\",\"name\":\"自己以及自己的子管理员的数据\"},{\"value\":\"personal\",\"name\":\"自己的数据\"}]', '2024-06-18 14:44:26', '2024-06-18 15:12:36', NULL);

在需要的表中添加admin_id字段

demo文件地址

演示图:

截图
截图

413 0 0
0个评论

不败少龙

9734
积分
0
获赞数
0
粉丝数
2018-08-17 加入
×
🔝