ThinkPHP5 1許可權認證類

2021-10-24 17:36:24 字數 3410 閱讀 4677

tp-auth

thinkphp5.1許可權認證類

安裝composer require leruge/auth

全域性配置

// 會自動在config配置目錄建立auth.php配置檔案,如果沒有,請手動建立

// 配置引數如下

return [

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

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

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

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

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

'super_ids'         => ,                   // 擁有所有許可權的使用者,如[1, 2, 3],那麼這三個使用者則擁有所有許可權

];匯入資料庫

wdl_為自定義表字首

-- ----------------------------

-- table structure for wdl_auth_group

-- ----------------------------

drop table if exists `tp_auth_group`;

create table `tp_auth_group` (

`id` int(10) unsigned not null auto_increment,

`title` varchar(255) collate utf8_bin default null comment '分組名稱',

`rules` varchar(255) collate utf8_bin default null comment '擁有的許可權,使用逗號分隔',

`create_time` int(11) not null default '0',

`update_time` int(11) not null default '0',

`delete_time` int(11) default null,

primary key (`id`)

) engine=innodb default charset=utf8 collate=utf8_bin;

-- ----------------------------

-- table structure for wdl_auth_group_access

-- ----------------------------

drop table if exists `tp_auth_group_access`;

create table `tp_auth_group_access` (

`id` int(10) unsigned not null auto_increment,

`uid` int(11) default null comment '使用者id',

`group_id` int(11) default null comment '分組id',

`create_time` int(11) not null default '0',

`update_time` int(11) not null default '0',

`delete_time` int(11) default null,

primary key (`id`)

) engine=innodb default charset=utf8 collate=utf8_bin;

-- ----------------------------

-- table structure for wdl_auth_rule

-- ----------------------------

drop table if exists `tp_auth_rule`;

create table `tp_auth_rule` (

`id` int(10) unsigned not null auto_increment,

`pid` int(11) default '0' comment '上級id',

`name` varchar(255) collate utf8_bin default null comment '許可權規則',

`title` varchar(255) collate utf8_bin default null comment '規則中文名',

`icon` varchar(255) collate utf8_bin default null comment '選單圖示',

`sort` int(11) default '9' comment '排序',

`is_show` int(11) default '1' comment '1顯示;0隱藏',

`create_time` int(11) not null default '0',

`update_time` int(11) not null default '0',

`delete_time` int(11) default null,

primary key (`id`)

) engine=innodb default charset=utf8 collate=utf8_bin;

原理auth類的原理網上介紹的比較多,這裡就不在做過多的介紹了。對外提供了乙個check、authlist、userruleidlist方法,用於校驗許可權。

使用方法

有倆種使用方法

使用許可權類auth

引入類庫use leruge\auth\auth;

獲取auth例項,$auth = new auth();

校驗許可權

if ($auth->check(模組/控制器/方法, 使用者id)) else

使用許可權類門面auth

引入類庫use leruge\auth\authfacade;

校驗許可權

if (authfacade::check(模組/控制器/方法, 使用者id)) else

check方法引數

check($name, $uid, $relation)

$name 驗證的許可權,字串是單條許可權,多條許可權使用陣列

$uid 驗證使用者id

$relation or滿足一條則通過;and全部滿足才能通過

authlist方法

authlist($uid)

返回許可權列表,不區分上下級

userruleidlist方法

userruleidlist($uid)

返回許可權id陣列

ThinkPHP5 1鉤子和行為

tp5.1的行為是乙個比較抽象的概念,執行的流程使用者的註冊,登入,退出登入等等都可以作為乙個行為。而不同的行為之間也具有位置共同性,比如,有些行為的作用位置在使用者註冊後,在登入之後,退出登入之後,等等有些行為的作用位置都是在應用執行前,有些行為都是在模板輸出之後,把這些行為發生作用的位置稱之為鉤...

thinkphp5 1 匯入excel檔案

public function importexcel 限制上傳 型別 ext substr strrchr files file name 1 if ext xls ext xlsx 讀取 filename files file tmp name reader iofactory createre...

ThinkPHP5 1學習 模組設計

一 目錄結構 thinkphp5.1 預設是多模組架構,也可以設定為單模組操作 手冊摘入的結構列表 多模組設計在 url 訪問時,必須指定響應的模組名,比如 public test abc eat 如果你只有 test 這乙個模組時,你可以繫結這個模組,從而省略寫法 此時,url 呼叫就變成了 pu...