Neos\Neos\Domain\Service\UserService::setUserPassword PHP Method

setUserPassword() public method

This method will iterate over all accounts owned by the given user and, if the account uses a UsernamePasswordToken, sets a new password accordingly.
public setUserPassword ( User $user, string $password ) : void
$user Neos\Neos\Domain\Model\User The user to set the password for
$password string A new password
return void
    public function setUserPassword(User $user, $password)
    {
        $tokens = $this->authenticationManager->getTokens();
        $indexedTokens = array();
        foreach ($tokens as $token) {
            /** @var TokenInterface $token */
            $indexedTokens[$token->getAuthenticationProviderName()] = $token;
        }
        foreach ($user->getAccounts() as $account) {
            /** @var Account $account */
            $authenticationProviderName = $account->getAuthenticationProviderName();
            if (isset($indexedTokens[$authenticationProviderName]) && $indexedTokens[$authenticationProviderName] instanceof UsernamePassword) {
                $account->setCredentialsSource($this->hashService->hashPassword($password));
                $this->accountRepository->update($account);
            }
        }
    }

Usage Example

 /**
  * Update a given account, ie. the password
  *
  * @param array $password Expects an array in the format array('<password>', '<password confirmation>')
  * @Flow\Validate(argumentName="password", type="\Neos\Neos\Validation\Validator\PasswordValidator", options={ "allowEmpty"=1, "minimum"=1, "maximum"=255 })
  * @return void
  */
 public function updateAccountAction(array $password = array())
 {
     $user = $this->currentUser;
     $password = array_shift($password);
     if (strlen(trim(strval($password))) > 0) {
         $this->userService->setUserPassword($user, $password);
         $this->addFlashMessage('The password has been updated.', 'Password updated', Message::SEVERITY_OK);
     }
     $this->redirect('index');
 }
All Usage Examples Of Neos\Neos\Domain\Service\UserService::setUserPassword