Files
2024-09-27 01:32:49 +08:00

70 lines
2.3 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* 访问权限控制
* Class AuthService
* @package app\admin\service
*/
namespace app\admin\service;
class AuthService {
/**
* 判断是否有权限访问该节点
* @param $node 节点
* @return bool true有权限false无权限
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public static function checkNode($node = '') {
//如果没有传参,默认获取当前位置 模块/控制器/方法 (小写)
if (empty($node)) $node = self::getNode();
//判断当前登录是否为超级管理员
if (session('user.id') == 1) return true;
//判断是否加入RABC控制
$is_auth = model('SysNode')->where(['node' => $node, 'is_auth' => 1])->find();
if (empty($is_auth)) return true;
//获取当前用户角色组信息 ,拆表
$user = new \app\admin\model\User;
$auth_id_list = json_decode($user->where(['id' => session('user.id'), 'status' => 1, 'is_deleted' => 0])->value('auth_id'), true);
//去除失效的角色组信息
foreach ($auth_id_list as $k => $val) {
if (empty(model('SysAuth')->where(['id' => $val, 'status' => 1])->find())) unset($auth_id_list[$k]);
}
//判断是否有权限访问
foreach ($auth_id_list as $vo) {
if ($vo == 0) return true; //超级管理员组权限
$is_auth_node = model('SysAuthNode')->where(['auth' => $vo, 'node' => $is_auth['id']])->find();
if (!empty($is_auth_node)) return true;
}
return false;
}
/**
* 获取当前节点
* @return string 节点信息
*/
public static function getNode() {
$node = app('request')->module() . '/' . app('request')->controller() . '/' . app('request')->action();
return self::parseNodeStr($node);
}
/**
* 驼峰转下划线规则
* @param string $node
* @return string
*/
public static function parseNodeStr($node) {
$tmp = [];
foreach (explode('/', $node) as $name) {
$tmp[] = strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
}
return str_replace('._', '.', trim(join('/', $tmp), '/'));
}
}