Thinkphp5 Auth許可權認證

2021-09-07 18:47:03 字數 2810 閱讀 1397

auth類在thinkphp類庫里是原本就有的,thinkphp5裡沒了,這裡是將其修改為thinkphp5適用

auth類的特點是

/**

* 許可權認證類

* 功能特性:

* 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欄位就可以定義規則表示式。 如定義》5 and <100 表示使用者的分數在5-100之間時這條規則才會通過。

*/

接著匯入資料庫表

//資料庫

/*-- ----------------------------

-- 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方法進行許可權認證

//許可權認證

$auth = new \auth\auth();

$request = request::instance();

if (!$auth->check($request->module() . '-' . $request->controller() . '-' . $request->action(), sid))

ThinkPHP5下Auth許可權認證

admin管理員 表裡有admin1 admin2 adimn 3個管理員 auth group 管理員組 表裡有 超級管理員 普通管理員 文章發布管理員 auth rule 管理規則 文章新增 文章刪除 文章修改 文章檢視 auth group access 裡面只有兩個字段,uid 哪個使用者 ...

thinkphp5中使用auth許可權控制

首先要建立三個表 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 defau...

Thinkphp 3 2 下的auth許可權認證

auth 認證相對於rbac來說,更加靈活方便,防止後面忘記來做做記錄。首先需要建立表 4張 1 auth rule 它是所有需要認證的許可權點集合,需要把認證的許可權記錄到資料庫。drop table if exists hskj auth rule create table hskj auth ...