tp5 auth權限設置


案例:把所需要的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”);
}
}
}

在各個控制器里面都繼承這個common控制器。

 

想要實現權限管理功能,還需要建對應的數據表

//數據庫
/*
— —————————-
— 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;
*/

這里面的字段可能需要修改樣式

然后在后台創建操作頁面進行操作。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM