案例:把所須要的auth類放在一個公共的地方引用,我這裏就只是放在了與application同級的extend裏面的org因此我在公共控制器裏面實例爲use \org\Auth; 而後我再公共控制器裏面寫數據庫
class Common extends Base
{
//任何操做加載的時候,會調用此函數
function _initialize(){
if(!Session::get(「uid」)){
$this->error(「請先登陸!」,」admin/login/index」);
}
$AUTH=new \org\Auth();
$mod=request()->module();
$con=request()->controller();
$act=request()->action();
$str=$mod.’/’.$con.’/’.$act; //把模塊、控制器和方法轉換成一個字符串
//echo $str;
if(!$AUTH->check($str,Session::get(「uid」))){
$this->error(「您沒有該操做的權限!」,」admin/index/showError」);
}
}
}app
在各個控制器裏面都繼承這個common控制器。函數
想要實現權限管理功能,還須要建對應的數據表ui
//數據庫
/*
— —————————-
— 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;
*/this
這裏面的字段可能須要修改樣式繼承
而後在後臺建立操做頁面進行操做。字符串