mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-23 18:03:17 +00:00
rollback : can't create token in sql since the om will cry during update. Can't mix om and sql.
This commit is contained in:
@@ -61,45 +61,29 @@ class TokenManipulator implements ManipulatorInterface
|
|||||||
*
|
*
|
||||||
* @return Token
|
* @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();
|
$n = 0;
|
||||||
|
do {
|
||||||
if($stmt === null) {
|
if ($n++ > 1024) {
|
||||||
$conn = $this->repository->getEntityManager()->getConnection();
|
throw new \RuntimeException('Unable to create a token.');
|
||||||
$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;
|
|
||||||
}
|
}
|
||||||
}
|
$value = $this->random->generateString(32, self::LETTERS_AND_NUMBERS);
|
||||||
if ($token === null) {
|
$found = null !== $this->om->getRepository('Phraseanet:Token')->find($value);
|
||||||
throw new \RuntimeException('Unable to create a token.');
|
} 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;
|
return $token;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user