private function checkAllTokens(UserInterface $user, $tokenHash) { $now = new \DateTime(); $tokensToRemove = array(); $throwExpirationDateException = false; /** @var Token $token */ foreach ($user->getTokens() as $key => $token) { if ($token->getExpirationDate() < $now) { $tokensToRemove[$key] = $token; } if ($token->getHash() == $tokenHash) { if ($token->getExpirationDate() < $now) { $throwExpirationDateException = true; } } } if (!empty($tokensToRemove)) { foreach ($tokensToRemove as $key => $token) { $user->getTokens()->remove($key); $this->repositoryService->remove($token); } } if ($throwExpirationDateException) { throw new TokenExpirationDateExpiredException(); } }
public function generate(UserInterface $user, $daysToLive = 1, $storeUser = true) { $tokenHash = Rand::getString(64, $this->charList); $dateStr = sprintf('+ %d day', $daysToLive); $expirationDate = new \Datetime($dateStr); /* @todo We should consider using the Prototype Design Pattern here. */ $token = new Token(); $token->setHash($tokenHash)->setExpirationDate($expirationDate); $user->getTokens()->add($token); if ($storeUser) { $this->repositoryService->store($user); } return $tokenHash; }