Skip to content

Commit e886360

Browse files
netpoktaylorotwell
andauthored
[8.x] Copy password rule to current_password (#37650)
* Rename validation and add alias * Add test for renamed rule * Update ValidatesAttributes.php Co-authored-by: Taylor Otwell <[email protected]>
1 parent 0b1610f commit e886360

File tree

2 files changed

+118
-11
lines changed

2 files changed

+118
-11
lines changed

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,28 @@ public function validateConfirmed($attribute, $value)
348348
return $this->validateSame($attribute, $value, [$attribute.'_confirmation']);
349349
}
350350

351+
/**
352+
* Validate that the password of the currently authenticated user matches the given value.
353+
*
354+
* @param string $attribute
355+
* @param mixed $value
356+
* @param array $parameters
357+
* @return bool
358+
*/
359+
protected function validateCurrentPassword($attribute, $value, $parameters)
360+
{
361+
$auth = $this->container->make('auth');
362+
$hasher = $this->container->make('hash');
363+
364+
$guard = $auth->guard(Arr::first($parameters));
365+
366+
if ($guard->guest()) {
367+
return false;
368+
}
369+
370+
return $hasher->check($value, $guard->user()->getAuthPassword());
371+
}
372+
351373
/**
352374
* Validate that an attribute is a valid date.
353375
*
@@ -1325,7 +1347,7 @@ public function validateNumeric($attribute, $value)
13251347
}
13261348

13271349
/**
1328-
* Validate that the current logged in user's password matches the given value.
1350+
* Validate that the password of the currently authenticated user matches the given value.
13291351
*
13301352
* @param string $attribute
13311353
* @param mixed $value
@@ -1334,16 +1356,7 @@ public function validateNumeric($attribute, $value)
13341356
*/
13351357
protected function validatePassword($attribute, $value, $parameters)
13361358
{
1337-
$auth = $this->container->make('auth');
1338-
$hasher = $this->container->make('hash');
1339-
1340-
$guard = $auth->guard(Arr::first($parameters));
1341-
1342-
if ($guard->guest()) {
1343-
return false;
1344-
}
1345-
1346-
return $hasher->check($value, $guard->user()->getAuthPassword());
1359+
return $this->validateCurrentPassword($attribute, $value, $parameters);
13471360
}
13481361

13491362
/**

tests/Validation/ValidationValidatorTest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,100 @@ public function testValidateArrayKeys()
718718
$this->assertFalse($v->passes());
719719
}
720720

721+
public function testValidateCurrentPassword()
722+
{
723+
// Fails when user is not logged in.
724+
$auth = m::mock(Guard::class);
725+
$auth->shouldReceive('guard')->andReturn($auth);
726+
$auth->shouldReceive('guest')->andReturn(true);
727+
728+
$hasher = m::mock(Hasher::class);
729+
730+
$container = m::mock(Container::class);
731+
$container->shouldReceive('make')->with('auth')->andReturn($auth);
732+
$container->shouldReceive('make')->with('hash')->andReturn($hasher);
733+
734+
$trans = $this->getTranslator();
735+
$trans->shouldReceive('get')->andReturnArg(0);
736+
737+
$v = new Validator($trans, ['password' => 'foo'], ['password' => 'current_password']);
738+
$v->setContainer($container);
739+
740+
$this->assertFalse($v->passes());
741+
742+
// Fails when password is incorrect.
743+
$user = m::mock(Authenticatable::class);
744+
$user->shouldReceive('getAuthPassword');
745+
746+
$auth = m::mock(Guard::class);
747+
$auth->shouldReceive('guard')->andReturn($auth);
748+
$auth->shouldReceive('guest')->andReturn(false);
749+
$auth->shouldReceive('user')->andReturn($user);
750+
751+
$hasher = m::mock(Hasher::class);
752+
$hasher->shouldReceive('check')->andReturn(false);
753+
754+
$container = m::mock(Container::class);
755+
$container->shouldReceive('make')->with('auth')->andReturn($auth);
756+
$container->shouldReceive('make')->with('hash')->andReturn($hasher);
757+
758+
$trans = $this->getTranslator();
759+
$trans->shouldReceive('get')->andReturnArg(0);
760+
761+
$v = new Validator($trans, ['password' => 'foo'], ['password' => 'current_password']);
762+
$v->setContainer($container);
763+
764+
$this->assertFalse($v->passes());
765+
766+
// Succeeds when password is correct.
767+
$user = m::mock(Authenticatable::class);
768+
$user->shouldReceive('getAuthPassword');
769+
770+
$auth = m::mock(Guard::class);
771+
$auth->shouldReceive('guard')->andReturn($auth);
772+
$auth->shouldReceive('guest')->andReturn(false);
773+
$auth->shouldReceive('user')->andReturn($user);
774+
775+
$hasher = m::mock(Hasher::class);
776+
$hasher->shouldReceive('check')->andReturn(true);
777+
778+
$container = m::mock(Container::class);
779+
$container->shouldReceive('make')->with('auth')->andReturn($auth);
780+
$container->shouldReceive('make')->with('hash')->andReturn($hasher);
781+
782+
$trans = $this->getTranslator();
783+
$trans->shouldReceive('get')->andReturnArg(0);
784+
785+
$v = new Validator($trans, ['password' => 'foo'], ['password' => 'current_password']);
786+
$v->setContainer($container);
787+
788+
$this->assertTrue($v->passes());
789+
790+
// We can use a specific guard.
791+
$user = m::mock(Authenticatable::class);
792+
$user->shouldReceive('getAuthPassword');
793+
794+
$auth = m::mock(Guard::class);
795+
$auth->shouldReceive('guard')->with('custom')->andReturn($auth);
796+
$auth->shouldReceive('guest')->andReturn(false);
797+
$auth->shouldReceive('user')->andReturn($user);
798+
799+
$hasher = m::mock(Hasher::class);
800+
$hasher->shouldReceive('check')->andReturn(true);
801+
802+
$container = m::mock(Container::class);
803+
$container->shouldReceive('make')->with('auth')->andReturn($auth);
804+
$container->shouldReceive('make')->with('hash')->andReturn($hasher);
805+
806+
$trans = $this->getTranslator();
807+
$trans->shouldReceive('get')->andReturnArg(0);
808+
809+
$v = new Validator($trans, ['password' => 'foo'], ['password' => 'current_password:custom']);
810+
$v->setContainer($container);
811+
812+
$this->assertTrue($v->passes());
813+
}
814+
721815
public function testValidateFilled()
722816
{
723817
$trans = $this->getIlluminateArrayTranslator();

0 commit comments

Comments
 (0)