remove EncryptUserPassword event

This commit is contained in:
Pig Fang 2020-06-03 17:08:22 +08:00
parent 70bf5f10bc
commit 3841459bcf
3 changed files with 26 additions and 48 deletions

View File

@ -1,25 +0,0 @@
<?php
namespace App\Events;
use App\Models\User;
class EncryptUserPassword extends Event
{
public $user;
public $raw;
/**
* Create a new event instance.
*
* @param string $raw the raw password before encrypted
*
* @return void
*/
public function __construct($raw, User $user)
{
$this->raw = $raw;
$this->user = $user;
}
}

View File

@ -2,39 +2,31 @@
namespace App\Models\Concerns; namespace App\Models\Concerns;
use App\Events\EncryptUserPassword; use App\Services\Cipher\BaseCipher;
use Illuminate\Support\Arr; use Blessing\Filter;
trait HasPassword trait HasPassword
{ {
public function verifyPassword(string $raw) public function verifyPassword(string $raw)
{ {
// Compare directly if any responses is returned by event dispatcher /** @var BaseCipher */
if ($result = $this->getEncryptedPwdFromEvent($raw, $this)) { $cipher = resolve('cipher');
return hash_equals($this->password, $result); // @codeCoverageIgnore /** @var Filter */
} $filter = resolve(Filter::class);
$password = $this->password;
$user = $this;
return app('cipher')->verify($raw, $this->password, config('secure.salt')); $passed = $cipher->verify($raw, $password, config('secure.salt'));
} $passed = $filter->apply('verify_password', $passed, [$raw, $user]);
/** return $passed;
* Try to get encrypted password from event dispatcher.
*/
public function getEncryptedPwdFromEvent(string $raw)
{
$responses = event(new EncryptUserPassword($raw, $this));
return Arr::get($responses, 0);
} }
public function changePassword(string $password): bool public function changePassword(string $password): bool
{ {
$responses = event(new EncryptUserPassword($password, $this)); $password = resolve('cipher')->hash($password, config('secure.salt'));
$hash = Arr::get($responses, 0); $password = resolve(Filter::class)->apply('user_password', $password);
if (empty($hash)) { $this->password = $password;
$hash = app('cipher')->hash($password, config('secure.salt'));
}
$this->password = $hash;
return $this->save(); return $this->save();
} }

View File

@ -330,6 +330,7 @@ class UserControllerTest extends TestCase
]); ]);
// Change password successfully // Change password successfully
$filter = Fakes\Filter::fake();
$this->postJson('/user/profile', [ $this->postJson('/user/profile', [
'action' => 'password', 'action' => 'password',
'current_password' => '12345678', 'current_password' => '12345678',
@ -349,7 +350,17 @@ class UserControllerTest extends TestCase
return true; return true;
}); });
Event::assertDispatched(Events\EncryptUserPassword::class); $filter->assertApplied('verify_password', function ($passed, $raw, $u) use ($user) {
$this->assertEquals('12345678', $raw);
$this->assertTrue($user->is($u));
return true;
});
$filter->assertApplied('user_password', function ($password) {
$this->assertTrue(password_verify('87654321', $password));
return true;
});
$this->assertTrue(User::find($user->uid)->verifyPassword('87654321')); $this->assertTrue(User::find($user->uid)->verifyPassword('87654321'));
// After changed password, user should re-login. // After changed password, user should re-login.
$this->assertGuest(); $this->assertGuest();