0
我想根據CakePHP博客教程(http://book.cakephp.org/3.0/en/tutorials-and-examples/blog-auth-example/auth.html)中的一個設置用戶訪問模型,但角色位於單獨的表中,並通過UserRoles錶鏈接到用戶。如何在通過CakePHP 3.0中的錶鏈接模型時檢索信息?
我現在有在型號/表/ UsersTable.php以下:
$this->belongsToMany('Roles', [
'through' => 'UserRoles'
]);
,並在型號/表/ RolesTable.php以下:
$this->belongsToMany('Users', [
'through' => 'UserRoles'
]);
,並在型號以下/ Table/UserRolesTable.php:
$this->belongsTo('Users', [
'foreignKey' => 'user_id'
]);
$this->belongsTo('Roles', [
'foreignKey' => 'role_id'
]);
我已創建查看,創建和管理員角色。我試圖弄清楚如何在AppController.php中檢查用戶的角色或角色。這是當角色併入用戶對象給出的簡單例子:
public function isAuthorized($user)
{
// Admin can access every action
if (isset($user['role']) && $user['role'] === 'admin') {
return true;
}
// Default deny
return false;
}
我不知道如何訪問用戶對象,並得到從AppController中文件的用戶ID用戶的角色。由於用戶沒有直接鏈接到角色,我將如何從IsAuthorized函數訪問角色信息?如何在用另一個錶鏈接時執行查找以檢索用戶的角色? 謝謝!
謝謝!我將如何去檢查控制器函數或視圖中用戶的角色是什麼? – Sarah 2015-04-04 01:03:12
將用戶傳遞給beforeRender()中的視圖,如$ this-> set('userData',$ this-> Auth-> user()); – 2015-04-04 12:00:10