2022-08-19 19:48:37 +08:00
|
|
|
<?php
|
|
|
|
|
declare (strict_types=1);
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
// | swiftAdmin 极速开发框架 [基于WebMan开发]
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
// | Copyright (c) 2020-2030 http://www.swiftadmin.net
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
// | swiftAdmin.net High Speed Development Framework
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
// | Author: meystack <coolsec@foxmail.com> Apache 2.0 License
|
|
|
|
|
// +----------------------------------------------------------------------
|
2023-07-03 10:08:34 +08:00
|
|
|
namespace app\admin\service;
|
2022-08-19 19:48:37 +08:00
|
|
|
|
2023-07-03 10:08:34 +08:00
|
|
|
use app\admin\enums\AdminEnum;
|
|
|
|
|
use app\common\model\system\Admin;
|
2022-08-19 19:48:37 +08:00
|
|
|
use app\common\model\system\AdminAccess;
|
|
|
|
|
use app\common\model\system\AdminGroup as AdminGroupModel;
|
2023-07-03 10:08:34 +08:00
|
|
|
use app\common\model\system\AdminRules as AdminRulesModel;
|
2022-11-28 19:11:12 +08:00
|
|
|
use think\db\exception\DataNotFoundException;
|
|
|
|
|
use think\db\exception\DbException;
|
|
|
|
|
use think\db\exception\ModelNotFoundException;
|
2022-08-19 19:48:37 +08:00
|
|
|
use Webman\Event\Event;
|
|
|
|
|
|
|
|
|
|
/**
|
2023-07-03 10:08:34 +08:00
|
|
|
* 后台权限验证
|
|
|
|
|
* @package app\admin\service
|
|
|
|
|
* Class AuthService
|
2022-08-19 19:48:37 +08:00
|
|
|
*/
|
2023-07-03 10:08:34 +08:00
|
|
|
class AuthService
|
2022-08-19 19:48:37 +08:00
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* 数据库实例
|
2023-07-03 10:08:34 +08:00
|
|
|
* @var object
|
2022-08-19 19:48:37 +08:00
|
|
|
*/
|
2023-07-03 10:08:34 +08:00
|
|
|
protected object $model;
|
2022-08-19 19:48:37 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 分组标记
|
2022-11-28 19:11:12 +08:00
|
|
|
* @var string
|
2022-08-19 19:48:37 +08:00
|
|
|
*/
|
2022-11-28 19:11:12 +08:00
|
|
|
public string $authGroup = 'authGroup';
|
2022-08-19 19:48:37 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 用户私有标记
|
2022-11-28 19:11:12 +08:00
|
|
|
* @var string
|
2022-08-19 19:48:37 +08:00
|
|
|
*/
|
2022-11-28 19:11:12 +08:00
|
|
|
public string $authPrivate = 'authPrivate';
|
2022-08-19 19:48:37 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 默认权限字段
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2022-11-28 19:11:12 +08:00
|
|
|
public string $authFields = 'id,cid,pid,title,auth';
|
2022-08-19 19:48:37 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 错误信息
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2022-11-28 19:11:12 +08:00
|
|
|
protected string $_error = '';
|
2022-08-19 19:48:37 +08:00
|
|
|
|
|
|
|
|
/**
|
2023-07-03 10:08:34 +08:00
|
|
|
* @var ?object 对象实例
|
2022-08-19 19:48:37 +08:00
|
|
|
*/
|
2023-07-03 10:08:34 +08:00
|
|
|
protected static ?object $instance = null;
|
2022-08-19 19:48:37 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 类构造函数
|
|
|
|
|
* class constructor.
|
|
|
|
|
*/
|
2023-07-03 10:08:34 +08:00
|
|
|
public function __construct()
|
2022-08-19 19:48:37 +08:00
|
|
|
{
|
2023-07-03 10:08:34 +08:00
|
|
|
$this->model = new Admin();
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 初始化
|
|
|
|
|
* @access public
|
|
|
|
|
* @param array $options 参数
|
2023-07-03 10:08:34 +08:00
|
|
|
* @return object|null
|
2022-08-19 19:48:37 +08:00
|
|
|
*/
|
2023-07-03 10:08:34 +08:00
|
|
|
public static function instance(array $options = []): ?object
|
2022-08-19 19:48:37 +08:00
|
|
|
{
|
|
|
|
|
if (is_null(self::$instance)) {
|
|
|
|
|
self::$instance = new static($options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回实例
|
|
|
|
|
return self::$instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查权限
|
2023-07-03 10:08:34 +08:00
|
|
|
* @param mixed $name 需要验证的规则列表,支持逗号分隔的权限规则或索引数组
|
|
|
|
|
* @param int $adminId 认证用户的id
|
2022-08-19 19:48:37 +08:00
|
|
|
* @param int $type 认证类型
|
|
|
|
|
* @param string $mode 执行check的模式
|
2023-07-03 10:08:34 +08:00
|
|
|
* @param string $relation 如果为 'or' 表示满足任一条规则即通过验证;如果为 and则表示需满足所有规则才能通过验证
|
2022-08-19 19:48:37 +08:00
|
|
|
* @return bool 通过验证返回true;失败返回false
|
2022-12-02 11:16:57 +08:00
|
|
|
* @throws DataNotFoundException
|
|
|
|
|
* @throws DbException
|
|
|
|
|
* @throws ModelNotFoundException
|
2022-08-19 19:48:37 +08:00
|
|
|
*/
|
2023-07-03 10:08:34 +08:00
|
|
|
public function permissions(mixed $name, int $adminId = 0, int $type = 1, string $mode = 'url', string $relation = 'or'): bool
|
2022-08-19 19:48:37 +08:00
|
|
|
{
|
|
|
|
|
// 转换格式
|
|
|
|
|
if (is_string($name)) {
|
|
|
|
|
$name = strtolower($name);
|
2022-11-28 19:11:12 +08:00
|
|
|
if (str_contains($name, ',')) {
|
2022-08-19 19:48:37 +08:00
|
|
|
$name = explode(',', $name);
|
|
|
|
|
} else {
|
|
|
|
|
$name = [$name];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$authList = [];
|
|
|
|
|
if ('url' == $mode) { // 解析URL参数
|
|
|
|
|
$REQUEST = unserialize(strtolower(serialize(request()->all())));
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-03 10:08:34 +08:00
|
|
|
foreach ($this->getAuthList($adminId) as $auth) {
|
2022-08-19 19:48:37 +08:00
|
|
|
|
|
|
|
|
// 非鉴权接口
|
|
|
|
|
$router = strtolower($auth['router']);
|
|
|
|
|
if (in_array($router, $name) && $auth['auth'] == 0) {
|
|
|
|
|
$authList[] = $router;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-03 10:08:34 +08:00
|
|
|
// 校验正则模式
|
|
|
|
|
if (!empty($auth['condition'])) {
|
|
|
|
|
$rule = $condition = '';
|
|
|
|
|
$user = $this->getUserInfo();
|
|
|
|
|
$command = preg_replace('/\{(\w*?)\}/', '$user[\'\\1\']', $rule);
|
|
|
|
|
@(eval('$condition=(' . $command . ');'));
|
|
|
|
|
if ($condition) {
|
|
|
|
|
$authList[] = $router;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-19 19:48:37 +08:00
|
|
|
// URL参数模式
|
|
|
|
|
$query = preg_replace('/^.+\?/U', '', $router);
|
|
|
|
|
if ('url' == $mode && $query != $router) {
|
|
|
|
|
parse_str($query, $param);
|
|
|
|
|
$intersect = array_intersect_assoc($REQUEST, $param);
|
|
|
|
|
$router = preg_replace('/\?.*$/U', '', $router);
|
|
|
|
|
if (in_array($router, $name) && $intersect == $param) {
|
|
|
|
|
$authList[] = $router;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (in_array($router, $name)) {
|
|
|
|
|
$authList[] = $router;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$authList = array_unique($authList);
|
|
|
|
|
if ('or' == $relation && !empty($authList)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$authDiff = array_diff($name, $authList);
|
|
|
|
|
if ('and' == $relation && empty($authDiff)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-07-03 10:08:34 +08:00
|
|
|
* 查询权限列表
|
|
|
|
|
* @param mixed $adminId 用户id
|
|
|
|
|
* @param array $nodes 已获取节点
|
2022-08-19 19:48:37 +08:00
|
|
|
* @return array
|
2022-11-28 19:11:12 +08:00
|
|
|
* @throws DataNotFoundException
|
|
|
|
|
* @throws DbException
|
|
|
|
|
* @throws ModelNotFoundException
|
2022-08-19 19:48:37 +08:00
|
|
|
*/
|
2023-07-03 10:08:34 +08:00
|
|
|
public function getAuthList(mixed $adminId = 0, array $nodes = []): array
|
2022-08-19 19:48:37 +08:00
|
|
|
{
|
2023-07-03 10:08:34 +08:00
|
|
|
// 查找节点
|
|
|
|
|
$where[] = ['status', '=', 1];
|
|
|
|
|
if (!$this->superAdmin()) {
|
|
|
|
|
$authNodes = !empty($nodes) ? $nodes : $this->getRulesNode($adminId);
|
|
|
|
|
return AdminRulesModel::where(function ($query) use ($where, $authNodes) {
|
|
|
|
|
if (empty($authNodes[$this->authPrivate])) {
|
|
|
|
|
$where[] = ['auth', '=', '0'];
|
|
|
|
|
$query->where($where);
|
|
|
|
|
} else {
|
|
|
|
|
$where[] = ['id', 'in', $authNodes[$this->authPrivate]];
|
|
|
|
|
$query->where($where)->whereOr('auth', '0');
|
|
|
|
|
}
|
|
|
|
|
})->order('sort asc')->select()->toArray();
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
2023-07-03 10:08:34 +08:00
|
|
|
return AdminRulesModel::where($where)->order('sort asc')->select()->toArray();
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取权限菜单
|
2023-07-03 10:08:34 +08:00
|
|
|
* @return string
|
2022-12-02 11:16:57 +08:00
|
|
|
* @throws DataNotFoundException
|
|
|
|
|
* @throws DbException
|
2022-08-19 19:48:37 +08:00
|
|
|
*/
|
2023-07-03 10:08:34 +08:00
|
|
|
public function getPermissionsMenu(): string
|
2022-08-19 19:48:37 +08:00
|
|
|
{
|
|
|
|
|
$authNodes = $this->getRulesNode();
|
2023-07-03 10:08:34 +08:00
|
|
|
$nodeLists = $this->getAuthList(get_admin_id(), $authNodes);
|
|
|
|
|
foreach ($nodeLists as $key => $value) {
|
|
|
|
|
$nodeLists[$key]['title'] = __($value['title']);
|
|
|
|
|
if ($value['router'] != '#') {
|
|
|
|
|
$nodeLists[$key]['router'] = (string)url($value['router']);
|
|
|
|
|
}
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
2023-07-03 10:08:34 +08:00
|
|
|
$this->superAdmin() && $authNodes['supersAdmin'] = true;
|
|
|
|
|
$authNodes['authorities'] = list_to_tree($nodeLists);
|
2022-08-19 19:48:37 +08:00
|
|
|
return json_encode($authNodes, JSON_UNESCAPED_UNICODE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-07-03 10:08:34 +08:00
|
|
|
* 管理组分级鉴权
|
|
|
|
|
* @param array $operationIds
|
|
|
|
|
* @return bool
|
2022-12-02 11:16:57 +08:00
|
|
|
* @throws DataNotFoundException
|
|
|
|
|
* @throws DbException
|
|
|
|
|
* @throws ModelNotFoundException
|
2022-08-19 19:48:37 +08:00
|
|
|
*/
|
2023-07-03 10:08:34 +08:00
|
|
|
public function checkRulesForGroup(array $operationIds = []): bool
|
2022-08-19 19:48:37 +08:00
|
|
|
{
|
2023-07-03 10:08:34 +08:00
|
|
|
if ($this->superAdmin()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$group_id = $this->getUserInfo()['group_id'];
|
|
|
|
|
$adminGroupIds = explode(',', $group_id);
|
|
|
|
|
$adminGroupList = AdminGroupModel::where('id', 'in', $adminGroupIds)->select()->toArray();
|
|
|
|
|
// 查询操作组
|
|
|
|
|
$operationList = AdminGroupModel::where('id', 'in', $operationIds)->select()->toArray();
|
|
|
|
|
foreach ($operationList as $item) {
|
|
|
|
|
foreach ($adminGroupList as $child) {
|
|
|
|
|
if ($item['pid'] < $child['id']
|
|
|
|
|
|| $item['pid'] == $child['pid']) {
|
|
|
|
|
return false;
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
2023-07-03 10:08:34 +08:00
|
|
|
}
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
2023-07-03 10:08:34 +08:00
|
|
|
return true;
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 查询权限节点
|
|
|
|
|
* @access public
|
2023-07-03 10:08:34 +08:00
|
|
|
* @param $type
|
|
|
|
|
* @param $class
|
2022-08-19 19:48:37 +08:00
|
|
|
* @param bool $tree
|
2023-07-03 10:08:34 +08:00
|
|
|
* @return array|false|string
|
2022-11-28 19:11:12 +08:00
|
|
|
* @throws DataNotFoundException
|
|
|
|
|
* @throws DbException
|
|
|
|
|
* @throws ModelNotFoundException
|
2022-08-19 19:48:37 +08:00
|
|
|
*/
|
2023-07-03 10:08:34 +08:00
|
|
|
public function getRuleCatesTree($type, $class, bool $tree = true)
|
2022-08-19 19:48:37 +08:00
|
|
|
{
|
|
|
|
|
if (is_array($type) && $type) {
|
2023-07-03 10:08:34 +08:00
|
|
|
$type = $type['type'] ?? AdminEnum::ADMIN_AUTH_RULES;
|
2022-08-19 19:48:37 +08:00
|
|
|
$class = $type['class'] ?? $this->authGroup;
|
|
|
|
|
}
|
|
|
|
|
$class = $class != $this->authGroup ? $this->authPrivate : $class;
|
2023-07-03 10:08:34 +08:00
|
|
|
$authNodes = $this->getRulesNode(get_admin_id(), $type);
|
|
|
|
|
$where[] = ['status', '=', 1];
|
|
|
|
|
if ($type && $type == AdminEnum::ADMIN_AUTH_RULES) {
|
2022-08-19 19:48:37 +08:00
|
|
|
if (!$this->superAdmin()) {
|
2023-07-03 10:08:34 +08:00
|
|
|
$menuList = AdminRulesModel::where(function ($query) use ($where, $authNodes, $class) {
|
|
|
|
|
if (empty($authNodes[$class])) {
|
2022-08-19 19:48:37 +08:00
|
|
|
$where[] = ['auth', '=', '0'];
|
|
|
|
|
$query->where($where);
|
|
|
|
|
} else {
|
2023-07-03 10:08:34 +08:00
|
|
|
$where[] = ['id', 'in', $authNodes[$class]];
|
2022-08-19 19:48:37 +08:00
|
|
|
$query->where($where)->whereOr('auth', '0');
|
|
|
|
|
}
|
|
|
|
|
})->order('sort asc')->select()->toArray();
|
|
|
|
|
} else {
|
2023-07-03 10:08:34 +08:00
|
|
|
$menuList = AdminRulesModel::where($where)->order('sort asc')->select()->toArray();
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
2023-07-03 10:08:34 +08:00
|
|
|
|
2022-08-19 19:48:37 +08:00
|
|
|
} else {
|
2022-11-28 19:11:12 +08:00
|
|
|
/**
|
|
|
|
|
* 栏目二次开发接口
|
2023-07-03 10:08:34 +08:00
|
|
|
* @param $menuList
|
2022-11-28 19:11:12 +08:00
|
|
|
*/
|
2023-07-03 10:08:34 +08:00
|
|
|
if (!$this->superAdmin() && !empty($authNodes[$class])) {
|
|
|
|
|
$menuList = Event::emit('cmsCategoryPermissions', [
|
|
|
|
|
'field' => $this->authFields,
|
|
|
|
|
'nodes' => $authNodes[$class]
|
|
|
|
|
], true);
|
2022-08-28 22:24:52 +08:00
|
|
|
} else {
|
2023-07-03 10:08:34 +08:00
|
|
|
$menuList = Event::emit('cmsCategoryPermissions', [
|
2022-08-28 22:24:52 +08:00
|
|
|
'field' => $this->authFields
|
|
|
|
|
], true);
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-03 10:08:34 +08:00
|
|
|
return $tree ? ($menuList ? json_encode(list_to_tree($menuList)) : json_encode([])) : $menuList;
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
2023-07-03 10:08:34 +08:00
|
|
|
|
2022-08-19 19:48:37 +08:00
|
|
|
/**
|
2023-07-03 10:08:34 +08:00
|
|
|
* 校验节点避免越权
|
2022-08-19 19:48:37 +08:00
|
|
|
* @access public
|
2023-07-03 10:08:34 +08:00
|
|
|
* @param $rules
|
|
|
|
|
* @param string $type
|
2022-11-28 19:11:12 +08:00
|
|
|
* @param string $class
|
2022-08-19 19:48:37 +08:00
|
|
|
* @return bool
|
2022-11-28 19:11:12 +08:00
|
|
|
* @throws DataNotFoundException
|
|
|
|
|
* @throws DbException
|
|
|
|
|
* @throws ModelNotFoundException
|
2022-08-19 19:48:37 +08:00
|
|
|
*/
|
2023-07-03 10:08:34 +08:00
|
|
|
public function checkRuleOrCateNodes($rules, string $type, string $class = 'pri'): bool
|
2022-08-19 19:48:37 +08:00
|
|
|
{
|
2023-07-03 10:08:34 +08:00
|
|
|
if ($this->superAdmin()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$type = !empty($type) ? $type : AdminEnum::ADMIN_AUTH_RULES;
|
|
|
|
|
$class = !empty($class) ? $class : $this->authGroup;
|
|
|
|
|
$class = $class != $this->authGroup ? $this->authPrivate : $class;
|
|
|
|
|
$authNodes = $this->getRulesNode(get_admin_id(), $type);
|
|
|
|
|
$differ = array_unique(array_merge($rules, $authNodes[$class]));
|
|
|
|
|
if (count($differ) > count($authNodes[$class])) {
|
|
|
|
|
return false;
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-07-03 10:08:34 +08:00
|
|
|
* 获取权限节点
|
|
|
|
|
* @param mixed $adminId 管理员id
|
|
|
|
|
* @param string $type 节点类型
|
|
|
|
|
* @return array
|
2022-12-02 11:16:57 +08:00
|
|
|
* @throws DataNotFoundException
|
|
|
|
|
* @throws DbException
|
|
|
|
|
* @throws ModelNotFoundException
|
2022-08-19 19:48:37 +08:00
|
|
|
*/
|
2023-07-03 10:08:34 +08:00
|
|
|
public function getRulesNode(mixed $adminId = 0, string $type = AdminEnum::ADMIN_AUTH_RULES): array
|
2022-08-19 19:48:37 +08:00
|
|
|
{
|
2023-07-03 10:08:34 +08:00
|
|
|
$authGroup = $authPrivate = [];
|
|
|
|
|
$adminId = $adminId > 0 ? $adminId : get_admin_id();
|
|
|
|
|
$authNodes = AdminAccess::where('admin_id', $adminId)->findOrEmpty()->toArray();
|
|
|
|
|
|
|
|
|
|
// 私有节点
|
|
|
|
|
if (!empty($authNodes[$type])) {
|
|
|
|
|
$authPrivate = explode(',', $authNodes[$type]);
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
2023-07-03 10:08:34 +08:00
|
|
|
|
|
|
|
|
// 用户组节点
|
|
|
|
|
if (!empty($authNodes['group_id'])) {
|
|
|
|
|
$groupNodes = (new AdminGroupModel)->whereIn('id', $authNodes['group_id'])->select()->toArray();
|
|
|
|
|
foreach ($groupNodes as $value) {
|
|
|
|
|
$nodes = !empty($value[$type]) ? explode(',', $value[$type]) : [];
|
|
|
|
|
$authGroup = array_merge($authGroup, $nodes);
|
|
|
|
|
$authPrivate = array_merge($authPrivate, $nodes);
|
|
|
|
|
}
|
|
|
|
|
$authGroup = array_unique($authGroup);
|
|
|
|
|
$authPrivate = array_unique($authPrivate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
$this->authGroup => $authGroup,
|
|
|
|
|
$this->authPrivate => $authPrivate,
|
|
|
|
|
];
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-07-03 10:08:34 +08:00
|
|
|
* 超级管理员
|
|
|
|
|
* @param int $adminId
|
|
|
|
|
* @param int $type
|
2022-08-19 19:48:37 +08:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2023-07-03 10:08:34 +08:00
|
|
|
public function superAdmin(int $adminId = 0, int $type = 1): bool
|
2022-08-19 19:48:37 +08:00
|
|
|
{
|
2023-07-03 10:08:34 +08:00
|
|
|
$adminId = $adminId > 1 ? $adminId : get_admin_id();
|
|
|
|
|
$adminInfo = $this->getUserInfo($adminId);
|
|
|
|
|
$adminGroup = explode(',', $adminInfo['group_id']);
|
|
|
|
|
if ($adminInfo['id'] == $type || array_search($type, $adminGroup)) {
|
2022-08-19 19:48:37 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-03 10:08:34 +08:00
|
|
|
return false;
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取用户信息
|
2023-07-03 10:08:34 +08:00
|
|
|
* @param int $adminId
|
2022-11-28 19:11:12 +08:00
|
|
|
* @return array
|
2022-08-19 19:48:37 +08:00
|
|
|
*/
|
2023-07-03 10:08:34 +08:00
|
|
|
public function getUserInfo(int $adminId = 0): array
|
2022-08-19 19:48:37 +08:00
|
|
|
{
|
2023-07-03 10:08:34 +08:00
|
|
|
$_pk = is_string($this->model->getPk()) ? $this->model->getPk() : 'id';
|
|
|
|
|
return $this->model->where($_pk, $adminId)->findOrEmpty()->toArray();
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取最后产生的错误
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getError(): string
|
|
|
|
|
{
|
|
|
|
|
return $this->_error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置错误
|
2023-07-03 10:08:34 +08:00
|
|
|
* @param string $error
|
2022-08-19 19:48:37 +08:00
|
|
|
*/
|
2022-11-28 19:11:12 +08:00
|
|
|
protected function setError(string $error): void
|
2022-08-19 19:48:37 +08:00
|
|
|
{
|
|
|
|
|
$this->_error = $error;
|
|
|
|
|
}
|
2023-07-03 10:08:34 +08:00
|
|
|
}
|