天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

ThinkPHP權限認證Auth實例詳解

本文以實例代碼的形式深入剖析了Thinkphp權限認證Auth的實現原理與方法,具體步驟如下:

mysql數據庫部分sql代碼:

-- ------------------------------ Table structure for think_auth_group-- ----------------------------DROP TABLE IF EXISTS `think_auth_group`;CREATE TABLE `think_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 AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='用戶組表';-- ------------------------------ Records of think_auth_group-- ----------------------------INSERT INTO `think_auth_group` VALUES ('1', '管理組', '1', '1,2');-- ------------------------------ Table structure for think_auth_group_access-- ----------------------------DROP TABLE IF EXISTS `think_auth_group_access`;CREATE TABLE `think_auth_group_access` ( `uid` mediumint(8) unsigned NOT NULL COMMENT '用戶id', `group_id` mediumint(8) unsigned NOT NULL COMMENT '用戶組id', UNIQUE KEY `uid_group_id` (`uid`,`group_id`), KEY `uid` (`uid`), KEY `group_id` (`group_id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='用戶組明細表';-- ------------------------------ Records of think_auth_group_access-- ----------------------------INSERT INTO `think_auth_group_access` VALUES ('1', '1');INSERT INTO `think_auth_group_access` VALUES ('1', '2');-- ------------------------------ Table structure for think_auth_rule-- ----------------------------DROP TABLE IF EXISTS `think_auth_rule`;CREATE TABLE `think_auth_rule` ( `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, `name` char(80) NOT NULL DEFAULT '' COMMENT '規則唯一標識', `title` char(20) NOT NULL DEFAULT '' COMMENT '規則中文名稱', `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '狀態:為1正常,為0禁用', `type` char(80) NOT NULL, `condition` char(100) NOT NULL DEFAULT '' COMMENT '規則表達式,為空表示存在就驗證,不為空表示按照條件驗證', PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`)) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COMMENT='規則表';-- ------------------------------ Records of think_auth_rule-- ----------------------------INSERT INTO `think_auth_rule` VALUES ('1', 'Home/index', '列表', '1', 'Home', '');INSERT INTO `think_auth_rule` VALUES ('2', 'Home/add', '添加', '1', 'Home', '');INSERT INTO `think_auth_rule` VALUES ('3', 'Home/edit', '編輯', '1', 'Home', '');INSERT INTO `think_auth_rule` VALUES ('4', 'Home/delete', '刪除', '1', 'Home', '');DROP TABLE IF EXISTS `think_user`;CREATE TABLE `think_user` ( `id` int(11) NOT NULL, `username` varchar(30) DEFAULT NULL, `password` varchar(32) DEFAULT NULL, `age` tinyint(2) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;-- ------------------------------ Records of think_user-- ----------------------------INSERT INTO `think_user` VALUES ('1', 'admin', '21232f297a57a5a743894a0e4a801fc3', '25');

配置文件Application/Common/Conf/config.php部分:

<?phpreturn array(  //'配置項'=>'配置值'  'DB_DSN' => '', // 數據庫連接DSN 用于PDO方式  'DB_TYPE' => 'mysql', // 數據庫類型  'DB_HOST' => 'localhost', // 服務器地址  'DB_NAME' => 'thinkphp', // 數據庫名  'DB_USER' => 'root', // 用戶名  'DB_PWD' => 'root', // 密碼  'DB_PORT' => 3306, // 端口  'DB_PREFIX' => 'think_', // 數據庫表前綴     'AUTH_CONFIG' => array(    'AUTH_ON' => true, //認證開關    'AUTH_TYPE' => 1, // 認證方式,1為時時認證;2為登錄認證。    'AUTH_GROUP' => 'think_auth_group', //用戶組數據表名    'AUTH_GROUP_ACCESS' => 'think_auth_group_access', //用戶組明細表    'AUTH_RULE' => 'think_auth_rule', //權限規則表    'AUTH_USER' => 'think_user'//用戶信息表  ));

項目Home控制器部分Application/Home/Controller/IndexController.class.php代碼:

<?phpnamespace Home/Controller;use Think/Controller;class IndexController extends Controller {  public function index() {    $Auth = new /Think/Auth();    //需要驗證的規則列表,支持逗號分隔的權限規則或索引數組    $name = MODULE_NAME . '/' . ACTION_NAME;    //當前用戶id    $uid = '1';    //分類    $type = MODULE_NAME;    //執行check的模式    $mode = 'url';    //'or' 表示滿足任一條規則即通過驗證;    //'and'則表示需滿足所有規則才能通過驗證    $relation = 'and';    if ($Auth->check($name, $uid, $type, $mode, $relation)) {      die('認證:成功');    } else {      die('認證:失敗');    }  }}

以上這些代碼就是最基本的驗證代碼示例。

下面是源碼閱讀:

1、權限檢驗類初始化配置信息:

$Auth = new /Think/Auth();

創建一個對象時程序會合并配置信息
程序會合并Application/Common/Conf/config.php中的AUTH_CONFIG數組

  public function __construct() {    $prefix = C('DB_PREFIX');    $this->_config['AUTH_GROUP'] = $prefix . $this->_config['AUTH_GROUP'];    $this->_config['AUTH_RULE'] = $prefix . $this->_config['AUTH_RULE'];    $this->_config['AUTH_USER'] = $prefix . $this->_config['AUTH_USER'];    $this->_config['AUTH_GROUP_ACCESS'] = $prefix . $this->_config['AUTH_GROUP_ACCESS'];    if (C('AUTH_CONFIG')) {      //可設置配置項 AUTH_CONFIG, 此配置項為數組。      $this->_config = array_merge($this->_config, C('AUTH_CONFIG'));    }  }

2、檢查權限:

check($name, $uid, $type = 1, $mode = 'url', $relation = 'or')

大體分析一下這個方法

首先判斷是否關閉權限校驗 如果配置信息AUTH_ON=>false 則不會進行權限驗證 否則繼續驗證權限

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 = array($name);  }}

所以$name參數是不區分大小寫的,最終都會轉換成小寫


開啟url模式時全部轉換為小寫:

if ($mode == 'url') {  $REQUEST = unserialize(strtolower(serialize($_REQUEST)));}

權限校驗核心代碼段之一,即循環所有該用戶權限 判斷 當前需要驗證的權限 是否 在用戶授權列表中:

foreach ($authList as $auth) {  $query = preg_replace('/^.+/?/U', '', $auth);//獲取url參數  if ($mode == 'url' && $query != $auth) { parse_str($query, $param); //獲取數組形式url參數 $intersect = array_intersect_assoc($REQUEST, $param); $auth = preg_replace('//?.*$/U', '', $auth);//獲取訪問的url文件 if (in_array($auth, $name) && $intersect == $param) { //如果節點相符且url參數滿足   $list[] = $auth; }  } else if (in_array($auth, $name)) { $list[] = $auth;  }}

in_array($auth, $name) 如果 權限列表中 其中一條權限 等于 當前需要校驗的權限 則加入到$list中
注:

$list = array(); //保存驗證通過的規則名if ($relation == 'or' and !empty($list)) {  return true;}$diff = array_diff($name, $list);if ($relation == 'and' and empty($diff)) {  return true;}$relation == 'or' and !empty($list); //當or時 只要有一條是通過的 則 權限為真$relation == 'and' and empty($diff); //當and時 $name與$list完全相等時 權限為真

3、獲取權限列表:

$authList = $this->getAuthList($uid, $type); //獲取用戶需要驗證的所有有效規則列表

這個主要流程:

獲取用戶組

$groups = $this->getGroups($uid);//SELECT `rules` FROM think_auth_group_access a INNER JOIN think_auth_group g on a.group_id=g.id WHERE ( a.uid='1' and g.status='1' )

簡化操作就是:

SELECT `rules` FROM think_auth_group WHERE STATUS = '1' AND id='1'//按正常流程 去think_auth_group_access表中內聯有點多余....!

取得用戶組rules規則字段 這個字段中保存的是think_auth_rule規則表的id用,分割

$ids就是$groups變量最終轉換成的 id數組:

$map = array(  'id' => array('in', $ids),  'type' => $type,  'status' => 1,);

取得think_auth_rule表中的規則信息,之后循環:

foreach ($rules as $rule) {   if (!empty($rule['condition'])) { //根據condition進行驗證    $user = $this->getUserInfo($uid); //獲取用戶信息,一維數組    $command = preg_replace('//{(/w*?)/}/', '$user[/'//1/']', $rule['condition']);    //dump($command);//debug    @(eval('$condition=(' . $command . ');'));    if ($condition) {     $authList[] = strtolower($rule['name']);    }   } else {    //只要存在就記錄    $authList[] = strtolower($rule['name']);   }  }if (!empty($rule['condition'])) { //根據condition進行驗證

這里就可以明白getUserInfo 會去獲取配置文件AUTH_USER對應表名 去查找用戶信息

重點是:

$command = preg_replace('//{(/w*?)/}/', '$user[/'//1/']', $rule['condition']);@(eval('$condition=(' . $command . ');'));

'//{(/w*?)/}/ 可以看成要匹配的文字為 {字符串} 那么 {字符串} 會替換成$user['字符串']
$command =$user['字符串']

如果

$rule['condition'] = '{age}';$command =$user['age']$rule['condition'] = '{age} > 5';$command =$user['age'] > 10@(eval('$condition=(' . $command . ');'));

即:

$condition=($user['age'] > 10);

這時再看下面代碼 如果為真則加為授權列表

if ($condition) {   $authList[] = strtolower($rule['name']);}

php技術ThinkPHP權限認證Auth實例詳解,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 秋霞电影网伦大理电影在线观看 | 777米奇影院第七色色 | 51久久夜色精品国产 | 久久亚洲A片COM人成A | 19不插片免费视频 | 4399亚洲AV无码V无码网站 | 爱啪国产精品视频在线 | 成人区精品一区二区不卡AV免费 | 日本xxxxx按摩19 | 国产盗摄一区二区 | 男人叼女人 | 少妇精品无码一区二区三区 | 激情男女高潮射精AV免费 | 欧洲最大无人区免费高清完整版 | 日日射夜夜干夜夜插在线播放 | 欧美手机在线 | 秋霞三级理伦免费观看 | 看黄色片子 | 日韩一区二区三区免费体验 | 欧美亚洲色帝国 | BL低喘贯穿顶弄老师H | 寂寞骚妇女被后入式抽插 | 一二三四中文字幕在线看 | 精品国产国产综合精品 | 91热久久免费频精品99欧美 | 国产精品99亚发布 | 18videosex性欧美黑色 | 免费在线观看的毛片 | 国产AV无码成人黄网站免费 | 大地影院在线播放 | 俄罗斯性xxxx | 無码一区中文字幕少妇熟女网站 | beeg日本老妇人 | 青娱乐极品视觉盛宴av | 日本三级按摩推拿按摩 | 黄色三级图片 | 国产午夜精品理论片久久影视 | 啊灬啊别停灬用力啊在线观看视频 | 狼群资源网中文字幕 | 久久精品亚洲AV无码三区观看 | 麻豆精品无码久久久久久久久 |