thinkphp5中使用auth許可權控制

2021-08-17 05:36:08 字數 2420 閱讀 2392

首先要建立三個表

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;

然後使用驗證類

use think\request;

$controller=request::instance()->controller();

$module=request::instance()->module();

$action=request::instance()->action();

$url=$module.'/'.$controller.'/'.$action;

check($url, $uid);

其中$uid為登入使用者的id

然後需要在配置檔案上配置
$_config = array(

'auth_on' => true, // 認證開關

'auth_type' => 1, // 認證方式,1為實時認證;2為登入認證。

'auth_group' => 'auth_group', // 使用者組資料表名

'auth_group_access' => 'auth_group_access', // 使用者-使用者組關係表

'auth_rule' => 'auth_rule', // 許可權規則表

'auth_user' => 'user' // 使用者資訊表

);

去下

auth類

thinkphp5的模型中使用關聯

有以下兩個表 artitcle表 表示文章 article的主鍵是id,外來鍵是cateid,cateid對應於cate表的主鍵id。cate表 表示欄目 cate表的主鍵是id。這兩張表表示,每一篇文章屬於乙個欄目,每乙個欄目有很多篇文章。然後編寫model類 article的model類 cla...

ThinkPHP5使用快取

cache 使用復合快取型別 type complex 預設使用的快取 default 驅動方式 type file 在這設定換人的快取方式 快取儲存目錄 path cache path,檔案快取 file 驅動方式 type file 設定不同的快取儲存目錄 path runtime path f...

ThinkPHP5使用QueryList4教程

幾乎每天都有人問我tp5中如何使用querylist4,所以寫了這篇教程。說實話我並不太想寫這篇教程,因為實在是沒有什麼技術含量。在thinkphp5 根目錄執行composer命令安裝querylist composer require jaeger querylist下面演示在index控制器中...