我设置了忘记密码方案,用户提交电子邮件地址,获取发送给他们的临时密码,用户点击链接发送给用户的编辑功能,将密码更改为他们可以记得。我正在通过网址中的临时传递。
我调试了我能做的事情并且似乎结帐,但仍然不允许未登录的用户访问编辑方法。它似乎只授权登录用户(如果他们忘记了密码,你可以看到我的问题,因此无法登录)。
那么如果temp pass与url的id param匹配,你将如何允许访问控制器方法?
isAuthorized function:
public function isAuthorized($user = null)
{
if (isset($user['role']) && $user['role'] === 1 ) {
return true;
}
if ( $this->request->action === 'edit' ) {
$paramId = (int)$this->request->pass;
$logged_out_user = $this->Users->get($paramId);
if ( password_verify($this->request->query('pass'), $logged_out_user->password) === true ) {
return true;
} else {
return $this->redirect($this->Auth->redirectUrl());
}
}
return parent::isAuthorized($user);
}
示例链接地址:http://localhost/site/users/edit/1?pass=o1eNbs5l7GlHlqvPAmU.
答案 0 :(得分:0)
我只是想通了。我将'edit'动作添加到我的beforefilter中,这样任何人都可以访问它,无论是否登录,然后在编辑功能中我只是添加了检查,非常简单,但由于某种原因我无法想到它
内部编辑功能:
if ($this->request->session()->read('Auth.User')) {
if ( $user->id === $this->request->session()->read('Auth.User.id') || $this->request->session()->read('Auth.User.role') === 1 ) {
$this->Auth->allow();
} else {
$this->Flash->error(__('You do not have access to that page.'));
return $this->redirect(['action' => 'login']);
}
} elseif ( !empty($this->request->query('pass')) && password_verify($this->request->query('pass'), $user->password) === true ) {
$this->Auth->setUser($user->toArray());
} else {
$this->Flash->error(__('You do not have access to that page.'));
return $this->redirect(['action' => 'login']);
}
if ( !empty($this->request->query('pass')) && password_verify($this->request->query('pass'), $user->password) === true ) {
$user->old_password = $this->request->query('pass');
}
答案 1 :(得分:0)
我认为理想情况下,您可以在控制器中进行两个单独的操作,这样您只能进行实际用户编辑的编辑页面和重置密码页面才能重置密码。
然后就这样做(在您的控制器中)......
public function initialize()
{
parent::initialize();
$this->Auth->allow(['reset_password']);
}
这假设你让你的控制器重置密码操作......
public function reset_password($pass = false) {
if (password_verify($pass)) {
// show password reset form
} else {
$this->redirect(['action' => 'key_required_or_expired']);
}
}