123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- <?php
- namespace fast;
- use think\Db;
- use think\Config;
- use think\Session;
- use think\Request;
- class Auth
- {
-
- protected static $instance;
- protected $rules = [];
-
- protected $request;
-
- protected $config = [
- 'auth_on' => 1,
- 'auth_type' => 1,
- 'auth_group' => 'auth_group',
- 'auth_group_access' => 'auth_group_access',
- 'auth_rule' => 'auth_rule',
- 'auth_user' => 'user',
- ];
- public function __construct()
- {
- if ($auth = Config::get('auth')) {
- $this->config = array_merge($this->config, $auth);
- }
-
- $this->request = Request::instance();
- }
-
- public static function instance($options = [])
- {
- if (is_null(self::$instance)) {
- self::$instance = new static($options);
- }
- return self::$instance;
- }
-
- public function check($name, $uid, $relation = 'or', $mode = 'url')
- {
- if (!$this->config['auth_on']) {
- return true;
- }
-
- $rulelist = $this->getRuleList($uid);
- if (in_array('*', $rulelist)) {
- return true;
- }
- 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 ($rulelist as $rule) {
- $query = preg_replace('/^.+\?/U', '', $rule);
- if ('url' == $mode && $query != $rule) {
- parse_str($query, $param);
- $intersect = array_intersect_assoc($REQUEST, $param);
- $rule = preg_replace('/\?.*$/U', '', $rule);
- if (in_array($rule, $name) && $intersect == $param) {
-
- $list[] = $rule;
- }
- } else {
- if (in_array($rule, $name)) {
- $list[] = $rule;
- }
- }
- }
- if ('or' == $relation && !empty($list)) {
- return true;
- }
- $diff = array_diff($name, $list);
- if ('and' == $relation && empty($diff)) {
- return true;
- }
- return false;
- }
-
- public function getGroups($uid)
- {
- static $groups = [];
- if (isset($groups[$uid])) {
- return $groups[$uid];
- }
-
- $user_groups = Db::name($this->config['auth_group_access'])
- ->alias('aga')
- ->join('__' . strtoupper($this->config['auth_group']) . '__ ag', 'aga.group_id = ag.id', 'LEFT')
- ->field('aga.uid,aga.group_id,ag.id,ag.pid,ag.name,ag.rules')
- ->where("aga.uid='{$uid}' and ag.status='normal'")
- ->select();
- $groups[$uid] = $user_groups ?: [];
- return $groups[$uid];
- }
-
- public function getRuleList($uid)
- {
- static $_rulelist = [];
- if (isset($_rulelist[$uid])) {
- return $_rulelist[$uid];
- }
- if (2 == $this->config['auth_type'] && Session::has('_rule_list_' . $uid)) {
- return Session::get('_rule_list_' . $uid);
- }
-
- $ids = $this->getRuleIds($uid);
- if (empty($ids)) {
- $_rulelist[$uid] = [];
- return [];
- }
-
- $where = [
- 'status' => 'normal'
- ];
- if (!in_array('*', $ids)) {
- $where['id'] = ['in', $ids];
- }
-
- $this->rules = Db::name($this->config['auth_rule'])->where($where)->field('id,pid,condition,icon,name,title,ismenu')->select();
-
- $rulelist = [];
- if (in_array('*', $ids)) {
- $rulelist[] = "*";
- }
- foreach ($this->rules as $rule) {
-
- if (!empty($rule['condition']) && !in_array('*', $ids)) {
-
- $user = $this->getUserInfo($uid);
- $nums = 0;
- $condition = str_replace(['&&', '||'], "\r\n", $rule['condition']);
- $condition = preg_replace('/\{(\w*?)\}/', '\\1', $condition);
- $conditionArr = explode("\r\n", $condition);
- foreach ($conditionArr as $index => $item) {
- preg_match("/^(\w+)\s?([\>\<\=]+)\s?(.*)$/", trim($item), $matches);
- if ($matches && isset($user[$matches[1]]) && version_compare($user[$matches[1]], $matches[3], $matches[2])) {
- $nums++;
- }
- }
- if ($conditionArr && ((stripos($rule['condition'], "||") !== false && $nums > 0) || count($conditionArr) == $nums)) {
- $rulelist[$rule['id']] = strtolower($rule['name']);
- }
- } else {
-
- $rulelist[$rule['id']] = strtolower($rule['name']);
- }
- }
- $_rulelist[$uid] = $rulelist;
-
- if (2 == $this->config['auth_type']) {
-
- Session::set('_rule_list_' . $uid, $rulelist);
- }
- return array_unique($rulelist);
- }
- public function getRuleIds($uid)
- {
-
- $groups = $this->getGroups($uid);
- $ids = [];
- foreach ($groups as $g) {
- $ids = array_merge($ids, explode(',', trim($g['rules'], ',')));
- }
- $ids = array_unique($ids);
- return $ids;
- }
-
- protected function getUserInfo($uid)
- {
- static $user_info = [];
- $user = Db::name($this->config['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];
- }
- }
|