mirror of
https://github.com/bs-community/blessing-skin-server.git
synced 2025-02-17 14:49:27 +08:00
remove EncryptUserPassword
event
This commit is contained in:
parent
70bf5f10bc
commit
3841459bcf
@ -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;
|
||||
}
|
||||
}
|
@ -2,39 +2,31 @@
|
||||
|
||||
namespace App\Models\Concerns;
|
||||
|
||||
use App\Events\EncryptUserPassword;
|
||||
use Illuminate\Support\Arr;
|
||||
use App\Services\Cipher\BaseCipher;
|
||||
use Blessing\Filter;
|
||||
|
||||
trait HasPassword
|
||||
{
|
||||
public function verifyPassword(string $raw)
|
||||
{
|
||||
// Compare directly if any responses is returned by event dispatcher
|
||||
if ($result = $this->getEncryptedPwdFromEvent($raw, $this)) {
|
||||
return hash_equals($this->password, $result); // @codeCoverageIgnore
|
||||
}
|
||||
/** @var BaseCipher */
|
||||
$cipher = resolve('cipher');
|
||||
/** @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]);
|
||||
|
||||
/**
|
||||
* Try to get encrypted password from event dispatcher.
|
||||
*/
|
||||
public function getEncryptedPwdFromEvent(string $raw)
|
||||
{
|
||||
$responses = event(new EncryptUserPassword($raw, $this));
|
||||
|
||||
return Arr::get($responses, 0);
|
||||
return $passed;
|
||||
}
|
||||
|
||||
public function changePassword(string $password): bool
|
||||
{
|
||||
$responses = event(new EncryptUserPassword($password, $this));
|
||||
$hash = Arr::get($responses, 0);
|
||||
if (empty($hash)) {
|
||||
$hash = app('cipher')->hash($password, config('secure.salt'));
|
||||
}
|
||||
$this->password = $hash;
|
||||
$password = resolve('cipher')->hash($password, config('secure.salt'));
|
||||
$password = resolve(Filter::class)->apply('user_password', $password);
|
||||
$this->password = $password;
|
||||
|
||||
return $this->save();
|
||||
}
|
||||
|
@ -330,6 +330,7 @@ class UserControllerTest extends TestCase
|
||||
]);
|
||||
|
||||
// Change password successfully
|
||||
$filter = Fakes\Filter::fake();
|
||||
$this->postJson('/user/profile', [
|
||||
'action' => 'password',
|
||||
'current_password' => '12345678',
|
||||
@ -349,7 +350,17 @@ class UserControllerTest extends TestCase
|
||||
|
||||
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'));
|
||||
// After changed password, user should re-login.
|
||||
$this->assertGuest();
|
||||
|
Loading…
Reference in New Issue
Block a user