Permission operation
Create a new Auth.php, copy the code below, and put the file into the extend new folder org
<?php class Auth { /** * @var object 對象實例 */ protected static $instance; /** * 當前請求實例 * @var Request */ protected $request; //默認配置 protected $config = [ 'auth_on' => 1, // 權(quán)限開關(guān) 'auth_type' => 1, // 認證方式,1為實時認證;2為登錄認證。 'auth_group' => 'auth_group', // 用戶組數(shù)據(jù)表名 'auth_group_access' => 'auth_group_access', // 用戶-用戶組關(guān)系表 'auth_rule' => 'auth_rule', // 權(quán)限規(guī)則表 'auth_user' => 'member', // 用戶信息表 ]; /** * 類架構(gòu)函數(shù) * Auth constructor. */ public function __construct() { //可設(shè)置配置項 auth, 此配置項為數(shù)組。 if ($auth = Config::get('auth')) { $this->config = array_merge($this->config, $auth); } // 初始化request $this->request = Request::instance(); } /** * 初始化 * @access public * @param array $options 參數(shù) * @return \think\Request */ public static function instance($options = []) { if (is_null(self::$instance)) { self::$instance = new static($options); } return self::$instance; } /** * 檢查權(quán)限 * @param $name string|array 需要驗證的規(guī)則列表,支持逗號分隔的權(quán)限規(guī)則或索引數(shù)組 * @param $uid int 認證用戶的id * @param int $type 認證類型 * @param string $mode 執(zhí)行check的模式 * @param string $relation 如果為 'or' 表示滿足任一條規(guī)則即通過驗證;如果為 'and'則表示需滿足所有規(guī)則才能通過驗證 * @return bool 通過驗證返回true;失敗返回false */ public function check($name, $uid, $type = 1, $mode = 'url', $relation = 'or') { if (!$this->config['auth_on']) { return true; } // 獲取用戶需要驗證的所有有效規(guī)則列表 $authList = $this->getAuthList($uid, $type); if (is_string($name)) { $name = strtolower($name); if (strpos($name, ',') !== false) { $name = explode(',', $name); } else { $name = [$name]; } } $list = []; //保存驗證通過的規(guī)則名 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); //解析規(guī)則中的param $intersect = array_intersect_assoc($REQUEST, $param); $auth = preg_replace('/\?.*$/U', '', $auth); if (in_array($auth, $name) && $intersect == $param) { //如果節(jié)點相符且url參數(shù)滿足 $list[] = $auth; } } else { if (in_array($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; } /** * 根據(jù)用戶id獲取用戶組,返回值為數(shù)組 * @param $uid int 用戶id * @return array 用戶所屬的用戶組 array( * array('uid'=>'用戶id','group_id'=>'用戶組id','title'=>'用戶組名稱','rules'=>'用戶組擁有的規(guī)則id,多個,號隔開'), * ...) */ public function getGroups($uid) { static $groups = []; if (isset($groups[$uid])) { return $groups[$uid]; } // 轉(zhuǎn)換表名 $auth_group_access = Loader::parseName($this->config['auth_group_access'], 1); $auth_group = Loader::parseName($this->config['auth_group'], 1); // 執(zhí)行查詢 $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]; } /** * 獲得權(quán)限列表 * @param integer $uid 用戶id * @param integer $type * @return array */ protected function getAuthList($uid, $type) { static $_authList = []; //保存用戶驗證通過的權(quán)限列表 $t = implode(',', (array)$type); if (isset($_authList[$uid . $t])) { return $_authList[$uid . $t]; } if (2 == $this->config['auth_type'] && Session::has('_auth_list_' . $uid . $t)) { return Session::get('_auth_list_' . $uid . $t); } //讀取用戶所屬用戶組 $groups = $this->getGroups($uid); $ids = []; //保存用戶所屬用戶組設(shè)置的所有權(quán)限規(guī)則id foreach ($groups as $g) { $ids = array_merge($ids, explode(',', trim($g['rules'], ','))); } $ids = array_unique($ids); if (empty($ids)) { $_authList[$uid . $t] = []; return []; } $map = [ 'id' => ['in', $ids], 'type' => $type ]; //讀取用戶組所有權(quán)限規(guī)則 $rules = Db::name($this->config['auth_rule'])->where($map)->field('condition,name')->select(); //循環(huán)規(guī)則,判斷結(jié)果。 $authList = []; // foreach ($rules as $rule) { if (!empty($rule['condition'])) { //根據(jù)condition進行驗證 $user = $this->getUserInfo($uid); //獲取用戶信息,一維數(shù)組 $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 (2 == $this->config['auth_type']) { //規(guī)則列表結(jié)果保存到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_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]; } }
Create a new controller, the verification code is as follows:
<?php namespace app\admin\controller; use \think\Controller; use think\Loader; class Base extends Controller { protected $current_action; public function _initialize() { Loader::import("org/Auth", EXTEND_PATH); $auth=new \Auth(); $this->current_action = request()->module().'/'.request()->controller().'/'.lcfirst(request()->action()); $result = $auth->check($this->current_action,session('user_id')); if($result){ echo "權(quán)限驗證成功"; } } }