Skip to content

Commit

Permalink
[FEATURE] Laravel 11 update (#575)
Browse files Browse the repository at this point in the history
* Bump dependencies for Laravel 11

* restricted doctrine/orm version to v2

* restricted phpunit/phpunit version to v9

* implemented "getAuthPasswordName()" to Authenticatable trait

* implemented "rehashPasswordIfRequired()" method to DoctrineUserProvider

* fixed types for phpstan

* added assert for lumen
  • Loading branch information
77web authored May 13, 2024
1 parent bfd7f8a commit 71eb6ed
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 12 deletions.
24 changes: 12 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
"doctrine/dbal": "^3.2",
"doctrine/orm": "^2.14",
"doctrine/persistence": "^3",
"illuminate/auth": "^9.0|^10.0",
"illuminate/console": "^9.0|^10.0",
"illuminate/container": "^9.0|^10.0",
"illuminate/contracts": "^9.0|^10.0",
"illuminate/pagination": "^9.0|^10.0",
"illuminate/routing": "^9.0|^10.0",
"illuminate/support": "^9.0|^10.0",
"illuminate/validation": "^9.0|^10.0",
"illuminate/view": "^9.0|^10.0",
"illuminate/auth": "^9.0|^10.0|^11.0",
"illuminate/console": "^9.0|^10.0|^11.0",
"illuminate/container": "^9.0|^10.0|^11.0",
"illuminate/contracts": "^9.0|^10.0|^11.0",
"illuminate/pagination": "^9.0|^10.0|^11.0",
"illuminate/routing": "^9.0|^10.0|^11.0",
"illuminate/support": "^9.0|^10.0|^11.0",
"illuminate/validation": "^9.0|^10.0|^11.0",
"illuminate/view": "^9.0|^10.0|^11.0",
"symfony/cache": "^6.0|^7.0",
"symfony/serializer": "^5.0|^6.0|^7.0",
"symfony/yaml": "^5.0|^6.0|^7.0"
Expand All @@ -40,9 +40,9 @@
"require-dev": {
"phpunit/phpunit": "^9.3",
"mockery/mockery": "^1.3.1",
"illuminate/log": "^9.0|^10.0",
"illuminate/notifications": "^9.0|^10.0",
"illuminate/queue": "^9.0|^10.0",
"illuminate/log": "^9.0|^10.0|^11.0",
"illuminate/notifications": "^9.0|^10.0|^11.0",
"illuminate/queue": "^9.0|^10.0|^11.0",
"phpstan/phpstan": "^1.9",
"phpstan/phpstan-deprecation-rules": "^1.1"
},
Expand Down
5 changes: 5 additions & 0 deletions src/Auth/Authenticatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,9 @@ public function getRememberTokenName()
{
return 'rememberToken';
}

public function getAuthPasswordName()
{
return 'password';
}
}
16 changes: 16 additions & 0 deletions src/Auth/DoctrineUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,20 @@ public function getModel()
{
return $this->entity;
}

/**
* @param array{password: string} $credentials
* @return void
*/
public function rehashPasswordIfRequired(Authenticatable $user, array $credentials, bool $force = false)
{
if (! $this->hasher->needsRehash($user->getAuthPassword()) && ! $force) {
return;
}
assert(method_exists($user, 'setPassword'));

$user->setPassword($this->hasher->make($credentials['password']));
$this->em->persist($user);
$this->em->flush();
}
}
1 change: 1 addition & 0 deletions src/DoctrineServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ protected function ensureValidatorIsUsable()
if (!$this->isLumen()) {
return;
}
assert(property_exists($this->app, 'availableBindings'));

if ($this->shouldRegisterDoctrinePresenceValidator()) {
// due to weirdness the default presence verifier overrides one set by a service provider
Expand Down
37 changes: 37 additions & 0 deletions tests/Auth/DoctrineUserProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,43 @@ public function test_can_validate_credentials()
));
}

public function test_rehash_password_if_required_rehash()
{
$user = new AuthenticableMock;

$this->hasher->shouldReceive('needsRehash')->once()->andReturn(true);
$this->hasher->shouldReceive('make')->once()->andReturn('hashedPassword');
$this->em->shouldReceive('persist')->once();
$this->em->shouldReceive('flush')->once();

$this->provider->rehashPasswordIfRequired($user, ['password' => 'rawPassword'], false);
$this->assertEquals('hashedPassword', $user->getPassword());
}

public function test_rehash_password_if_required_rehash_force()
{
$user = new AuthenticableMock;

$this->hasher->shouldReceive('needsRehash')->once()->andReturn(false);
$this->hasher->shouldReceive('make')->once()->andReturn('hashedPassword');
$this->em->shouldReceive('persist')->once();
$this->em->shouldReceive('flush')->once();

$this->provider->rehashPasswordIfRequired($user, ['password' => 'rawPassword'], true);
$this->assertEquals('hashedPassword', $user->getPassword());
}

public function test_rehash_password_if_required_rehash_norehash_needed()
{
$user = new AuthenticableMock;
$user->setPassword('originalPassword');

$this->hasher->shouldReceive('needsRehash')->once()->andReturn(false);

$this->provider->rehashPasswordIfRequired($user, ['password' => 'rawPassword'], false);
$this->assertEquals('originalPassword', $user->getPassword());
}

protected function mockGetRepository($class = AuthenticableMock::class)
{
$this->em->shouldReceive('getRepository')
Expand Down

0 comments on commit 71eb6ed

Please sign in to comment.