/** * Initializes the security context for the given request. * * @return void * @throws Exception */ public function initialize() { if ($this->initialized === true) { return; } if ($this->canBeInitialized() === false) { throw new Exception('The security Context cannot be initialized yet. Please check if it can be initialized with $securityContext->canBeInitialized() before trying to do so.', 1358513802); } if ($this->csrfProtectionStrategy !== self::CSRF_ONE_PER_SESSION) { $this->csrfProtectionTokens = array(); } $this->tokens = $this->mergeTokens($this->authenticationManager->getTokens(), $this->tokens); $this->separateActiveAndInactiveTokens(); $this->updateTokens($this->activeTokens); $this->initialized = true; }
/** * Sets a new password for the given user * * This method will iterate over all accounts owned by the given user and, if the account uses a UsernamePasswordToken, * sets a new password accordingly. * * @param User $user The user to set the password for * @param string $password A new password * @return void * @api */ 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); } } }