diff --git a/lib/Alchemy/Phrasea/Authentication/Authenticator.php b/lib/Alchemy/Phrasea/Authentication/Authenticator.php index 8766d46096..d43660bd80 100644 --- a/lib/Alchemy/Phrasea/Authentication/Authenticator.php +++ b/lib/Alchemy/Phrasea/Authentication/Authenticator.php @@ -149,6 +149,19 @@ class Authenticator */ public function isAuthenticated() { - return $this->session->has('usr_id'); + if (!$this->session->has('usr_id')) { + return false; + } + + if ($this->session->has('session_id')) { + if (null !== $this->em->find('Entities\Session', $this->session->get('session_id'))) { + return true; + } + } + + $this->session->invalidate(); + $this->reinitUser(); + + return false; } } diff --git a/lib/Alchemy/Phrasea/Setup/Installer.php b/lib/Alchemy/Phrasea/Setup/Installer.php index 24759b4811..b6aea89c47 100644 --- a/lib/Alchemy/Phrasea/Setup/Installer.php +++ b/lib/Alchemy/Phrasea/Setup/Installer.php @@ -113,8 +113,7 @@ class Installer private function createUser($email, $password) { $user = \User_Adapter::create($this->app, $email, $password, $email, true); - - $this->app['session']->set('usr_id', $user->get_id()); + $this->app['authentication']->openAccount($user); return $user; } diff --git a/tests/Alchemy/Tests/Phrasea/Authentication/AuthenticatorTest.php b/tests/Alchemy/Tests/Phrasea/Authentication/AuthenticatorTest.php index 682192ecf1..2383e0f735 100644 --- a/tests/Alchemy/Tests/Phrasea/Authentication/AuthenticatorTest.php +++ b/tests/Alchemy/Tests/Phrasea/Authentication/AuthenticatorTest.php @@ -5,6 +5,7 @@ namespace Alchemy\Tests\Phrasea\Authentication; use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Authentication\Authenticator; use Alchemy\Phrasea\Exception\RuntimeException; +use Entities\Session; class AuthenticatorTest extends \PhraseanetPHPUnitAbstract { @@ -33,11 +34,17 @@ class AuthenticatorTest extends \PhraseanetPHPUnitAbstract $app['browser'] = $browser = $this->getBrowserMock(); $app['session'] = $session = $this->getSessionMock(); - $app['EM'] = $em = $this->getEntityManagerMock(); + + $sessionEntity = new Session(); + $sessionEntity->setUsrId($user->get_id()); + $sessionEntity->setUserAgent(''); + $app['EM']->persist($sessionEntity); + $app['EM']->flush(); $session->set('usr_id', $user->get_id()); + $session->set('session_id', $sessionEntity->getId()); - $authenticator = new Authenticator($app, $browser, $session, $em); + $authenticator = new Authenticator($app, $browser, $session, $app['EM']); $this->assertEquals($user, $authenticator->getUser()); } @@ -69,8 +76,7 @@ class AuthenticatorTest extends \PhraseanetPHPUnitAbstract public function testOpenAccount() { $app = new Application(); - - $sessionId = 2442; + $capturedSession = null; $app['browser'] = $browser = $this->getBrowserMock(); $app['session'] = $session = $this->getSessionMock(); @@ -97,11 +103,8 @@ class AuthenticatorTest extends \PhraseanetPHPUnitAbstract $em->expects($this->at(0)) ->method('persist') ->with($this->isInstanceOf('Entities\Session')) - ->will($this->returnCallback(function ($session) use ($sessionId) { - $ref = new \ReflectionObject($session); - $prop = $ref->getProperty('id'); - $prop->setAccessible(true); - $prop->setValue($session, $sessionId); + ->will($this->returnCallback(function ($session) use (&$capturedSession) { + $capturedSession = $session; })); $em->expects($this->at(1)) ->method('flush'); @@ -110,7 +113,7 @@ class AuthenticatorTest extends \PhraseanetPHPUnitAbstract $phsession = $authenticator->openAccount($user); $this->assertInstanceOf('Entities\Session', $phsession); - $this->assertEquals($sessionId, $session->get('session_id')); + $this->assertEquals($capturedSession, $phsession); } /** @@ -237,11 +240,17 @@ class AuthenticatorTest extends \PhraseanetPHPUnitAbstract $app['browser'] = $browser = $this->getBrowserMock(); $app['session'] = $session = $this->getSessionMock(); - $app['EM'] = $em = $this->getEntityManagerMock(); + + $sessionEntity = new Session(); + $sessionEntity->setUsrId($user->get_id()); + $sessionEntity->setUserAgent(''); + $app['EM']->persist($sessionEntity); + $app['EM']->flush(); $session->set('usr_id', $user->get_id()); + $session->set('session_id', $sessionEntity->getId()); - $authenticator = new Authenticator($app, $browser, $session, $em); + $authenticator = new Authenticator($app, $browser, $session, $app['EM']); $this->assertTrue($authenticator->isAuthenticated()); }