From e4d3da93365ff452799fbffb9a03969392161486 Mon Sep 17 00:00:00 2001 From: Jean-Yves Gaulier Date: Sat, 8 Feb 2020 22:29:52 +0100 Subject: [PATCH] rollback : can't create token in sql since the om will cry during update. Can't mix om and sql. --- .../Model/Manipulator/TokenManipulator.php | 56 +++++++------------ 1 file changed, 20 insertions(+), 36 deletions(-) diff --git a/lib/Alchemy/Phrasea/Model/Manipulator/TokenManipulator.php b/lib/Alchemy/Phrasea/Model/Manipulator/TokenManipulator.php index 6314e9e614..607cbc6cc6 100644 --- a/lib/Alchemy/Phrasea/Model/Manipulator/TokenManipulator.php +++ b/lib/Alchemy/Phrasea/Model/Manipulator/TokenManipulator.php @@ -61,45 +61,29 @@ class TokenManipulator implements ManipulatorInterface * * @return Token */ - public function create($user, $type, \DateTime $expiration = null, $data = null) + public function create(User $user = null, $type, \DateTime $expiration = null, $data = null) { - static $stmt = null; + $this->removeExpiredTokens(); - // $this->removeExpiredTokens(); - - if($stmt === null) { - $conn = $this->repository->getEntityManager()->getConnection(); - $sql = "INSERT INTO Tokens (value, user_id, type, data, created, updated, expiration)\n" - . " VALUES(:value, :user_id, :type, :data, :created, :updated, :expiration)"; - $stmt = $conn->prepare($sql); - } - - $token = null; - $now = (new \DateTime())->format('Y-m-d H:i:s'); - $stmtParms = [ - ':value' => null, - ':user_id' => $user ? $user->getId() : null, - ':type' => $type, - ':data' => $data, - ':created' => $now, - ':updated' => $now, - ':expiration' => $expiration ? $expiration->format('Y-m-d H:i:s') : null - ]; - for($try=0; $try<1024; $try++) { - $stmtParms['value'] = $this->random->generateString(32, self::LETTERS_AND_NUMBERS); - if($stmt->execute($stmtParms) === true) { - $token = new Token(); - $token->setUser($user) - ->setType($type) - ->setValue($stmtParms['value']) - ->setExpiration($expiration) - ->setData($data); - break; + $n = 0; + do { + if ($n++ > 1024) { + throw new \RuntimeException('Unable to create a token.'); } - } - if ($token === null) { - throw new \RuntimeException('Unable to create a token.'); - } + $value = $this->random->generateString(32, self::LETTERS_AND_NUMBERS); + $found = null !== $this->om->getRepository('Phraseanet:Token')->find($value); + } while ($found); + + $token = new Token(); + + $token->setUser($user) + ->setType($type) + ->setValue($value) + ->setExpiration($expiration) + ->setData($data); + + $this->om->persist($token); + $this->om->flush(); return $token; }