## 驗證器功能說明
驗證器 Godok\Org\Auth
驗證器提供了用戶登錄狀態保存,用戶權限驗證,用戶及權限信息獲取的功能,用戶信息保存在session中,session未過期之前一直有效
默認登錄后保存了如下信息
~~~
$user=[
'id'=>用戶唯一標識,
'username'=>帳號,
'nickname'=>昵稱,
'avatar'=>頭像url,
'email'=>郵箱,
'phone'=>電話,
'rules'=>[1,5,6,...] /*權限id*/,
'groupids'=>[2,3,4,...]/*用戶組id*/
];
\Godok\Org\Auth::user($user);
~~~
**讀用戶信息**
~~~
\Godok\Org\Auth::user() //讀取用戶信息$user
\Godok\Org\Auth::user('name') //讀取$user['name']
~~~
**寫用戶信息**
~~~
\Godok\Org\Auth::user( ['name'=>'test'] ) //參數1為數組表示覆蓋用戶信息:$use = ['name'=>'test']
\Godok\Org\Auth::set('name','test') //寫$user['name'] = 'test',第二個參數也可以是數組
~~~
**刪除用戶信息**
~~~
\Godok\Org\Auth::clear() //清空用戶信息
\Godok\Org\Auth::set('name') //刪除$user['name']
~~~
**獲取用戶組信息**
~~~
\Godok\Org\Auth::getGroups(); //獲取當前用戶的組信息(二維數組:包括 id rules 子鍵)
\Godok\Org\Auth::getGroups($uid); //獲取指定用戶的組信息(二維數組:包括 id rules 子鍵)
~~~
**獲取用戶組(id)**
~~~
\Godok\Org\Auth::getGroupids(); //獲取當前用戶的組(一維數組:由id組成)
\Godok\Org\Auth::getGroupids($uid); //獲取指定用戶的組(一維數組:由id組成)
~~~
**獲取用戶權限(id)**
~~~
\Godok\Org\Auth::getRules(); //獲取當前用戶的權限(一維數組:由id組成),繼承游客和登錄用戶組
\Godok\Org\Auth::getRules($uid); //獲取指定用戶的權限(一維數組:由id組成),繼承游客和登錄用戶組
\Godok\Org\Auth::getRules($groups); //獲取傳入用戶組信息的權限集(一維數組:由id組成),不繼承
\Godok\Org\Auth::getRules($groupids); //獲取指定組的權限集(一維數組:由id組成),不繼承
~~~
**手動驗證**
自動驗證是驗證的url請求,但是有些權限不是通過請求來判斷的,那么就需要在業務中手動去判斷
>Auth::check($action, $uid)
@$action 匹配權限規則中的action值
@$uid 缺省值是當前登錄用戶的id
return boolen,驗證通過時返回true,否則返回false
參考說明:
用傳入的參數到goa_rule表中尋找規則,未找到返回false,找到繼續判斷用戶是否擁有該權限
~~~
Auth::check('edit') //判斷:module='' && controller='' && action='edit' && condition = ''
Auth::check('Uer/edit') //判斷:module='' && controller='User' && action='edit' && condition = ''
Auth::check('auth/User/edit?type=1') //module='auth' && controller='User' && action='edit' && condition = 'type=1'
//多條用“,”或者“|”隔開,前者是“and”判斷,后者是“or”判斷
//如:
Auth::check('edit,add,delete')
Auth::check('edit|add|delete')
~~~
>手動驗證是url驗證的補充,不支持正則表達式
**返回權限菜單樹**
>\Godok\Org\Auth::menu($where, $pid)
$where 為可選項
$pid 獲取$pid下的子菜單,默認為0,獲取所有菜單
*注意:無論傳入什么參數,都只能獲取到當前用戶擁有的權限菜單
返回的權限菜單例子
~~~
Array
(
[0] => Array
(
[id] => 1
[pid] => 0
[title] => 系統設置
[module] =>
[controller] =>
[action] =>
[status] => 1
[ismenu] => 1
[condition] =>
[icon] => fa fa-gears
[listorder] => 0
[children] => Array
(
[0] => Array
(
[id] => 172
[pid] => 1
[title] => 用戶管理
[module] => auth
[controller] => User
[action] => index
[status] => 1
[ismenu] => 1
[condition] =>
[icon] =>
[listorder] => 1
)
)
)
[1] => Array
(
[id] => 194
[pid] => 0
[title] => 個人中心
[module] => auth
[controller] => Profile
[action] => index
[status] => 1
[ismenu] => 0
[condition] =>
[icon] =>
[listorder] => 2
)
)
~~~
路由驗證未通過會根據請求類型返回一個錯誤的json數據或者一個錯誤提示消息
~~~
{
code : -1,
msg : "請登錄",
url : "/auth/Login"
}
~~~