Skip to content

Commit 39d2f20

Browse files
committed
implemented "rehashPasswordIfRequired()" method to DoctrineUserProvider
1 parent 2e0b1e1 commit 39d2f20

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/Auth/DoctrineUserProvider.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,15 @@ public function getModel()
142142
{
143143
return $this->entity;
144144
}
145+
146+
public function rehashPasswordIfRequired(Authenticatable $user, array $credentials, bool $force = false)
147+
{
148+
if (! $this->hasher->needsRehash($user->getAuthPassword()) && ! $force) {
149+
return;
150+
}
151+
152+
$user->setPassword($this->hasher->make($credentials['password']));
153+
$this->em->persist($user);
154+
$this->em->flush();
155+
}
145156
}

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)