## 呆錯權限組件
* 呆錯權限組件基于數據表dc_op進行開發,不支持表單擴展。
* 呆錯權限組件可以理解為驗證URL請求操作條件,如(admin/index/index),可以定義到URL參數。
* 呆錯權限組件是通過將用戶角色(用戶組)擁有的訪問節點保存在數據庫中,在插件開發時需要對用戶的訪問請求權力限制時進行驗證。
* 呆錯權限組件支持給某一個用戶單獨添加任意節點,只需編輯該用戶資料時添加相應節點即可。
* 助手函數與常用公用類庫提供的方法適用于自行開發或封裝模板調用標簽等。
* 點此查看呆錯官方提供的呆錯權限組件[開發實例](https://www.daicuo.org/help/66)。
## 后臺
* 可以直接通過 (網站后臺 > 系統 > 權限管理)對網站操作的權限進行統一管理。
## 前臺
* 需要繼承前臺基礎類庫后才能獲取當前用戶所擁有的權限節點列表以及使用驗證方法。
* 角色組的查詢、升級、刪除等基本需要配合呆錯用戶組件、呆錯權限組件。
## 使用方法
* 框架控制器基類 apps/common/Base.php 提供 _authCheck 方法驗證,在繼承了基類控制器后,只需在控制器里定義權限驗證屬性后調用驗證方法即可,以Admin后臺模塊為例:
* 如果沒有繼承基類控制器,則可以通過 \daicuo\Auth::check 方法 或 助手函數 DcAuthCheck 手動驗證。
```
class Admin extends Base
{
// 系統權限屬性
protected $auth = [
'check' => true,//是否開啟權限驗證,默認為false
'rule' => '',//自定義驗證規則,留空則使用默認規則(模塊名/控制器名/操作名)
'none_login' => ['admin/index/login','admin/index/logout'],//不需要登錄的方法(自然就不需要鑒權)
'none_right' => [],//需要登錄但不需要鑒權的節點
'error_login' => 'admin/index/login',//未登錄時回跳路徑
'error_right' => '',//未取得權限時回跳路徑
];
/**
* 繼承初始化方法
*/
public function _initialize()
{
// 繼承上級
parent::_initialize();
// 權限驗證
$this->_authCheck();
}
}
```
## 權限驗證實例
```
//定義驗證規則
$this->site['auth_rule'] = $this->site['module'].'/'.$this->site['controll'].'/'.$this->site['action'];
//定義用戶擁有的角色或權限節點
$this->site['user']['user_capabilities'] = ['guest'];
//通過Auth模塊的Check方法驗證
if(false == \daicuo\Auth::check($this->site['auth_rule'], $this->site['user']['user_capabilities']) ){
$this->error(lang('not_authorized'));
}
```
## 全局配置實例
```
'user_roles' => [
//游客擁有兩個訪問權限
'guest' => [
'admin/index/login',
'admin/index/logout',
],
//管理員擁有全部權限
'administrator' => [
'*',
],
//動態節點內置專用角色名(不會出現在角色名列表內)
'caps' => [
'index/index/index',
],
];
```
## 擴展角色實例
```
//方法1:增加一個test的用戶組并且擁有三個節點權限
config('user_roles.test',[
'test/index/index',
'test/index/create',
'test/index/save',
]);
//方法2:批量增加test用戶組的權限關系(推薦)
\daicuo\Op::write([
'test' => 'index/demo/testa',
'test' => 'index/demo/testb',
],'common','system','auth','0','no');
```
## 注冊節點實例
```
//方法1:將任意節點注冊到系統的專用角色(caps)名下,需要時可以給用戶單獨使用
$caps = array_merge(config('user_roles.caps'),[
'test/index/index',
'test/index/logout',
'test/addon/index?action=index',
]);
config('user_roles.caps',$caps);
//方法2:用戶表增加自定義字段user_caps并保存對應的權限節點即可,因為框架的基類驗證方法已預埋了此字段。
if ( false == \daicuo\Auth::check($this->auth['rule'], $this->site['user']['user_capabilities'], $this->site['user']['user_caps']) ) {
$this->error('error');
}
```
## 常用方法
* model('common/Auth','loglic')->write($post=[])
* model('common/Auth','loglic')->delete($args=[])
* model('common/Auth','loglic')->get($args=[])
* model('common/Auth','loglic')->select($args=[])
* model('common/Auth','loglic')->install($args=[])
* model('common/Auth','loglic')->unInstall($args=[])
## **助手函數**
* DcAuthConfig 獲取系統已配置的角色與權限對應關系
* DcAuthOption 獲取權限列表keyValue數組格式