我正在使用 CakePHP 2.0 的集成 Auth 组件。
我有以下表格:
我的模型关系如下:
User belongsTo Group
User hasMany Profiles
登录该站点时,我注意到身份验证会话仅包含用户表信息,但我还需要登录用户的组和配置文件表的信息。
有什么方法可以使用 Auth 组件来做到这一点吗?
没有办法做到这一点AuthComponent
因为它处理会话密钥的方式。但是,您可以自己将其保存到会话中。
执行此操作的唯一方法是在用户登录时添加到会话中:
function login() {
if ($this->Auth->login($this->data)) {
$this->User->id = $this->Auth->user('id');
$this->User->contain(array('Profile', 'Group'));
$this->Session->write('User', $this->User->read());
}
}
然后在你的beforeFilter()
在你的AppController
,保存一个 var 供控制器访问:
function beforeFilter() {
$this->activeUser = $this->Session->read('User');
}
// and allow the views to have access to user data
function beforeRender() {
$this->set('activeUser', $this->activeUser);
}
Update:从 CakePHP 2.2 开始(在这里宣布 http://bakery.cakephp.org/articles/lorenzo/2012/07/01/cakephp_2_2_and_2_1_4_released),AuthComponent 现在接受“contain”键以在会话中存储额外信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)