在过去的一年里,我的身份验证组件一直运行良好。大约两个小时前,我登录了我的网站并做了一个小改动。这两个小时我下车了,自从我大约半小时前回来后,我一直无法登录我的网站。错误消息说用户名/密码组合不正确。我认为不知何故我的数据库中的值发生了变化,或者我的浏览器自动保存了一个旧密码,所以只是为了确保我用适当的 md5 散列值更新了我的数据库中的密码,然后我在浏览器中再次尝试了它。它仍然没有工作。
我清空了我的缓存(这是我经常以任何方式做的事情),因为有人建议我从这篇文章中这样做,但这也不起作用(也没有帮助那个问题的发布者)。该人在视图文件中添加了破坏数据库连接的内容的解决方案不适用于我,因为我没有更改任何视图文件。我也没有更改用户模型或应用程序控制器。在我之前的时间里,我唯一改变的是我编辑了 UsersController online() 动作。从那以后我把它改回来了。事实上,我进入了我的站点备份实用程序,并将所有控制器和模型文件恢复到了 2 天前一切正常时的最新备份。还是没有影响。
我无法登录我已注册的任何帐户。我什至取消了数据库中的一个密码并尝试使用该帐户登录,但这也不起作用。
应用控制器
//I HAVE NOT CHANGED THIS IN SEVERAL MONTHS
public $helpers = array('Form', 'Html', 'Custom', 'Time', 'Js', 'Cache');
public $components = array('CustomPage', 'Session', 'CustomUser',
'Auth' => array(
'autoRedirect' => false,
'loginAction' => array('controller' => 'users', 'action' => 'login', 'prefix' => false, 'admin' => false, 'moderate' => false),
'loginRedirect' => array('prefix' => false, 'admin' => false, 'moderate' => false, 'controller' => 'account', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'index', 'prefix' => false, 'admin' => false, 'moderate' => false),
'authError' => "You can't access that page",
'authorize' => array('Controller')
)
); // components
public function isAuthorized($user) {
return true;
}
用户控制器
// DID NOT CHANGE FOLLOWING ACTION
public function login() {
if ($this->Session->read('Auth.User')) {
$this->Session->setFlash('You are already logged in');
$this->redirect(array('controller' => 'account', 'action' => 'index'));
}
$this->layout = "simple";
$this->set('title_for_layout', 'Login');
if ($this->request->is('post')) {
$this->User->UsersOnline->deleteAll(array('UsersOnline.session' => $this->viewVars['session_session']), false);
if ($this->Auth->login()) { // check user is logged in
$this->User->id = $this->Auth->user('id');
$this->User->saveField('last_login', date(Configure::read('Site.date_format'))); // save login time
$this->redirect($this->Auth->redirect()); // redirect to default place
} else {
$this->Session->setFlash('Your username/password combination was incorrect');
}
}
} // end login
// THE FOLLOWING ACTION WAS THE ONLY THING
// THAT WAS CHANGED BUT IT IS NOW BACK TO ORIGINAL VERSION
public function online() {
$page_id = $this->viewVars['page_id'];
$link = $this->viewVars['link'];
// CONTAIN
$this->User->UsersOnline->contain(array(
'User' => array(
'fields' => array(
'User.username', 'User.online'
),
'Avatar' => array(
'fields' => array(
'Avatar.file'
)
)
)
));
if($page_id){
$this->set('users', $this->paginate($this->User->UsersOnline, array('UsersOnline.page_id' => $page_id)));
$this->set('title_for_layout', 'Fans Online');
}
else{
$this->set('title_for_layout', 'Users Online');
$this->set('users', $this->paginate($this->User->UsersOnline));
$this->layout = "default";
}
} // end online action
我的用户模型使用标准的“用户名”和“密码”列来验证用户身份。
我已将以下代码添加到我的 UserController login() 操作中,并打印了正确的结果...
$password = md5($this->request->data['User']['password']);
print_r(
$this->User->find('first',
array(
'User.username' => $this->request->data['User']['username'],
'User.password' => $password
)
)
);
同样,我已将所有控制器和模型文件从 2 天前恢复到它们的状态,所以我真的不知道是什么原因造成的。
编辑 1:现在为了安全起见,我从本周末开始将所有视图文件恢复为最新的备份版本。这并没有解决问题。
编辑2:如果我调试$this->Auth->login
,结果是空的。如果什么都没有改变,为什么这会突然变空呢?
编辑 3:我的 UsersController register() 操作正确地创建了一个新用户并自动登录该用户。
用户控制器
public function register() {
if($this->Session->read('Auth.User')) {
$this->Session->setFlash('You are already registered');
$this->redirect(array('controller' => 'account', 'action' => 'index'));
}
$this->layout = "simple";
$this->set('title_for_layout', 'Register');
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$id = $this->User->id;
$this->request->data['User'] = array_merge($this->request->data['User'], array('id' => $id));
if($this->Auth->login($this->request->data['User'])){
$this->Session->setFlash(__('Your account has successfully been created and you have logged in'));
$this->redirect(array('controller' => 'account', 'action' => 'index'));
} // end if account created and successful log in
else{
$this->Session->setFlash(__('Your account has successfully been created but you cannot log in'));
} // end else if account created but not logged in
} else {
$this->Session->setFlash(__('Your account could not be created. Please, try again.'));
} // end else if account cannot be created
} // end if method is post
} // end register