diff --git a/lib/Alchemy/Phrasea/Application.php b/lib/Alchemy/Phrasea/Application.php index 03318b7ea6..884ecc10d1 100644 --- a/lib/Alchemy/Phrasea/Application.php +++ b/lib/Alchemy/Phrasea/Application.php @@ -84,6 +84,7 @@ use Alchemy\Phrasea\Core\Provider\NotificationDelivererServiceProvider; use Alchemy\Phrasea\Core\Provider\ORMServiceProvider; use Alchemy\Phrasea\Core\Provider\SearchEngineServiceProvider; use Alchemy\Phrasea\Core\Provider\TaskManagerServiceProvider; +use Alchemy\Phrasea\Core\Provider\TokensServiceProvider; use Alchemy\Phrasea\Core\Provider\UnicodeServiceProvider; use FFMpeg\FFMpegServiceProvider; use Neutron\Silex\Provider\ImagineServiceProvider; @@ -207,6 +208,7 @@ class Application extends SilexApplication $this->register(new ServiceControllerServiceProvider()); $this->register(new SwiftmailerServiceProvider()); $this->register(new TaskManagerServiceProvider()); + $this->register(new TokensServiceProvider()); $this->register(new TwigServiceProvider(), array( 'twig.options' => array( 'cache' => realpath(__DIR__ . '/../../../../../../tmp/cache_twig/'), diff --git a/lib/Alchemy/Phrasea/Application/Lightbox.php b/lib/Alchemy/Phrasea/Application/Lightbox.php index 626d4adc3c..b6acc14836 100644 --- a/lib/Alchemy/Phrasea/Application/Lightbox.php +++ b/lib/Alchemy/Phrasea/Application/Lightbox.php @@ -41,7 +41,7 @@ class Lightbox implements ControllerProviderInterface } try { - $datas = \random::helloToken($app, $request->query->get('LOG')); + $datas = $app['tokens']->helloToken($request->query->get('LOG')); } catch (\Exception_NotFound $e) { return; } @@ -482,8 +482,8 @@ class Lightbox implements ControllerProviderInterface $expires = new \DateTime('+10 days'); $url = $app['phraseanet.registry']->get('GV_ServerName') - . 'lightbox/index.php?LOG=' . \random::getUrlToken( - $app, \random::TYPE_VALIDATE + . 'lightbox/index.php?LOG=' . $app['tokens']->getUrlToken( + \random::TYPE_VALIDATE , $basket->getValidation()->getInitiator($app)->get_id() , $expires , $basket->getId() diff --git a/lib/Alchemy/Phrasea/Controller/Prod/DoDownload.php b/lib/Alchemy/Phrasea/Controller/Prod/DoDownload.php index 0cac50a205..18ccc632b6 100644 --- a/lib/Alchemy/Phrasea/Controller/Prod/DoDownload.php +++ b/lib/Alchemy/Phrasea/Controller/Prod/DoDownload.php @@ -92,7 +92,7 @@ class DoDownload implements ControllerProviderInterface public function prepareDownload(Application $app, Request $request, $token) { try { - $datas = \random::helloToken($app, $token); + $datas = $app['tokens']->helloToken($token); } catch (\Exception_NotFound $e) { $app->abort(404, 'Invalid token'); } @@ -141,7 +141,7 @@ class DoDownload implements ControllerProviderInterface public function downloadDocuments(Application $app, Request $request, $token) { try { - $datas = \random::helloToken($app, $token); + $datas = $app['tokens']->helloToken($token); } catch (\Exception_NotFound $e) { $app->abort(404, 'Invalid token'); } @@ -201,7 +201,7 @@ class DoDownload implements ControllerProviderInterface public function downloadExecute(Application $app, Request $request, $token) { try { - $datas = \random::helloToken($app, $token); + $datas = $app['tokens']->helloToken($token); } catch (\Exception_NotFound $e) { return $app->json(array( 'success' => false, diff --git a/lib/Alchemy/Phrasea/Controller/Prod/Download.php b/lib/Alchemy/Phrasea/Controller/Prod/Download.php index d0691f1470..7dce3d6e42 100644 --- a/lib/Alchemy/Phrasea/Controller/Prod/Download.php +++ b/lib/Alchemy/Phrasea/Controller/Prod/Download.php @@ -78,8 +78,7 @@ class Download implements ControllerProviderInterface $list['export_name'] = sprintf('%s.zip', $download->getExportName()); - $token = \random::getUrlToken( - $app, + $token = $app['tokens']->getUrlToken( \random::TYPE_DOWNLOAD, $app['phraseanet.user']->get_id(), new \DateTime('+3 hours'), // Token lifetime diff --git a/lib/Alchemy/Phrasea/Controller/Prod/Export.php b/lib/Alchemy/Phrasea/Controller/Prod/Export.php index d30d5f4b8f..b2b3672eed 100644 --- a/lib/Alchemy/Phrasea/Controller/Prod/Export.php +++ b/lib/Alchemy/Phrasea/Controller/Prod/Export.php @@ -267,7 +267,7 @@ class Export implements ControllerProviderInterface //generate validation token $endDateObject = new \DateTime('+1 day'); - $token = \random::getUrlToken($app, \random::TYPE_EMAIL, false, $endDateObject, serialize($list)); + $token = $app['tokens']->getUrlToken(\random::TYPE_EMAIL, false, $endDateObject, serialize($list)); if (count($destMails) > 0 && $token) { //zip documents diff --git a/lib/Alchemy/Phrasea/Controller/Prod/Push.php b/lib/Alchemy/Phrasea/Controller/Prod/Push.php index 124801f124..42cec3e3ab 100644 --- a/lib/Alchemy/Phrasea/Controller/Prod/Push.php +++ b/lib/Alchemy/Phrasea/Controller/Prod/Push.php @@ -218,7 +218,7 @@ class Push implements ControllerProviderInterface $url = $app['phraseanet.registry']->get('GV_ServerName') . 'lightbox/index.php?LOG=' - . \random::getUrlToken($app, \random::TYPE_VALIDATE, $user_receiver->get_id(), null, $Basket->getId()); + . $app['tokens']->getUrlToken(\random::TYPE_VALIDATE, $user_receiver->get_id(), null, $Basket->getId()); $receipt = $request->get('recept') ? $app['phraseanet.user']->get_email() : ''; @@ -415,7 +415,7 @@ class Push implements ControllerProviderInterface $url = $app['phraseanet.registry']->get('GV_ServerName') . 'lightbox/index.php?LOG=' - . \random::getUrlToken($app, \random::TYPE_VIEW, $participant_user->get_id(), null, $Basket->getId()); + . $app['tokens']->getUrlToken(\random::TYPE_VIEW, $participant_user->get_id(), null, $Basket->getId()); $receipt = $request->get('recept') ? $app['phraseanet.user']->get_email() : ''; diff --git a/lib/Alchemy/Phrasea/Controller/Root/Account.php b/lib/Alchemy/Phrasea/Controller/Root/Account.php index 080361f4b0..d5a9309d0f 100644 --- a/lib/Alchemy/Phrasea/Controller/Root/Account.php +++ b/lib/Alchemy/Phrasea/Controller/Root/Account.php @@ -263,10 +263,10 @@ class Account implements ControllerProviderInterface { if (null !== $token = $request->request->get('token')) { try { - $datas = \random::helloToken($app, $token); + $datas = $app['tokens']->helloToken($token); $user = \User_Adapter::getInstance((int) $datas['usr_id'], $app); $user->set_email($datas['datas']); - \random::removeToken($app, $token); + $app['tokens']->removeToken($token); return $app->redirect('/account/reset-email/?update=ok'); } catch (\Exception $e) { @@ -296,7 +296,7 @@ class Account implements ControllerProviderInterface } $date = new \DateTime('1 day'); - $token = \random::getUrlToken($app, \random::TYPE_EMAIL, $app['phraseanet.user']->get_id(), $date, $app['phraseanet.user']->get_email()); + $token = $app['tokens']->getUrlToken(\random::TYPE_EMAIL, $app['phraseanet.user']->get_id(), $date, $app['phraseanet.user']->get_email()); $url = $app['phraseanet.registry']->get('GV_ServerName') . 'account/reset-email/?token=' . $token; try { diff --git a/lib/Alchemy/Phrasea/Controller/Root/Login.php b/lib/Alchemy/Phrasea/Controller/Root/Login.php index 12f69cfb0c..c085339665 100644 --- a/lib/Alchemy/Phrasea/Controller/Root/Login.php +++ b/lib/Alchemy/Phrasea/Controller/Root/Login.php @@ -251,7 +251,7 @@ class Login implements ControllerProviderInterface if ($receiver) { $expire = new \DateTime('+3 days'); - $token = \random::getUrlToken($app, \random::TYPE_PASSWORD, $user->get_id(), $expire, $user->get_email()); + $token = $app['tokens']->getUrlToken(\random::TYPE_PASSWORD, $user->get_id(), $expire, $user->get_email()); $mail = MailRequestEmailConfirmation::create($app, $receiver); $mail->setButtonUrl($app['phraseanet.registry']->get('GV_ServerName') . "register-confirm/?code=" . $token); @@ -277,7 +277,7 @@ class Login implements ControllerProviderInterface } try { - $datas = \random::helloToken($app, $code); + $datas = $app['tokens']->helloToken($code); } catch (\Exception_NotFound $e) { return $app->redirect('/login/?redirect=prod&error=token-not-found'); } @@ -292,7 +292,7 @@ class Login implements ControllerProviderInterface return $app->redirect('/login/?redirect=prod¬ice=already'); } - \random::removeToken($app, $code); + $app['tokens']->removeToken($code); try { $receiver = Receiver::fromUser($user); @@ -301,7 +301,7 @@ class Login implements ControllerProviderInterface } $user->set_mail_locked(false); - \random::removeToken($app, $code); + $app['tokens']->removeToken($code); if (count($user->ACL()->get_granted_base()) > 0) { $mail = MailSuccessEmailConfirmationRegistered::create($app, $receiver); @@ -338,7 +338,7 @@ class Login implements ControllerProviderInterface return $app->redirect($app['url_generator']->generate('login_forgot_password', array('error' => 'invalidmail'))); } - $token = \random::getUrlToken($app, \random::TYPE_PASSWORD, $user->get_id(), new \DateTime('+1 day')); + $token = $app['tokens']->getUrlToken(\random::TYPE_PASSWORD, $user->get_id(), new \DateTime('+1 day')); if ($token) { $url = $app['url_generator']->generate('login_forgot_password', array('token' => $token), true); @@ -364,12 +364,12 @@ class Login implements ControllerProviderInterface } try { - $datas = \random::helloToken($app, $token); + $datas = $app['tokens']->helloToken($token); $user = \User_Adapter::getInstance($datas['usr_id'], $app); $user->set_password($passwordConfirm); - \random::removeToken($app, $token); + $app['tokens']->removeToken($token); return $app->redirect('/login/?notice=password-update-ok'); } catch (\Exception_NotFound $e) { @@ -392,7 +392,7 @@ class Login implements ControllerProviderInterface if (null !== $token = $request->query->get('token')) { try { - \random::helloToken($app, $token); + $app['tokens']->helloToken($token); $tokenize = true; } catch (\Exception $e) { $errorMsg = 'token'; @@ -703,7 +703,7 @@ class Login implements ControllerProviderInterface $user->set_mail_locked(true); $expire = new \DateTime('+3 days'); - $token = \random::getUrlToken($app, \random::TYPE_PASSWORD, $user->get_id(), $expire, $user->get_email()); + $token = $app['tokens']->getUrlToken(\random::TYPE_PASSWORD, $user->get_id(), $expire, $user->get_email()); $mail = MailRequestEmailConfirmation::create($app, $receiver); $mail->setButtonUrl($app['phraseanet.registry']->get('GV_ServerName') . "register-confirm/?code=" . $token); @@ -913,7 +913,7 @@ class Login implements ControllerProviderInterface $basketId = $validationSession->getBasket()->getId(); try { - $token = \random::getValidationToken($this->app, $participantId, $basketId); + $token = $this->app['tokens']->getValidationToken($participantId, $basketId); } catch (\Exception_NotFound $e) { continue; } @@ -1036,7 +1036,7 @@ class Login implements ControllerProviderInterface return $app->redirect($app['url_generator']->generate('login_forgot_password', array( 'salt' => 1, - 'token' => \random::getUrlToken($app, \random::TYPE_PASSWORD, $usr_id, $date) + 'token' => $app['tokens']->getUrlToken(\random::TYPE_PASSWORD, $usr_id, $date) ))); } catch (\Exception $e) { return $app->redirect("/login/?redirect=" . ltrim($request->request->get('redirect'), '/') . "&error=unexpected"); diff --git a/lib/Alchemy/Phrasea/Core/Provider/TokensServiceProvider.php b/lib/Alchemy/Phrasea/Core/Provider/TokensServiceProvider.php new file mode 100644 index 0000000000..96aca2a7dc --- /dev/null +++ b/lib/Alchemy/Phrasea/Core/Provider/TokensServiceProvider.php @@ -0,0 +1,29 @@ +share(function($app) { + return new \random($app); + }); + } + + public function boot(Application $app) + { + } +} diff --git a/lib/Alchemy/Phrasea/Helper/User/Manage.php b/lib/Alchemy/Phrasea/Helper/User/Manage.php index 50190405ff..fb47e9a925 100644 --- a/lib/Alchemy/Phrasea/Helper/User/Manage.php +++ b/lib/Alchemy/Phrasea/Helper/User/Manage.php @@ -180,7 +180,7 @@ class Manage extends Helper if ($receiver) { $expire = new \DateTime('+3 days'); - $token = \random::getUrlToken($this->app, \random::TYPE_PASSWORD, $createdUser->get_id(), $expire, $createdUser->get_email()); + $token = $this->app['tokens']->getUrlToken(\random::TYPE_PASSWORD, $createdUser->get_id(), $expire, $createdUser->get_email()); $mail = MailRequestPasswordSetup::create($this->app, $receiver); $mail->setButtonUrl($this->app['phraseanet.registry']->get('GV_ServerName') . "register-confirm/?code=" . $token); @@ -191,7 +191,7 @@ class Manage extends Helper } if ($sendCredentials) { - $urlToken = \random::getUrlToken($this->app, \random::TYPE_PASSWORD, $createdUser->get_id()); + $urlToken = $this->app['tokens']->getUrlToken(\random::TYPE_PASSWORD, $createdUser->get_id()); if ($receiver && false !== $urlToken) { $mail = MailSuccessEmailConfirmationUnregistered::create($this->app, $receiver); diff --git a/lib/classes/Session/Authentication/Token.php b/lib/classes/Session/Authentication/Token.php index 6812969867..8edfe6f3bb 100644 --- a/lib/classes/Session/Authentication/Token.php +++ b/lib/classes/Session/Authentication/Token.php @@ -38,7 +38,7 @@ class Session_Authentication_Token implements Session_Authentication_Interface $this->token = $token; try { - $datas = random::helloToken($app, $token); + $datas = $app['tokens']->helloToken($token); $usr_id = $datas['usr_id']; $this->user = User_Adapter::getInstance($usr_id, $this->app); } catch (Exception_NotFound $e) { diff --git a/lib/classes/User/Adapter.php b/lib/classes/User/Adapter.php index 821407d185..9a5350dcf8 100644 --- a/lib/classes/User/Adapter.php +++ b/lib/classes/User/Adapter.php @@ -474,7 +474,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface $stmt->closeCursor(); } if ($token === false) { - $token = random::getUrlToken($this->app, \random::TYPE_RSS, $this->id); + $token = $this->app['tokens']->getUrlToken(\random::TYPE_RSS, $this->id); } return new system_url($this->app['phraseanet.registry']->get('GV_ServerName') . 'atom/' . $token); diff --git a/lib/classes/eventsmanager/notify/feed.php b/lib/classes/eventsmanager/notify/feed.php index f4cd50f3ea..fc1bff7c28 100644 --- a/lib/classes/eventsmanager/notify/feed.php +++ b/lib/classes/eventsmanager/notify/feed.php @@ -94,8 +94,7 @@ class eventsmanager_notify_feed extends eventsmanager_notifyAbstract if ($this->shouldSendNotificationFor($user_to_notif->get_id())) { $readyToSend = false; try { - $token = \random::getUrlToken( - $this->app, + $token = $this->app['tokens']->getUrlToken( \random::TYPE_FEED_ENTRY , $user_to_notif->get_id() , null diff --git a/lib/classes/random.php b/lib/classes/random.php index 7e647c1030..9eef566574 100644 --- a/lib/classes/random.php +++ b/lib/classes/random.php @@ -34,17 +34,23 @@ class random const TYPE_VALIDATE = 'validate'; const TYPE_RSS = 'rss'; + private $app; + + public function __construct(Application $app) + { + $this->app = $app; + } + /** - * - * @return Void + * @return Boolean */ - public static function cleanTokens(Application $app) + public function cleanTokens() { try { - $conn = connection::getPDOConnection($app); + $conn = connection::getPDOConnection($this->app); $date = new DateTime(); - $date = $app['date-formatter']->format_mysql($date); + $date = $this->app['date-formatter']->format_mysql($date); $sql = 'SELECT * FROM tokens WHERE expire_on < :date AND datas IS NOT NULL AND (type="download" OR type="email")'; @@ -103,7 +109,6 @@ class random /** * - * @param Application $app * @param string $type * @param int $usr * @param DateTime $end_date @@ -111,10 +116,10 @@ class random * * @return boolean */ - public static function getUrlToken(Application $app, $type, $usr, DateTime $end_date = null, $datas = '') + public function getUrlToken($type, $usr, DateTime $end_date = null, $datas = '') { - self::cleanTokens($app); - $conn = connection::getPDOConnection($app); + $this->cleanTokens(); + $conn = connection::getPDOConnection($this->app); $token = $test = false; switch ($type) { @@ -167,12 +172,12 @@ class random return $token; } - public static function removeToken(Application $app, $token) + public function removeToken($token) { - self::cleanTokens($app); + $this->cleanTokens(); try { - $conn = connection::getPDOConnection($app); + $conn = connection::getPDOConnection($this->app); $sql = 'DELETE FROM tokens WHERE value = :token'; $stmt = $conn->prepare($sql); $stmt->execute(array(':token' => $token)); @@ -186,10 +191,10 @@ class random return false; } - public static function updateToken(Application $app, $token, $datas) + public function updateToken($token, $datas) { try { - $conn = connection::getPDOConnection($app); + $conn = connection::getPDOConnection($this->app); $sql = 'UPDATE tokens SET datas = :datas WHERE value = :token'; @@ -206,11 +211,11 @@ class random return false; } - public static function helloToken(Application $app, $token) + public function helloToken($token) { - self::cleanTokens($app); + $this->cleanTokens(); - $conn = connection::getPDOConnection($app); + $conn = connection::getPDOConnection($this->app); $sql = 'SELECT * FROM tokens WHERE value = :token AND (expire_on > NOW() OR expire_on IS NULL)'; @@ -228,7 +233,6 @@ class random /** * Get the validation token for one user and one validation basket * - * @param Application $app * @param integer $userId * @param integer $basketId * @@ -236,9 +240,9 @@ class random * * @throws \Exception_NotFound */ - public static function getValidationToken(Application $app, $userId, $basketId) + public function getValidationToken($userId, $basketId) { - $conn = \connection::getPDOConnection($app); + $conn = \connection::getPDOConnection($this->app); $sql = ' SELECT value FROM tokens WHERE type = :type diff --git a/lib/classes/set/export.php b/lib/classes/set/export.php index 37fca4452b..02cfc8a77d 100644 --- a/lib/classes/set/export.php +++ b/lib/classes/set/export.php @@ -718,7 +718,7 @@ class set_export extends set_abstract $list['complete'] = false; - random::updateToken($app, $token, serialize($list)); + $app['tokens']->updateToken($token, serialize($list)); $toRemove = array(); @@ -751,7 +751,7 @@ class set_export extends set_abstract $list['complete'] = true; - random::updateToken($app, $token, serialize($list)); + $app['tokens']->updateToken($token, serialize($list)); $app['filesystem']->remove($toRemove); $app['filesystem']->chmod($zipFile, 0760); diff --git a/tests/Alchemy/Tests/Phrasea/Application/LightboxTest.php b/tests/Alchemy/Tests/Phrasea/Application/LightboxTest.php index 0721dba804..f593926a3c 100644 --- a/tests/Alchemy/Tests/Phrasea/Application/LightboxTest.php +++ b/tests/Alchemy/Tests/Phrasea/Application/LightboxTest.php @@ -70,7 +70,7 @@ class ApplicationLightboxTest extends \PhraseanetWebTestCaseAuthenticatedAbstrac self::$DI['app']->closeAccount(); $Basket = $this->insertOneBasket(); - $token = \random::getUrlToken(self::$DI['app'], \random::TYPE_VIEW, self::$DI['user_alt2']->get_id(), null, $Basket->getId()); + $token = self::$DI['app']['tokens']->getUrlToken(\random::TYPE_VIEW, self::$DI['user_alt2']->get_id(), null, $Basket->getId()); self::$DI['client']->request('GET', '/lightbox/?LOG='.$token); diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Prod/DoDownloadTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Prod/DoDownloadTest.php index 83e54ceedd..23eae82915 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Prod/DoDownloadTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Prod/DoDownloadTest.php @@ -166,7 +166,7 @@ class DoDownloadTest extends \PhraseanetWebTestCaseAuthenticatedAbstract $token = $this->getToken($list); // Get token - $datas = \random::helloToken(self::$DI['app'], $token); + $datas = self::$DI['app']['tokens']->helloToken($token); // Build zip \set_export::build_zip( self::$DI['app'], @@ -341,8 +341,7 @@ class DoDownloadTest extends \PhraseanetWebTestCaseAuthenticatedAbstract private function getToken($datas = array()) { - return \random::getUrlToken( - self::$DI['app'], + return self::$DI['app']['tokens']->getUrlToken( \random::TYPE_DOWNLOAD, self::$DI['user']->get_id(), new \DateTime('+10 seconds'), // Token lifetime diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Root/AccountTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Root/AccountTest.php index 83a62f38a8..1d8abb8005 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Root/AccountTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Root/AccountTest.php @@ -91,7 +91,7 @@ class AccountTest extends \PhraseanetWebTestCaseAuthenticatedAbstract */ public function testPostResetMailWithToken() { - $token = \random::getUrlToken(self::$DI['app'], \random::TYPE_EMAIL, self::$DI['user']->get_id(), null, 'new_email@email.com'); + $token = self::$DI['app']['tokens']->getUrlToken(\random::TYPE_EMAIL, self::$DI['user']->get_id(), null, 'new_email@email.com'); self::$DI['client']->request('POST', '/account/reset-email/', array('token' => $token)); $response = self::$DI['client']->getResponse(); $this->assertTrue($response->isRedirect()); @@ -100,7 +100,7 @@ class AccountTest extends \PhraseanetWebTestCaseAuthenticatedAbstract $this->assertEquals('new_email@email.com', self::$DI['user']->get_email()); self::$DI['user']->set_email('noone@example.com'); try { - \random::helloToken(self::$DI['app'], $token); + self::$DI['app']['tokens']->helloToken($token); $this->fail('TOken has not been removed'); } catch (\Exception_NotFound $e) { @@ -186,7 +186,7 @@ class AccountTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testPostResetMailEmail() { $this->mockNotificationDeliverer('Alchemy\Phrasea\Notification\Mail\MailRequestEmailUpdate'); - + $password = \random::generatePassword(); self::$DI['app']['phraseanet.user']->set_password($password); self::$DI['client']->request('POST', '/account/reset-email/', array( diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Root/LoginTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Root/LoginTest.php index 6eed8e4a95..1725a276d2 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Root/LoginTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Root/LoginTest.php @@ -96,7 +96,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract { self::$DI['app']->closeAccount(); $email = $this->generateEmail(); - $token = \random::getUrlToken(self::$DI['app'], \random::TYPE_EMAIL, 0, null, $email); + $token = self::$DI['app']['tokens']->getUrlToken(\random::TYPE_EMAIL, 0, null, $email); self::$DI['client']->request('GET', '/login/register-confirm/', array('code' => $token)); $response = self::$DI['client']->getResponse(); @@ -111,7 +111,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract { self::$DI['app']->closeAccount(); $email = $this->generateEmail(); - $token = \random::getUrlToken(self::$DI['app'], \random::TYPE_EMAIL, self::$DI['user']->get_id(), null, $email); + $token = self::$DI['app']['tokens']->getUrlToken(\random::TYPE_EMAIL, self::$DI['user']->get_id(), null, $email); self::$DI['user']->set_mail_locked(false); @@ -132,7 +132,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract self::$DI['app']->closeAccount(); $email = $this->generateEmail(); $appboxRegister = new \appbox_register(self::$DI['app']['phraseanet.appbox']); - $token = \random::getUrlToken(self::$DI['app'], \random::TYPE_EMAIL, self::$DI['user']->get_id(), null, $email); + $token = self::$DI['app']['tokens']->getUrlToken(\random::TYPE_EMAIL, self::$DI['user']->get_id(), null, $email); self::$DI['user']->set_mail_locked(true); $this->deleteRequest(); @@ -156,7 +156,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract self::$DI['app']->closeAccount(); $email = $this->generateEmail(); - $token = \random::getUrlToken(self::$DI['app'], \random::TYPE_EMAIL, $user->get_id(), null, $email); + $token = self::$DI['app']['tokens']->getUrlToken(\random::TYPE_EMAIL, $user->get_id(), null, $email); $user->set_mail_locked(true); @@ -253,7 +253,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testRenewPassword() { self::$DI['app']->closeAccount(); - $token = \random::getUrlToken(self::$DI['app'], \random::TYPE_PASSWORD, self::$DI['user']->get_id()); + $token = self::$DI['app']['tokens']->getUrlToken(\random::TYPE_PASSWORD, self::$DI['user']->get_id()); self::$DI['client']->request('POST', '/login/forgot-password/', array( 'token' => $token, diff --git a/tests/Alchemy/Tests/Phrasea/Core/Provider/TokensServiceProviderTest.php b/tests/Alchemy/Tests/Phrasea/Core/Provider/TokensServiceProviderTest.php new file mode 100644 index 0000000000..de74d7a645 --- /dev/null +++ b/tests/Alchemy/Tests/Phrasea/Core/Provider/TokensServiceProviderTest.php @@ -0,0 +1,16 @@ +random = new \random(self::$DI['app']); + } public function testCleanTokens() { $expires_on = new DateTime('-5 minutes'); $usr_id = self::$DI['user']->get_id(); - $token = random::getUrlToken(self::$DI['app'], \random::TYPE_PASSWORD, $usr_id, $expires_on, 'some nice datas'); - random::cleanTokens(self::$DI['app']); + $token = $this->random->getUrlToken(\random::TYPE_PASSWORD, $usr_id, $expires_on, 'some nice datas'); + $this->random->cleanTokens(self::$DI['app']); try { - random::helloToken(self::$DI['app'], $token); + $this->random->helloToken($token); $this->fail(); } catch (Exception_NotFound $e) { @@ -58,13 +65,13 @@ class randomTest extends PhraseanetPHPUnitAbstract public function testGetUrlToken() { $usr_id = self::$DI['user']->get_id(); - $token = random::getUrlToken(self::$DI['app'], \random::TYPE_PASSWORD, $usr_id, null, 'some nice datas'); - $datas = random::helloToken(self::$DI['app'], $token); + $token = $this->random->getUrlToken(\random::TYPE_PASSWORD, $usr_id, null, 'some nice datas'); + $datas = $this->random->helloToken($token); $this->assertEquals('some nice datas', $datas['datas']); - random::updateToken(self::$DI['app'], $token, 'some very nice datas'); - $datas = random::helloToken(self::$DI['app'], $token); + $this->random->updateToken($token, 'some very nice datas'); + $datas = $this->random->helloToken($token); $this->assertEquals('some very nice datas', $datas['datas']); - random::removeToken(self::$DI['app'], $token); + $this->random->removeToken($token); } public function testRemoveToken() @@ -80,8 +87,8 @@ class randomTest extends PhraseanetPHPUnitAbstract public function testHelloToken() { $usr_id = self::$DI['user']->get_id(); - $token = random::getUrlToken(self::$DI['app'], \random::TYPE_PASSWORD, $usr_id, null, 'some nice datas'); - $datas = random::helloToken(self::$DI['app'], $token); + $token = $this->random->getUrlToken(\random::TYPE_PASSWORD, $usr_id, null, 'some nice datas'); + $datas = $this->random->helloToken($token); $this->assertEquals('some nice datas', $datas['datas']); $this->assertNull($datas['expire_on']); $created_on = new DateTime($datas['created_on']); @@ -91,9 +98,9 @@ class randomTest extends PhraseanetPHPUnitAbstract $this->assertTrue($date >= $created_on); $this->assertEquals('password', $datas['type']); - random::removeToken(self::$DI['app'], $token); + $this->random->removeToken($token); try { - random::helloToken(self::$DI['app'], $token); + $this->random->helloToken($token); $this->fail(); } catch (Exception_NotFound $e) { @@ -101,8 +108,8 @@ class randomTest extends PhraseanetPHPUnitAbstract $expires_on = new DateTime('+5 minutes'); $usr_id = self::$DI['user']->get_id(); - $token = random::getUrlToken(self::$DI['app'], \random::TYPE_PASSWORD, $usr_id, $expires_on, 'some nice datas'); - $datas = random::helloToken(self::$DI['app'], $token); + $token = $this->random->getUrlToken(\random::TYPE_PASSWORD, $usr_id, $expires_on, 'some nice datas'); + $datas = $this->random->helloToken($token); $this->assertEquals('some nice datas', $datas['datas']); $sql_expires = new DateTime($datas['expire_on']); $this->assertTrue($sql_expires == $expires_on); @@ -113,9 +120,9 @@ class randomTest extends PhraseanetPHPUnitAbstract $this->assertTrue($date >= $created_on); $this->assertEquals('password', $datas['type']); - random::removeToken(self::$DI['app'], $token); + $this->random->removeToken($token); try { - random::helloToken(self::$DI['app'], $token); + $this->random->helloToken($token); $this->fail(); } catch (Exception_NotFound $e) { @@ -124,10 +131,10 @@ class randomTest extends PhraseanetPHPUnitAbstract $expires_on = new DateTime('-5 minutes'); $usr_id = self::$DI['user']->get_id(); - $token = random::getUrlToken(self::$DI['app'], \random::TYPE_PASSWORD, $usr_id, $expires_on, 'some nice datas'); + $token = $this->random->getUrlToken(\random::TYPE_PASSWORD, $usr_id, $expires_on, 'some nice datas'); try { - random::helloToken(self::$DI['app'], $token); + $this->random->helloToken($token); $this->fail(); } catch (Exception_NotFound $e) {