Skip to content

Commit

Permalink
Update Pull Request yiisoft#602
Browse files Browse the repository at this point in the history
  • Loading branch information
rossaddison committed May 5, 2024
1 parent 826a572 commit 2b23069
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 33 deletions.
12 changes: 12 additions & 0 deletions blog/resources/rbac/items.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,16 @@
'updatedAt' => 1599036348,
'createdAt' => 1599036348,
],

/**
* Purpose: Effects read-only status of Login text box
* @see Auth/Controller/ChangePasswordController 'canChangePasswordForAnyUser'
* @see views/changepassword/change $canChangePasswordForAnyUser
*/
'canChangePasswordForAnyUser' => [
'name' => 'canChangePasswordForAnyUser',
'type' => 'permission',
'updatedAt' => 1599036348,
'createdAt' => 1599036348,
],
];
3 changes: 2 additions & 1 deletion blog/resources/views/changepassword/change.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
->id('changeForm')
->open() ?>

<?= Field::text($formModel, 'login')->addInputAttributes(['value'=> $login ?? '', 'readonly'=>'readonly']) ?>
<?= $canChangePasswordForAnyUser ? Field::text($formModel, 'login')->addInputAttributes(['value'=> $login ?? ''])
: Field::text($formModel, 'login')->addInputAttributes(['value'=> $login ?? '', 'readonly'=>'readonly']); ?>
<?= Field::password($formModel, 'password') ?>
<?= Field::password($formModel, 'newPassword') ?>
<?= Field::password($formModel, 'newPasswordVerify') ?>
Expand Down
63 changes: 31 additions & 32 deletions blog/src/Auth/Controller/ChangePasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public function __construct(
private Session $session,
private Flash $flash,
private Translator $translator,
private CurrentUser $current_user,
private CurrentUser $currentUser,
private WebControllerService $webService,
private ViewRenderer $viewRenderer,
)
{
$this->current_user = $current_user;
$this->currentUser = $currentUser;
$this->session = $session;
$this->flash = new Flash($session);
$this->translator = $translator;
Expand All @@ -46,36 +46,35 @@ public function change(
): ResponseInterface {
if ($authService->isGuest()) {
return $this->redirectToMain();
}
// permit an authenticated user, ie. not a guest, only and null!== current user
if (!$authService->isGuest()) {
if ($this->current_user->can('viewInv',[])) {
// readonly the login detail on the change form
$identity_id = $this->current_user->getIdentity()->getId();
if (null!==$identity_id) {
$identity = $identityRepository->findIdentity($identity_id);
if (null!==$identity) {
// Identity and User are in a HasOne relationship so no null value
$login = $identity->getUser()?->getLogin();
if ($request->getMethod() === Method::POST
&& $formHydrator->populate($changePasswordForm, $request->getParsedBody())
&& $changePasswordForm->change()
) {
// Identity implements CookieLoginIdentityInterface: ensure the regeneration of the cookie auth key by means of $authService->logout();
// @see vendor\yiisoft\user\src\Login\Cookie\CookieLoginIdentityInterface

// Specific note: "Make sure to invalidate earlier issued keys when you implement force user logout,
// PASSWORD CHANGE and other scenarios, that require forceful access revocation for old sessions.
// The authService logout function will regenerate the auth key here => overwriting any auth key
$authService->logout();
$this->flash_message('success', $this->translator->translate('validator.password.change'));
return $this->redirectToMain();
}
return $this->viewRenderer->render('change', ['formModel' => $changePasswordForm, 'login' => $login]);
} // identity
} // identity_id
} // current user
} // auth service
}

$identity_id = $this->currentUser->getIdentity()->getId();
if (null!==$identity_id) {
$identity = $identityRepository->findIdentity($identity_id);
if (null!==$identity) {
// Identity and User are in a HasOne relationship so no null value
$login = $identity->getUser()?->getLogin();
if ($request->getMethod() === Method::POST
&& $formHydrator->populate($changePasswordForm, $request->getParsedBody())
&& $changePasswordForm->change()
) {
// Identity implements CookieLoginIdentityInterface: ensure the regeneration of the cookie auth key by means of $authService->logout();
// @see vendor\yiisoft\user\src\Login\Cookie\CookieLoginIdentityInterface
// Specific note: "Make sure to invalidate earlier issued keys when you implement force user logout,
// PASSWORD CHANGE and other scenarios, that require forceful access revocation for old sessions.
// The authService logout function will regenerate the auth key here => overwriting any auth key
$authService->logout();
$this->flash_message('success', $this->translator->translate('validator.password.change'));
return $this->redirectToMain();
}
return $this->viewRenderer->render('change',
[
'formModel' => $changePasswordForm,
'login' => $login,
'canChangePasswordForAnyUser' => $this->currentUser->can('changePasswordForAnyUser')
]);
} // identity
} // identity_id
} // reset

/**
Expand Down

0 comments on commit 2b23069

Please sign in to comment.