【发布时间】:2014-04-05 16:27:35
【问题描述】:
我已经做了很多次了,这个问题从来没有发生过。
$this->Auth->user() 返回一个仅包含 username 和 password 字段的数组
$this->Auth->user('id') 返回 null。
有人吗?
编辑
AppController 中的 AuthComponent 定义:
public $components = array(
'Session',
'Cookie',
'Auth' => array(
'authorize' => 'Controller',
'loginError' => 'Invalid account specified',
'authError' => 'No Permission',
'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
),
);
编辑 2
用户模型:
<?php
class User extends AppModel {
public $validate = array(
'_password' => array(
'equaltofield' => array(
'rule' => array('equaltofield', 'password'),
'message' => 'Require the same value to password.',
)
)
);
public function beforeValidate ($options = array()) {
$this->data[$this->alias]['password'] = AuthComponent :: password($this->data[$this->alias]['password']);
$this->data[$this->alias]['_password'] = AuthComponent :: password($this->data[$this->alias]['_password']);
}
function equaltofield($check,$otherfield) {
$fname = '';
foreach ($check as $key => $value){
$fname = $key;
break;
}
return $this->data[$this->name][$otherfield] === $this->data[$this->name][$fname];
}
}
?>
UsersController 登录/注销功能:
public function login () {
if($this->request->is('post')) {
if($this->Auth->login($this->request->data)) {
if($this->Auth->user('role' == 'administrator')) {
return $this->redirect(array('controller' => 'brands', 'action' => 'index'));
}
else {
return $this->redirect(array('controller' => 'visits', 'action' => 'index'));
}
}
else {
$this->Session->setFlash('Login incorrect');
}
}
}
public function logout () {
$this->Auth->logout();
return $this->redirect(array('action' => 'login'));
}
【问题讨论】:
-
你是如何定义你的 Auth Components 设置的?
-
我用 Auth 组件设置更新了我的问题。
-
Auth 设置很好...这可能是一个缓存问题...清空您的缓存文件夹...如果不起作用,请使用 Users.php、UsersController 等大量信息更新您的问题。 php.......
-
我清空了缓存,没有任何变化...用户已登录,授权工作,但我无法检索当前用户的任何数据,除了用户名/密码。我不知道用户控制器和模型代码有什么好处,但是在那里,我也用它更新了我的问题。
标签: authentication cakephp-2.4