Skip to content

Commit

Permalink
Verify existing password before changing password.
Browse files Browse the repository at this point in the history
  • Loading branch information
demiankatz committed Aug 28, 2024
1 parent e5e4c1f commit 0967647
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 23 deletions.
18 changes: 18 additions & 0 deletions module/GeebyDeeby/src/GeebyDeeby/Controller/AbstractBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,24 @@ protected function forceLogin($extras = [], $forward = true)
: $this->redirect()->toRoute('login');
}

/**
* Perform authentication
*
* @param string $username Username
* @param string $password Password
*
* @return \GeebyDeeby\Authentication\Adapter
* @throws \Exception
*/
protected function getAuthenticationAdapter($username, $password)
{
return new \GeebyDeeby\Authentication\Adapter(
$this->getDbTable('user'),
$username,
$password
);
}

/**
* Format an RDF response.
*
Expand Down
12 changes: 6 additions & 6 deletions module/GeebyDeeby/src/GeebyDeeby/Controller/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ public function loginAction()
{
$view = $this->createViewModel();
if ($this->getRequest()->isPost()) {
$adapter = new \GeebyDeeby\Authentication\Adapter(
$this->getDbTable('user'),
$this->params()->fromPost('user'),
$this->params()->fromPost('pass')
);
try {
$result = $this->getAuth()->authenticate($adapter);
$result = $this->getAuth()->authenticate(
$this->getAuthenticationAdapter(
$this->params()->fromPost('user'),
$this->params()->fromPost('pass')
)
);
} catch (\GeebyDeeby\Authentication\UnapprovedUserException $e) {
$view->msg = 'Your account has not been approved yet.';
return $view;
Expand Down
43 changes: 28 additions & 15 deletions module/GeebyDeeby/src/GeebyDeeby/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public function editAction()
if (null !== $this->params()->fromPost('submit')) {
$view->fullname = $this->params()->fromPost('Fullname');
$view->address = $this->params()->fromPost('Address');
$password = $this->params()->fromPost('Password');
$password1 = $this->params()->fromPost('Password1');
$password2 = $this->params()->fromPost('Password2');
if ($view->fullname == '') {
Expand All @@ -156,22 +157,34 @@ public function editAction()
) {
$view->error = 'Your passwords did not match. Please try again.';
} else {
$table = $this->getDbTable('user');
$update = [
'Name' => $view->fullname, 'Address' => $view->address,
];
if (!empty($password1)) {
$hasher = new PasswordHasher();
$update['Password_Hash'] = $hasher->create($password1);
try {
$passwordCheck = $this->getAuthenticationAdapter(
$view->user['Username'],
$password
)->authenticate();
} catch (\Exception $e) {
$passwordCheck = null;
}
if (!empty($password1) && !($passwordCheck?->isValid())) {
$view->error = 'The existing password you provided is incorrect.';
} else {
$table = $this->getDbTable('user');
$update = [
'Name' => $view->fullname, 'Address' => $view->address,
];
if (!empty($password1)) {
$hasher = new PasswordHasher();
$update['Password_Hash'] = $hasher->create($password1);
}
$table->update(
$update,
['User_ID' => $view->user['User_ID']]
);
return $this->redirect()->toRoute(
'user',
['id' => $view->user['User_ID']]
);
}
$table->update(
$update,
['User_ID' => $view->user['User_ID']]
);
return $this->redirect()->toRoute(
'user',
['id' => $view->user['User_ID']]
);
}
} else {
$view->fullname = $view->user['Name'];
Expand Down
6 changes: 4 additions & 2 deletions module/GeebyDeeby/view/geeby-deeby/user/edit.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ leave the password fields blank.</p>
<input id="edit_fullname" type="text" name="Fullname" value="<?=$this->escapeHtml($fullname ?? '')?>" size="25" maxlength="50" /></td></tr>
<th scope="row"><label for="edit_email">E-Mail Address (optional):</label></th><td>
<input id="edit_email" type="email" name="Address" value="<?=$this->escapeHtml($address ?? '')?>" size="25" maxlength="50" /></td></tr>
<tr><th scope="row"><label for="edit_password1">Password:</label></th><td>
<tr><th scope="row"><label for="edit_password1">Existing Password:</label></th><td>
<input id="edit_password1" type="password" name="Password" size="25" maxlength="25" /></td></tr>
<tr><th scope="row"><label for="edit_password1">New Password:</label></th><td>
<input id="edit_password1" type="password" name="Password1" size="25" maxlength="25" /></td></tr>
<tr><th scope="row"><label for="edit_password2">Retype Password:</label></th><td>
<tr><th scope="row"><label for="edit_password2">Retype New Password:</label></th><td>
<input id="edit_password2" type="password" name="Password2" size="25" maxlength="25" /></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="Update Account" /></td></tr>
</table>
Expand Down

0 comments on commit 0967647

Please sign in to comment.