diff --git a/TODO b/TODO index 1d1fd752..785a3fb9 100644 --- a/TODO +++ b/TODO @@ -107,7 +107,6 @@ refactors: - reduce dependencies miscellaneous: - - use 1 token for logins, so that session isn't killed - endless pager should include information about page number - add customizable favicon - add customizable logo diff --git a/src/Dao/TokenDao.php b/src/Dao/TokenDao.php index 32438142..65591294 100644 --- a/src/Dao/TokenDao.php +++ b/src/Dao/TokenDao.php @@ -16,6 +16,18 @@ class TokenDao extends AbstractDao return $this->findOneBy('name', $tokenName); } + public function findByAdditionalDataAndPurpose($additionalData, $purpose) + { + $query = $this->fpdo->from($this->tableName) + ->where('additionalData', $additionalData) + ->where('purpose', $purpose); + $arrayEntities = iterator_to_array($query); + if (!$arrayEntities or !count($arrayEntities)) + return null; + $arrayEntity = array_shift($arrayEntities); + return $this->entityConverter->toEntity($arrayEntity); + } + public function deleteByName($tokenName) { return $this->deleteBy('name', $tokenName); diff --git a/src/Services/TokenService.php b/src/Services/TokenService.php index fd763db6..5bf67ff2 100644 --- a/src/Services/TokenService.php +++ b/src/Services/TokenService.php @@ -48,12 +48,17 @@ class TokenService { $transactionFunc = function() use ($additionalData, $tokenPurpose) { - $token = new \Szurubooru\Entities\Token(); - $token->setName(sha1(date('r') . uniqid() . microtime(true))); - $token->setAdditionalData($additionalData); - $token->setPurpose($tokenPurpose); - $this->invalidateByAdditionalData($additionalData); - $this->tokenDao->save($token); + $token = $this->tokenDao->findByAdditionalDataAndPurpose($additionalData, $tokenPurpose); + + if (!$token) + { + $token = new \Szurubooru\Entities\Token(); + $token->setName(sha1(date('r') . uniqid() . microtime(true))); + $token->setAdditionalData($additionalData); + $token->setPurpose($tokenPurpose); + $this->tokenDao->save($token); + } + return $token; }; return $this->transactionManager->commit($transactionFunc); diff --git a/tests/Dao/TokenDaoTest.php b/tests/Dao/TokenDaoTest.php index bb9ef6fd..f09cea57 100644 --- a/tests/Dao/TokenDaoTest.php +++ b/tests/Dao/TokenDaoTest.php @@ -5,12 +5,11 @@ final class TokenDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase { public function testRetrievingByValidName() { - $tokenDao = new \Szurubooru\Dao\TokenDao($this->databaseConnection); - $token = new \Szurubooru\Entities\Token(); $token->setName('test'); $token->setPurpose(\Szurubooru\Entities\Token::PURPOSE_LOGIN); + $tokenDao = $this->getTokenDao(); $tokenDao->save($token); $expected = $token; $actual = $tokenDao->findByName($token->getName()); @@ -20,10 +19,28 @@ final class TokenDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase public function testRetrievingByInvalidName() { - $tokenDao = new \Szurubooru\Dao\TokenDao($this->databaseConnection); - + $tokenDao = $this->getTokenDao(); $actual = $tokenDao->findByName('rubbish'); $this->assertNull($actual); } + + public function testRetrievingByAdditionalDataAndPurpose() + { + $token = new \Szurubooru\Entities\Token(); + $token->setName('test'); + $token->setPurpose(\Szurubooru\Entities\Token::PURPOSE_LOGIN); + + $tokenDao = $this->getTokenDao(); + $tokenDao->save($token); + $expected = $token; + + $this->assertEntitiesEqual($expected, $tokenDao->findByAdditionalDataAndPurpose(null, \Szurubooru\Entities\Token::PURPOSE_LOGIN)); + $this->assertNull($tokenDao->findByAdditionalDataAndPurpose(null, \Szurubooru\Entities\Token::PURPOSE_ACTIVATE)); + } + + private function getTokenDao() + { + return new \Szurubooru\Dao\TokenDao($this->databaseConnection); + } }