Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Laravel 11 update #575

Merged
merged 7 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'));
eigan marked this conversation as resolved.
Show resolved Hide resolved

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
Loading