CakeDC 用户插件:更改密码后重定向错误
CakeDC users plugin: redirect error after changePassword
我的应用程序是用 CakePHP 开发的 3.x。
我使用 CakedDC Users 插件,它工作正常,除非用户想要更改他的密码,然后单击提交。
假设我们有一个配置文件 ID = 52606b3f-c72d-4485-9c76-3b0f8
编辑页面有一个 url 像这样:
localhost/my_app/profile/52606b3f-c72d-4485-9c76-3b0f8
changePassword 页面有一个 url 像这样:
localhost/my_app/users/users/change-password/52606b3f-c72d-4485-9c76-3b0f8
当我点击提交时,它重定向到个人资料页面,但 ID 丢失了:
localhost/my_app/profile
我收到此错误消息:
Record not found in table "users" with primary key [NULL]
我认为原因是ID没有通过。而且我找不到修复它的位置和方法。
有什么帮助吗?
不传id时,id取自登录用户。你可以看看src/Controller/Traits/ProfileTrait.php。你能调试 $this->Auth->user('id')?
此外,您可以在更改密码后自定义重定向 url。 Configure::write('Users.Profile.route', [{url}]), 参见 src/Controller/Traits/PasswordManagementTrait.php ln44.
我不记得我的初始代码,但几个月后,我找到了解决方案。
在 src/Controller/Traits/ProfileTrait.php 中,设置 $redirect = Configure::read('Users.Profile.route');
public function changePassword()
{
$user = $this->getUsersTable()->newEntity();
$id = $this->Auth->user('id');
if (!empty($id)) {
$user->id = $this->Auth->user('id');
$validatePassword = true;
//@todo add to the documentation: list of routes used
$redirect = Configure::read('Users.Profile.route');
} else {
$user->id = $this->request->session()->read(Configure::read('Users.Key.Session.resetPasswordUserId'));
$validatePassword = false;
if (!$user->id) {
$this->Flash->error(__d('CakeDC/Users', 'User was not found'));
$this->redirect($this->Auth->config('loginAction'));
return;
}
//@todo add to the documentation: list of routes used
$redirect = $this->Auth->config('loginAction');
}
$this->set('validatePassword', $validatePassword);
if ($this->request->is('post')) {
try {
$validator = $this->getUsersTable()->validationPasswordConfirm(new Validator());
if (!empty($id)) {
$validator = $this->getUsersTable()->validationCurrentPassword($validator);
}
$user = $this->getUsersTable()->patchEntity($user, $this->request->data(), ['validate' => $validator]);
if ($user->errors()) {
$this->Flash->error(__d('CakeDC/Users', 'Password could not be changed'));
} else {
$user = $this->getUsersTable()->changePassword($user);
if ($user) {
$this->Flash->success(__d('CakeDC/Users', 'Password has been changed successfully'));
return $this->redirect($redirect);
} else {
$this->Flash->error(__d('CakeDC/Users', 'Password could not be changed'));
}
}
} catch (UserNotFoundException $exception) {
$this->Flash->error(__d('CakeDC/Users', 'User was not found'));
} catch (WrongPasswordException $wpe) {
$this->Flash->error(__d('CakeDC/Users', '{0}', $wpe->getMessage()));
} catch (Exception $exception) {
$this->Flash->error(__d('CakeDC/Users', 'Password could not be changed'));
}
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}
我的应用程序是用 CakePHP 开发的 3.x。
我使用 CakedDC Users 插件,它工作正常,除非用户想要更改他的密码,然后单击提交。
假设我们有一个配置文件 ID = 52606b3f-c72d-4485-9c76-3b0f8
编辑页面有一个 url 像这样:
localhost/my_app/profile/52606b3f-c72d-4485-9c76-3b0f8
changePassword 页面有一个 url 像这样:
localhost/my_app/users/users/change-password/52606b3f-c72d-4485-9c76-3b0f8
当我点击提交时,它重定向到个人资料页面,但 ID 丢失了:
localhost/my_app/profile
我收到此错误消息:
Record not found in table "users" with primary key [NULL]
我认为原因是ID没有通过。而且我找不到修复它的位置和方法。
有什么帮助吗?
不传id时,id取自登录用户。你可以看看src/Controller/Traits/ProfileTrait.php。你能调试 $this->Auth->user('id')?
此外,您可以在更改密码后自定义重定向 url。 Configure::write('Users.Profile.route', [{url}]), 参见 src/Controller/Traits/PasswordManagementTrait.php ln44.
我不记得我的初始代码,但几个月后,我找到了解决方案。
在 src/Controller/Traits/ProfileTrait.php 中,设置 $redirect = Configure::read('Users.Profile.route');
public function changePassword()
{
$user = $this->getUsersTable()->newEntity();
$id = $this->Auth->user('id');
if (!empty($id)) {
$user->id = $this->Auth->user('id');
$validatePassword = true;
//@todo add to the documentation: list of routes used
$redirect = Configure::read('Users.Profile.route');
} else {
$user->id = $this->request->session()->read(Configure::read('Users.Key.Session.resetPasswordUserId'));
$validatePassword = false;
if (!$user->id) {
$this->Flash->error(__d('CakeDC/Users', 'User was not found'));
$this->redirect($this->Auth->config('loginAction'));
return;
}
//@todo add to the documentation: list of routes used
$redirect = $this->Auth->config('loginAction');
}
$this->set('validatePassword', $validatePassword);
if ($this->request->is('post')) {
try {
$validator = $this->getUsersTable()->validationPasswordConfirm(new Validator());
if (!empty($id)) {
$validator = $this->getUsersTable()->validationCurrentPassword($validator);
}
$user = $this->getUsersTable()->patchEntity($user, $this->request->data(), ['validate' => $validator]);
if ($user->errors()) {
$this->Flash->error(__d('CakeDC/Users', 'Password could not be changed'));
} else {
$user = $this->getUsersTable()->changePassword($user);
if ($user) {
$this->Flash->success(__d('CakeDC/Users', 'Password has been changed successfully'));
return $this->redirect($redirect);
} else {
$this->Flash->error(__d('CakeDC/Users', 'Password could not be changed'));
}
}
} catch (UserNotFoundException $exception) {
$this->Flash->error(__d('CakeDC/Users', 'User was not found'));
} catch (WrongPasswordException $wpe) {
$this->Flash->error(__d('CakeDC/Users', '{0}', $wpe->getMessage()));
} catch (Exception $exception) {
$this->Flash->error(__d('CakeDC/Users', 'Password could not be changed'));
}
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}