Fixed logging in from multiple computers

This commit is contained in:
Marcin Kurczewski 2014-09-30 10:35:55 +02:00
parent 6c76f016e7
commit 3268618f26
4 changed files with 44 additions and 11 deletions

1
TODO
View file

@ -107,7 +107,6 @@ refactors:
- reduce dependencies - reduce dependencies
miscellaneous: miscellaneous:
- use 1 token for logins, so that session isn't killed
- endless pager should include information about page number - endless pager should include information about page number
- add customizable favicon - add customizable favicon
- add customizable logo - add customizable logo

View file

@ -16,6 +16,18 @@ class TokenDao extends AbstractDao
return $this->findOneBy('name', $tokenName); 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) public function deleteByName($tokenName)
{ {
return $this->deleteBy('name', $tokenName); return $this->deleteBy('name', $tokenName);

View file

@ -47,13 +47,18 @@ class TokenService
public function createAndSaveToken($additionalData, $tokenPurpose) public function createAndSaveToken($additionalData, $tokenPurpose)
{ {
$transactionFunc = function() use ($additionalData, $tokenPurpose) $transactionFunc = function() use ($additionalData, $tokenPurpose)
{
$token = $this->tokenDao->findByAdditionalDataAndPurpose($additionalData, $tokenPurpose);
if (!$token)
{ {
$token = new \Szurubooru\Entities\Token(); $token = new \Szurubooru\Entities\Token();
$token->setName(sha1(date('r') . uniqid() . microtime(true))); $token->setName(sha1(date('r') . uniqid() . microtime(true)));
$token->setAdditionalData($additionalData); $token->setAdditionalData($additionalData);
$token->setPurpose($tokenPurpose); $token->setPurpose($tokenPurpose);
$this->invalidateByAdditionalData($additionalData);
$this->tokenDao->save($token); $this->tokenDao->save($token);
}
return $token; return $token;
}; };
return $this->transactionManager->commit($transactionFunc); return $this->transactionManager->commit($transactionFunc);

View file

@ -5,12 +5,11 @@ final class TokenDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
{ {
public function testRetrievingByValidName() public function testRetrievingByValidName()
{ {
$tokenDao = new \Szurubooru\Dao\TokenDao($this->databaseConnection);
$token = new \Szurubooru\Entities\Token(); $token = new \Szurubooru\Entities\Token();
$token->setName('test'); $token->setName('test');
$token->setPurpose(\Szurubooru\Entities\Token::PURPOSE_LOGIN); $token->setPurpose(\Szurubooru\Entities\Token::PURPOSE_LOGIN);
$tokenDao = $this->getTokenDao();
$tokenDao->save($token); $tokenDao->save($token);
$expected = $token; $expected = $token;
$actual = $tokenDao->findByName($token->getName()); $actual = $tokenDao->findByName($token->getName());
@ -20,10 +19,28 @@ final class TokenDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
public function testRetrievingByInvalidName() public function testRetrievingByInvalidName()
{ {
$tokenDao = new \Szurubooru\Dao\TokenDao($this->databaseConnection); $tokenDao = $this->getTokenDao();
$actual = $tokenDao->findByName('rubbish'); $actual = $tokenDao->findByName('rubbish');
$this->assertNull($actual); $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);
}
} }