auth類在thinkphp類庫裏是本來就有的,thinkphp5裏沒了,這裏是將其修改成thinkphp5適用php
auth類的特色是git
/** * 權限認證類 * 功能特性: * 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之間時這條規則纔會經過。 */
接着導入數據庫表github
//數據庫 /* -- ---------------------------- -- think_auth_rule,規則表, -- id:主鍵,name:規則惟一標識, title:規則中文名稱 status 狀態:爲1正常,爲0禁用,condition:規則表達式,爲空表示存在就驗證,不爲空表示按照條件驗證 -- ---------------------------- 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 '', `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; -- ---------------------------- -- think_auth_group 用戶組表, -- id:主鍵, title:用戶組中文名稱, rules:用戶組擁有的規則id, 多個規則","隔開,status 狀態:爲1正常,爲0禁用 -- ---------------------------- 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 DEFAULT CHARSET=utf8; -- ---------------------------- -- think_auth_group_access 用戶組明細表 -- uid:用戶id,group_id:用戶組id -- ---------------------------- DROP TABLE IF EXISTS `think_auth_group_access`; CREATE TABLE `think_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; */
接着在須要權限驗證控制器裏建立_initialize方法進行權限認證thinkphp
//權限認證 $auth = new \Auth\Auth(); $request = Request::instance(); if (!$auth->check($request->module() . '-' . $request->controller() . '-' . $request->action(), SID)) {// 第一個參數是規則名稱,第二個參數是用戶UID /* return array('status'=>'error','msg'=>'有權限!');*/ $this->error('你沒有權限'); }
其中$request->module();$request->controller();$request->action();分別問獲取模塊名、控制器名、操做名
能夠在這裏下載修改好的類thinkphp5auth類下載地址下載好後放入extend擴展裏Auth文件夾下數據庫