<?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 創(chuàng)建時間
* @property string $updated_at 更新時間
* @property string $login_at 登錄時間
* @property string $roles 角色
* @property integer $status 狀態(tài) 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;
}
}
/**
* 數據限制
* 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'];
}
}
例如:AdminController文件中
/**
* 構造函數
* @return void
*/
public function __construct()
{
$this->model = new Admin;
parent::__construct();
}
/**
* 構造函數
* @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);