From 19b9a29c125151bc575fcdc42d2e21e9a12faee8 Mon Sep 17 00:00:00 2001 From: Barry O'Donovan Date: Thu, 5 Mar 2020 10:35:10 +0000 Subject: [PATCH 1/3] [FEATURE] Add support for Laravel 7.x All tests pass and `DoctrineTokenRepository` updated with tests. Not tested on an application yet but changes are minor and should be fine. Would be useful if someone else could test as I have no production apps on Laravel 7 just yet. --- README.md | 9 ++-- composer.json | 26 +++++----- .../Passwords/DoctrineTokenRepository.php | 48 ++++++++++++++++++- .../Passwords/DoctrineTokenRepositoryTest.php | 41 ++++++++++++++++ 4 files changed, 106 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 134b69a0..cca410ab 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ ```php $scientist = new Scientist( - 'Albert', + 'Albert', 'Einstein' ); @@ -36,7 +36,7 @@ EntityManager::flush(); * Password reminders implementation * Doctrine console commands * DoctrineExtensions supported -* Timestamps, Softdeletes and TablePrefix listeners +* Timestamps, Softdeletes and TablePrefix listeners ## Documentation @@ -51,15 +51,16 @@ Version | Supported Laravel Versions 1.2.x | 5.2.x, 5.3.x 1.3.x | 5.4.x ~1.4.0 | 5.5.x -~1.4.3 | 5.6.x +~1.4.3 | 5.6.x ~1.4.8 | 5.7.x ~1.4.10 | 5.8.x ~1.5 | 6.x +~1.6 | 7.x Require this package ```bash -composer require "laravel-doctrine/orm:1.5.*" +composer require "laravel-doctrine/orm:1.6.*" ``` Because of the auto package discovery feature Laravel 5.5 has, the ServiceProvider and Facades are automatically registered. diff --git a/composer.json b/composer.json index 9521598f..421ad1ea 100644 --- a/composer.json +++ b/composer.json @@ -16,18 +16,18 @@ } ], "require": { - "php": "^7.2", + "php": "^7.2.5", "doctrine/orm": "^2.6|^2.7", "doctrine/persistence": "^1.3", - "illuminate/auth": "^6.0", - "illuminate/console": "^6.0", - "illuminate/container": "^6.0", - "illuminate/contracts": "^6.0", - "illuminate/pagination": "^6.0", - "illuminate/routing": "^6.0", - "illuminate/support": "^6.0", - "illuminate/validation": "^6.0", - "illuminate/view": "^6.0", + "illuminate/auth": "^7.0", + "illuminate/console": "^7.0", + "illuminate/container": "^7.0", + "illuminate/contracts": "^7.0", + "illuminate/pagination": "^7.0", + "illuminate/routing": "^7.0", + "illuminate/support": "^7.0", + "illuminate/validation": "^7.0", + "illuminate/view": "^7.0", "symfony/serializer": "^2.7|^3.0|^4.0|^5.0" }, "require-dev": { @@ -35,9 +35,9 @@ "mockery/mockery": "^1.0", "barryvdh/laravel-debugbar": "~3.0", "itsgoingd/clockwork": "~1.9", - "illuminate/log": "^6.0", - "illuminate/notifications": "^6.0", - "illuminate/queue": "^6.0" + "illuminate/log": "^7.0", + "illuminate/notifications": "^7.0", + "illuminate/queue": "^7.0" }, "autoload": { "psr-4": { diff --git a/src/Auth/Passwords/DoctrineTokenRepository.php b/src/Auth/Passwords/DoctrineTokenRepository.php index 940cb91b..f2ad51cf 100644 --- a/src/Auth/Passwords/DoctrineTokenRepository.php +++ b/src/Auth/Passwords/DoctrineTokenRepository.php @@ -39,6 +39,13 @@ class DoctrineTokenRepository implements TokenRepositoryInterface */ protected $expires; + /** + * Minimum number of seconds before re-redefining the token. + * + * @var int + */ + protected $throttle; + /** * Create a new token repository instance. * @@ -47,12 +54,13 @@ class DoctrineTokenRepository implements TokenRepositoryInterface * @param string $hashKey * @param int $expires */ - public function __construct(Connection $connection, $table, $hashKey, $expires = 60) + public function __construct(Connection $connection, $table, $hashKey, $expires = 60, $throttle = 60) { $this->table = $table; $this->hashKey = $hashKey; $this->expires = $expires * 60; $this->connection = $connection; + $this->throttle = $throttle; } /** @@ -140,6 +148,44 @@ protected function tokenExpired($token) return $expiresAt->isPast(); } + + /** + * Determine if the given user recently created a password reset token. + * + * @param CanResetPassword $user + * @return bool + */ + public function recentlyCreatedToken(CanResetPassword $user) + { + $record = $this->getTable() + ->select('*') + ->from($this->table) + ->where('email = :email') + ->setParameter('email', $user->getEmailForPasswordReset()) + ->execute()->fetch(); + + return $record && $this->tokenRecentlyCreated($record['created_at']); + } + + /** + * Determine if the token was recently created. + * + * @param string $createdAt + * @return bool + */ + protected function tokenRecentlyCreated($createdAt) + { + if ($this->throttle <= 0) { + return false; + } + + return Carbon::parse($createdAt)->addSeconds( + $this->throttle + )->isFuture(); + } + + + /** * Delete a token record by token. * diff --git a/tests/Auth/Passwords/DoctrineTokenRepositoryTest.php b/tests/Auth/Passwords/DoctrineTokenRepositoryTest.php index 39b0161d..d433922d 100644 --- a/tests/Auth/Passwords/DoctrineTokenRepositoryTest.php +++ b/tests/Auth/Passwords/DoctrineTokenRepositoryTest.php @@ -153,6 +153,47 @@ public function test_can_check_if_exists() $this->assertTrue($this->repository->exists(new UserMock, 'token')); } + public function test_can_check_if_recently_created_token() + { + $this->connection->shouldReceive('createQueryBuilder') + ->once() + ->andReturn($this->builder); + + $this->builder->shouldReceive('select') + ->once() + ->with('*') + ->andReturnSelf(); + + $this->builder->shouldReceive('from') + ->once() + ->with('password_resets') + ->andReturnSelf(); + + $this->builder->shouldReceive('where') + ->once() + ->with('email = :email') + ->andReturnSelf(); + + $this->builder->shouldReceive('setParameter') + ->once() + ->with('email', 'user@mockery.mock') + ->andReturnSelf(); + + $this->builder->shouldReceive('execute') + ->once() + ->andReturnSelf(); + + $this->builder->shouldReceive('fetch') + ->once() + ->andReturn([ + 'email' => 'user@mockery.mock', + 'token' => 'token', + 'created_at' => Carbon::now() + ]); + + $this->assertTrue($this->repository->recentlyCreatedToken(new UserMock)); + } + public function test_can_delete() { $this->connection->shouldReceive('createQueryBuilder') From bb720ba6426d34fc89eb2e77f5065d4f2fd249f3 Mon Sep 17 00:00:00 2001 From: Barry O'Donovan Date: Thu, 5 Mar 2020 10:40:05 +0000 Subject: [PATCH 2/3] [FIX] Fix PR re Laravel 7 for style guide --- src/Auth/Passwords/DoctrineTokenRepository.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Auth/Passwords/DoctrineTokenRepository.php b/src/Auth/Passwords/DoctrineTokenRepository.php index f2ad51cf..49d92fc4 100644 --- a/src/Auth/Passwords/DoctrineTokenRepository.php +++ b/src/Auth/Passwords/DoctrineTokenRepository.php @@ -152,7 +152,7 @@ protected function tokenExpired($token) /** * Determine if the given user recently created a password reset token. * - * @param CanResetPassword $user + * @param CanResetPassword $user * @return bool */ public function recentlyCreatedToken(CanResetPassword $user) @@ -170,7 +170,7 @@ public function recentlyCreatedToken(CanResetPassword $user) /** * Determine if the token was recently created. * - * @param string $createdAt + * @param string $createdAt * @return bool */ protected function tokenRecentlyCreated($createdAt) @@ -184,8 +184,6 @@ protected function tokenRecentlyCreated($createdAt) )->isFuture(); } - - /** * Delete a token record by token. * From 6cb97511ce27a525ebd2701622fd23947a9f3c86 Mon Sep 17 00:00:00 2001 From: Barry O'Donovan Date: Thu, 5 Mar 2020 10:41:05 +0000 Subject: [PATCH 3/3] [FIX] Fix PR re Laravel 7 for style guide (take 2) --- src/Auth/Passwords/DoctrineTokenRepository.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Auth/Passwords/DoctrineTokenRepository.php b/src/Auth/Passwords/DoctrineTokenRepository.php index 49d92fc4..a7d1225c 100644 --- a/src/Auth/Passwords/DoctrineTokenRepository.php +++ b/src/Auth/Passwords/DoctrineTokenRepository.php @@ -148,7 +148,6 @@ protected function tokenExpired($token) return $expiresAt->isPast(); } - /** * Determine if the given user recently created a password reset token. *