Skip to content

Commit 71eb6ed

Browse files
authored
[FEATURE] Laravel 11 update (#575)
* 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
1 parent bfd7f8a commit 71eb6ed

File tree

5 files changed

+71
-12
lines changed

5 files changed

+71
-12
lines changed

composer.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121
"doctrine/dbal": "^3.2",
2222
"doctrine/orm": "^2.14",
2323
"doctrine/persistence": "^3",
24-
"illuminate/auth": "^9.0|^10.0",
25-
"illuminate/console": "^9.0|^10.0",
26-
"illuminate/container": "^9.0|^10.0",
27-
"illuminate/contracts": "^9.0|^10.0",
28-
"illuminate/pagination": "^9.0|^10.0",
29-
"illuminate/routing": "^9.0|^10.0",
30-
"illuminate/support": "^9.0|^10.0",
31-
"illuminate/validation": "^9.0|^10.0",
32-
"illuminate/view": "^9.0|^10.0",
24+
"illuminate/auth": "^9.0|^10.0|^11.0",
25+
"illuminate/console": "^9.0|^10.0|^11.0",
26+
"illuminate/container": "^9.0|^10.0|^11.0",
27+
"illuminate/contracts": "^9.0|^10.0|^11.0",
28+
"illuminate/pagination": "^9.0|^10.0|^11.0",
29+
"illuminate/routing": "^9.0|^10.0|^11.0",
30+
"illuminate/support": "^9.0|^10.0|^11.0",
31+
"illuminate/validation": "^9.0|^10.0|^11.0",
32+
"illuminate/view": "^9.0|^10.0|^11.0",
3333
"symfony/cache": "^6.0|^7.0",
3434
"symfony/serializer": "^5.0|^6.0|^7.0",
3535
"symfony/yaml": "^5.0|^6.0|^7.0"
@@ -40,9 +40,9 @@
4040
"require-dev": {
4141
"phpunit/phpunit": "^9.3",
4242
"mockery/mockery": "^1.3.1",
43-
"illuminate/log": "^9.0|^10.0",
44-
"illuminate/notifications": "^9.0|^10.0",
45-
"illuminate/queue": "^9.0|^10.0",
43+
"illuminate/log": "^9.0|^10.0|^11.0",
44+
"illuminate/notifications": "^9.0|^10.0|^11.0",
45+
"illuminate/queue": "^9.0|^10.0|^11.0",
4646
"phpstan/phpstan": "^1.9",
4747
"phpstan/phpstan-deprecation-rules": "^1.1"
4848
},

src/Auth/Authenticatable.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,9 @@ public function getRememberTokenName()
9292
{
9393
return 'rememberToken';
9494
}
95+
96+
public function getAuthPasswordName()
97+
{
98+
return 'password';
99+
}
95100
}

src/Auth/DoctrineUserProvider.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,20 @@ public function getModel()
142142
{
143143
return $this->entity;
144144
}
145+
146+
/**
147+
* @param array{password: string} $credentials
148+
* @return void
149+
*/
150+
public function rehashPasswordIfRequired(Authenticatable $user, array $credentials, bool $force = false)
151+
{
152+
if (! $this->hasher->needsRehash($user->getAuthPassword()) && ! $force) {
153+
return;
154+
}
155+
assert(method_exists($user, 'setPassword'));
156+
157+
$user->setPassword($this->hasher->make($credentials['password']));
158+
$this->em->persist($user);
159+
$this->em->flush();
160+
}
145161
}

src/DoctrineServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ protected function ensureValidatorIsUsable()
8888
if (!$this->isLumen()) {
8989
return;
9090
}
91+
assert(property_exists($this->app, 'availableBindings'));
9192

9293
if ($this->shouldRegisterDoctrinePresenceValidator()) {
9394
// due to weirdness the default presence verifier overrides one set by a service provider

tests/Auth/DoctrineUserProviderTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,43 @@ public function test_can_validate_credentials()
140140
));
141141
}
142142

143+
public function test_rehash_password_if_required_rehash()
144+
{
145+
$user = new AuthenticableMock;
146+
147+
$this->hasher->shouldReceive('needsRehash')->once()->andReturn(true);
148+
$this->hasher->shouldReceive('make')->once()->andReturn('hashedPassword');
149+
$this->em->shouldReceive('persist')->once();
150+
$this->em->shouldReceive('flush')->once();
151+
152+
$this->provider->rehashPasswordIfRequired($user, ['password' => 'rawPassword'], false);
153+
$this->assertEquals('hashedPassword', $user->getPassword());
154+
}
155+
156+
public function test_rehash_password_if_required_rehash_force()
157+
{
158+
$user = new AuthenticableMock;
159+
160+
$this->hasher->shouldReceive('needsRehash')->once()->andReturn(false);
161+
$this->hasher->shouldReceive('make')->once()->andReturn('hashedPassword');
162+
$this->em->shouldReceive('persist')->once();
163+
$this->em->shouldReceive('flush')->once();
164+
165+
$this->provider->rehashPasswordIfRequired($user, ['password' => 'rawPassword'], true);
166+
$this->assertEquals('hashedPassword', $user->getPassword());
167+
}
168+
169+
public function test_rehash_password_if_required_rehash_norehash_needed()
170+
{
171+
$user = new AuthenticableMock;
172+
$user->setPassword('originalPassword');
173+
174+
$this->hasher->shouldReceive('needsRehash')->once()->andReturn(false);
175+
176+
$this->provider->rehashPasswordIfRequired($user, ['password' => 'rawPassword'], false);
177+
$this->assertEquals('originalPassword', $user->getPassword());
178+
}
179+
143180
protected function mockGetRepository($class = AuthenticableMock::class)
144181
{
145182
$this->em->shouldReceive('getRepository')

0 commit comments

Comments
 (0)