-
composer require wenhainan/thinkphp6-auth
- 配置
// auth配置 自定義數據表位置在 ./config/auth.php里面
[
'auth_on' => 1, // 權限開關 1開啟;0關閉
'auth_type' => 1, // 認證方式,1為實時認證;2為登錄認證。
'auth_group' => 'think_auth_group', // 用戶組數據不帶前綴表名
'auth_group_access' => 'think_auth_group_access', // 用戶-用戶組關系不帶前綴表名
'auth_rule' => 'think_auth_rule', // 權限規則不帶前綴表名
'auth_user' => 'user', // 用戶信息表不帶前綴表名,主鍵自增字段為id
],
- 導入數據
-----`think_` 為自定義的數據表前綴-----------
----------------------------------------------
-- 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 '',
`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\wenhainan\Auth;
// 獲取auth實例
$auth = Auth::instance();
// 檢測權限
if($auth->check('show_button',1)){// 第一個參數是規則名稱,第二個參數是用戶UID
//有顯示操作按鈕的權限
}else{
//沒有顯示操作按鈕的權限
}
<?php
namespace app\admin\controller;
use app\BaseController;
use think\facade\View;
use think\wenhainan\Auth;
class AdminBase extends BaseController
{
public function initialize()
{
$controller = strtolower(request()->controller());
$action = strtolower(request()->action());
$auth = Auth::instance();
// dump($auth);exit;
// 檢測權限
if(!$auth->check($controller.'-'.$action,1)){// 第一個參數是規則名稱,第二個參數是用戶UID
//有顯示操作按鈕的權限
dump('您沒有權限訪問');
}
}
}
- 在Auth.php文件里注釋掉 ['type','=',$type]
- 注釋@eval 修改為
$condition = $command;
- 數據表配置
6.