You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
296 lines
10 KiB
296 lines
10 KiB
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
|
// +----------------------------------------------------------------------
|
|
// | Copyright (c) 2011 http://thinkphp.cn All rights reserved.
|
|
// +----------------------------------------------------------------------
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
|
// +----------------------------------------------------------------------
|
|
// | Author: luofei614 <weibo.com/luofei614>
|
|
// +----------------------------------------------------------------------
|
|
// | 修改者: anuo (本权限类在原3.2.3的基础上修改过来的)
|
|
// +----------------------------------------------------------------------
|
|
|
|
/**
|
|
* 权限认证类
|
|
* 功能特性:
|
|
* 1,是对规则进行认证,不是对节点进行认证。用户可以把节点当作规则名称实现对节点进行认证。
|
|
* $auth=new Auth(); $auth->check('规则名称','用户id')
|
|
* 2,可以同时对多条规则进行认证,并设置多条规则的关系(or或者and)
|
|
* $auth=new Auth(); $auth->check('规则1,规则2','用户id','and')
|
|
* 第三个参数为and时表示,用户需要同时具有规则1和规则2的权限。 当第三个参数为or时,表示用户值需要具备其中一个条件即可。默认为or
|
|
* 3,一个用户可以属于多个用户组(think_auth_group_access表 定义了用户所属用户组)。我们需要设置每个用户组拥有哪些规则(think_auth_group 定义了用户组权限)
|
|
* 4,支持规则表达式。
|
|
* 在think_auth_rule 表中定义一条规则时,如果type为1, condition字段就可以定义规则表达式。 如定义{score}>5 and {score}<100
|
|
* 表示用户的分数在5-100之间时这条规则才会通过。
|
|
*/
|
|
//数据库
|
|
/*
|
|
*/
|
|
|
|
namespace hook;
|
|
|
|
use think\Controller;
|
|
use think\Db;
|
|
use think\facade\Session;
|
|
use think\Loader;
|
|
use think\Request;
|
|
|
|
|
|
class Auth extends Controller
|
|
{
|
|
/**
|
|
* @var object 对象实例
|
|
*/
|
|
protected static $instance;
|
|
/**
|
|
* 当前请求实例
|
|
* @var Request
|
|
*/
|
|
protected $request;
|
|
|
|
//默认配置
|
|
protected $config_auth = [
|
|
'auth_on' => 1, // 权限开关
|
|
'auth_type' => 1, // 认证方式,1为实时认证;2为登录认证。
|
|
'auth_group' => 'auth_group', // 用户组数据表名
|
|
'auth_group_access' => 'auth_group_access', // 用户-用户组关系表
|
|
'auth_rule' => 'auth_rule', // 权限规则表
|
|
'auth_user' => 'member', // 用户信息表
|
|
];
|
|
|
|
/**
|
|
* 类架构函数
|
|
* Auth constructor.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
//可设置配置项 auth, 此配置项为数组。
|
|
if ($auth = config('auth')) {
|
|
$this->config_auth = array_merge($this->config_auth, $auth);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 初始化
|
|
* @access public
|
|
* @param array $options 参数
|
|
* @return \think\Request
|
|
*/
|
|
public static function instance($options = [])
|
|
{
|
|
if (is_null(self::$instance)) {
|
|
self::$instance = new static($options);
|
|
}
|
|
|
|
return self::$instance;
|
|
}
|
|
|
|
/**
|
|
* 检查权限
|
|
* @param $name string|array 需要验证的规则列表,支持逗号分隔的权限规则或索引数组
|
|
* @param $uid int 认证用户的id
|
|
* @param int $type 认证类型
|
|
* @param string $mode 执行check的模式
|
|
* @param string $relation 如果为 'or' 表示满足任一条规则即通过验证;如果为 'and'则表示需满足所有规则才能通过验证
|
|
* @return bool 通过验证返回true;失败返回false
|
|
*/
|
|
public function check($name, $uid, $type = 1, $mode = 'url', $relation = 'or')
|
|
{
|
|
if (!$this->config_auth['auth_on']) {
|
|
return true;
|
|
}
|
|
// 获取用户需要验证的所有有效规则列表
|
|
$authList = $this->getAuthList($uid, $type);
|
|
if (is_string($name)) {
|
|
$name = strtolower($name);
|
|
if (strpos($name, ',') !== false) {
|
|
$name = explode(',', $name);
|
|
} else {
|
|
$name = [$name];
|
|
}
|
|
}
|
|
|
|
$list = []; //保存验证通过的规则名
|
|
if ('url' == $mode) {
|
|
$REQUEST = unserialize(strtolower(serialize($this->request->param())));
|
|
}
|
|
|
|
foreach ($authList as $auth) {
|
|
$query = preg_replace('/^[.].+\?/U', '', $auth);
|
|
if ('url' == $mode && $query != $auth) {
|
|
parse_str($query, $param); //解析规则中的param
|
|
$intersect = array_intersect_assoc($REQUEST, $param);
|
|
|
|
$auth = preg_replace('/\?.*$/U', '', $auth);
|
|
if (in_array($auth, $name) && $intersect == $param) {
|
|
//如果节点相符且url参数满足
|
|
$list[] = $auth;
|
|
}
|
|
} else {
|
|
|
|
if (in_array($auth, $name) || $auth == $name) {
|
|
$list[] = $auth;
|
|
}
|
|
}
|
|
}
|
|
|
|
if ('or' == $relation && !empty($list)) {
|
|
return true;
|
|
}
|
|
$diff = array_diff($name, $list);
|
|
if ('and' == $relation && empty($diff)) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* 根据用户id获取用户组,返回值为数组
|
|
* @param $uid int 用户id
|
|
* @return array 用户所属的用户组 array(
|
|
* array('uid'=>'用户id','group_id'=>'用户组id','title'=>'用户组名称','rules'=>'用户组拥有的规则id,多个,号隔开'),
|
|
* ...)
|
|
*/
|
|
public function getGroups($uid)
|
|
{
|
|
static $groups = [];
|
|
if (isset($groups[$uid])) {
|
|
return $groups[$uid];
|
|
}
|
|
// 转换表名
|
|
$auth_group_access = Loader::parseName($this->config_auth['auth_group_access'], 1);
|
|
$auth_group = Loader::parseName($this->config_auth['auth_group'], 1);
|
|
// 执行查询
|
|
$user_groups = Db::view($auth_group_access, 'uid,group_id')
|
|
->view($auth_group, 'title,rules', "{$auth_group_access}.group_id={$auth_group}.id", 'LEFT')
|
|
->where("{$auth_group_access}.uid='{$uid}' and {$auth_group}.status='1'")
|
|
->select();
|
|
$groups[$uid] = $user_groups ?: [];
|
|
|
|
return $groups[$uid];
|
|
}
|
|
|
|
/**
|
|
* 获得权限列表
|
|
* @param integer $uid 用户id
|
|
* @param integer $type
|
|
* @return array
|
|
*/
|
|
protected function getAuthList($uid, $type, $is_session = 1)
|
|
{
|
|
static $_authList = []; //保存用户验证通过的权限列表
|
|
$t = implode(',', (array)$type);
|
|
|
|
if (isset($_authList[$uid . $t])) {
|
|
return $_authList[$uid . $t];
|
|
}
|
|
if ($is_session == 1 && 2 == $this->config_auth['auth_type'] && Session::has('_auth_list_' . $uid . $t)) {
|
|
return Session::get('_auth_list_' . $uid . $t);
|
|
}
|
|
|
|
$ismanager = 0;
|
|
//读取用户所属用户组
|
|
$groups = $this->getGroups($uid);
|
|
|
|
$ids = []; //保存用户所属用户组设置的所有权限规则id
|
|
foreach ($groups as $g) {
|
|
if ($g['group_id'] == 1) {
|
|
$ismanager = 1;
|
|
}
|
|
$ids = array_merge($ids, explode(',', trim($g['rules'], ',')));
|
|
}
|
|
$ids = array_unique($ids);
|
|
if (empty($ids)) {
|
|
$_authList[$uid . $t] = [];
|
|
return [];
|
|
}
|
|
|
|
$map = [
|
|
['id', 'in', implode(',', $ids)],
|
|
['type', '=', $type]
|
|
];
|
|
|
|
if ($ismanager) {
|
|
$map = [['type', '=', $type]];
|
|
}
|
|
//读取用户组所有权限规则
|
|
$rules_i = Db::name($this->config_auth['auth_rule'])->where($map)->order("sort desc")->field('id,parentid,title,icon,condition,name,button,is_dev')->select();
|
|
$rules = array();
|
|
foreach ($rules_i as $val) {
|
|
$rules[$val['id']] = $val;
|
|
}
|
|
if ($is_session == 1) {
|
|
Session::set('_authList', $rules);
|
|
}
|
|
|
|
//循环规则,判断结果。
|
|
$authList = []; //
|
|
foreach ($rules as $rule) {
|
|
//$rule['name'] = str_replace('Admin/','cloud/',$rule['name']);
|
|
if (!empty($rule['condition'])) {
|
|
//根据condition进行验证
|
|
$user = $this->getUserInfo($uid); //获取用户信息,一维数组
|
|
$command = preg_replace('/\{(\w*?)\}/', '$user[\'\\1\']', $rule['condition']);
|
|
@(eval('$condition=(' . $command . ');'));
|
|
if ($condition) {
|
|
$authList[] = strtolower($rule['name']);
|
|
}
|
|
} else {
|
|
//只要存在就记录
|
|
$authList[] = strtolower($rule['name']);
|
|
}
|
|
}
|
|
$_authList[$uid . $t] = $authList;
|
|
|
|
if ($is_session == 1 && 2 == $this->config_auth['auth_type']) {
|
|
//规则列表结果保存到session
|
|
Session::set('_auth_list_' . $uid . $t, $authList);
|
|
}
|
|
|
|
return array_unique($authList);
|
|
}
|
|
|
|
/**
|
|
* 获得用户资料
|
|
* @param $uid
|
|
* @return mixed
|
|
*/
|
|
protected function getUserInfo($uid)
|
|
{
|
|
static $user_info = [];
|
|
|
|
$user = Db::name($this->config_auth['auth_user']);
|
|
// 获取用户表主键
|
|
$_pk = is_string($user->getPk()) ? $user->getPk() : 'uid';
|
|
if (!isset($user_info[$uid])) {
|
|
$user_info[$uid] = $user->where($_pk, $uid)->find();
|
|
}
|
|
|
|
return $user_info[$uid];
|
|
}
|
|
|
|
/**
|
|
* 处理当前路径
|
|
* @param $auth 当前用户权限菜单集合
|
|
* @param $spm 页面spm参数
|
|
* @return string|void
|
|
*/
|
|
protected function formatCatpos($auth, $spm)
|
|
{
|
|
if (!$auth || !$spm) {
|
|
return;
|
|
}
|
|
$arr = explode('-', $spm);
|
|
$_catpos = '';
|
|
foreach ($arr as $val) {
|
|
if (isset($auth[$val])) {
|
|
$_catpos .= '<span class="kt-subheader__breadcrumbs-separator"></span><a href="javascript:;" class="kt-subheader__breadcrumbs-link">' . $auth[$val]['title'] . '</a>';
|
|
}
|
|
}
|
|
return $_catpos;
|
|
}
|
|
|
|
}
|