<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                我們來看下一個關于基于規則的權限類。 ~~~ -- ---------------------------- -- auth_rule,規則表, -- id:主鍵,name:規則唯一標識, title:規則中文名稱 status 狀態:為1正常,為0禁用,condition:規則表達式,為空表示存在就驗證,不為空表示按照條件驗證 -- ---------------------------- DROP TABLE IF EXISTS `auth_rule`; CREATE TABLE `bk_auth_rule` ( `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, `name` char(80) NOT NULL DEFAULT '', `title` char(20) NOT NULL DEFAULT '', `type` tinyint(1) NOT NULL DEFAULT '1', `status` tinyint(1) NOT NULL DEFAULT '1', `condition` char(100) NOT NULL DEFAULT '', # 規則附件條件,滿足附加條件的規則,才認為是有效的規則 PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; -- ---------------------------- -- auth_group 用戶組表, -- id:主鍵, title:用戶組中文名稱, rules:用戶組擁有的規則id, 多個規則","隔開,status 狀態:為1正常,為0禁用 -- ---------------------------- DROP TABLE IF EXISTS `auth_group`; CREATE TABLE `bk_auth_group` ( `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, `title` char(100) NOT NULL DEFAULT '', `status` tinyint(1) NOT NULL DEFAULT '1', `rules` char(80) NOT NULL DEFAULT '', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; -- ---------------------------- -- auth_group_access 用戶組明細表 -- uid:用戶id,group_id:用戶組id -- ---------------------------- DROP TABLE IF EXISTS `auth_group_access`; CREATE TABLE `bk_auth_group_access` ( `uid` mediumint(8) unsigned NOT NULL, `group_id` mediumint(8) unsigned NOT NULL, UNIQUE KEY `uid_group_id` (`uid`,`group_id`), KEY `uid` (`uid`), KEY `group_id` (`group_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; ~~~ ~~~ class Auth { //默認配置 protected $config = array( 'auth_on' => true, // 認證開關 'auth_type' => 2, // 認證方式,1為實時認證;2為登錄認證。 'auth_group' => 'auth_group', // 用戶組數據表名 'auth_group_access' => 'auth_group_access', // 用戶-用戶組關系表 'auth_rule' => 'auth_rule', // 權限規則表 'auth_user' => 'auth_member' // 用戶信息表 ); public function __construct() { if (Config::get('auth_config')) { $this->config = array_merge($this->config, Config::get('auth_config')); //可設置配置項 auth_config, 此配置項為數組。 } } /** * 檢查權限 * @param name string|array 需要驗證的規則列表,支持逗號分隔的權限規則或索引數組 * @param uid int 認證用戶的id * @param string mode 執行check的模式 * @param relation string 如果為 'or' 表示滿足任一條規則即通過驗證;如果為 'and'則表示需滿足所有規則才能通過驗證 * return boolean 通過驗證返回true;失敗返回false */ public function check($name, $uid, $type = 1, $mode = 'url', $relation = 'or') { if (!$this->config['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]; // } $name = strpos($name, ',') !== false ? explode(',', $name) : [$name]; } $list = []; //保存驗證通過的規則名 if ($mode == 'url') { $REQUEST = unserialize(strtolower(serialize($_REQUEST))); } foreach ($authList as $auth) { $query = preg_replace('/^.+\?/U', '', $auth); if ($mode == 'url' && $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)) { $list[] = $auth; } } if ($relation == 'or' and ! empty($list)) { return true; } $diff = array_diff($name, $list); if ($relation == 'and' and empty($diff)) { return true; } return false; } /** * 根據用戶id獲取用戶組,返回值為數組 * @param uid int 用戶id * return array 用戶所屬的用戶組 [ * ['uid'=>'用戶id','group_id'=>'用戶組id','title'=>'用戶組名稱','rules'=>'用戶組擁有的規則id,多個,號隔開'), * ...) */ public function getGroups($uid) { static $groups = []; if (isset($groups[$uid])) { return $groups[$uid]; } $user_groups = Db::view($this->config['auth_group_access'], 'uid,group_id')->view($this->config['auth_group'], 'title,rules', "{$this->config['auth_group_access']}.group_id={$this->config['auth_group']}.id") ->where(['uid' => $uid, 'status' => 1])->select(); $groups[$uid] = $user_groups ? $user_groups : []; return $groups[$uid]; } /** * 獲得權限列表 * @param integer $uid 用戶id * @param integer $type */ protected function getAuthList($uid, $type) { static $_authList = []; //保存用戶驗證通過的權限列表 $t = implode(',', (array) $type); if (isset($_authList[$uid . $t])) { return $_authList[$uid . $t]; } if ($this->config['auth_type'] == 2 && Session::has('_auth_list_' . $uid . $t)) { return Session::get('_auth_list_' . $uid . $t); } //讀取用戶所屬用戶組 $groups = $this->getGroups($uid); $ids = []; //保存用戶所屬用戶組設置的所有權限規則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, 'status' => 1, ]; //讀取用戶組所有權限規則 $rules = Db::name($this->config['auth_rule'])->where($map)->field('condition,name')->select(); //循環規則,判斷結果。 $authList = []; // foreach ($rules as $rule) { if (!empty($rule['condition'])) { //根據condition進行驗證 $this->getUserInfo($uid); //獲取用戶信息,一維數組 $command = preg_replace('/\{(\w*?)\}/', '$user[\'\\1\']', $rule['condition']); @(eval('$condition=(' . $command . ');')); $condition && $authList[] = strtolower($rule['name']); } else { $authList[] = strtolower($rule['name']); //只要存在就記錄 } } $_authList[$uid . $t] = $authList; if ($this->config['auth_type'] == 2) { $_SESSION['_auth_list_' . $uid . $t] = $authList; //規則列表結果保存到session } return array_unique($authList); } /** * 獲得用戶資料,根據自己的情況讀取數據庫 */ protected function getUserInfo($uid) { static $userinfo = []; if (!isset($userinfo[$uid])) { $userinfo[$uid] = Db::name($this->config['auth_user'])->where(['uid' => $uid])->find(); } return $userinfo[$uid]; } } ~~~ 這里的調用方式,先是讀出權限表的rules,然后通過check和getGroup函數來獲取和檢測用戶的權限組。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看