1
0
mirror of https://github.com/flarum/core.git synced 2025-10-27 05:31:29 +01:00

Refactor password checker, add extender (#2176)

This commit is contained in:
Alexander Skvortsov
2021-02-22 17:08:36 -05:00
committed by GitHub
parent fa10d794a4
commit 509adf228a
5 changed files with 206 additions and 5 deletions

View File

@@ -11,6 +11,9 @@ namespace Flarum\User\Event;
use Flarum\User\User;
/**
* @deprecated beta 16, remove in beta 17. Use Auth extender instead.
*/
class CheckingPassword
{
/**

View File

@@ -120,6 +120,13 @@ class User extends AbstractModel
*/
protected static $gate;
/**
* Callbacks to check passwords.
*
* @var array
*/
protected static $passwordCheckers;
/**
* Boot the model.
*
@@ -183,6 +190,11 @@ class User extends AbstractModel
static::$displayNameDriver = $driver;
}
public static function setPasswordCheckers(array $checkers)
{
static::$passwordCheckers = $checkers;
}
/**
* Rename the user.
*
@@ -333,11 +345,17 @@ class User extends AbstractModel
{
$valid = static::$dispatcher->until(new CheckingPassword($this, $password));
if ($valid !== null) {
return $valid;
foreach (static::$passwordCheckers as $checker) {
$result = $checker($this, $password);
if ($result === false) {
return false;
} elseif ($result === true) {
$valid = true;
}
}
return static::$hasher->check($password, $this->password);
return $valid || false;
}
/**

View File

@@ -38,6 +38,7 @@ class UserServiceProvider extends AbstractServiceProvider
{
$this->registerAvatarsFilesystem();
$this->registerDisplayNameDrivers();
$this->registerPasswordCheckers();
$this->app->singleton('flarum.user.group_processors', function () {
return [];
@@ -88,6 +89,19 @@ class UserServiceProvider extends AbstractServiceProvider
->give($avatarsFilesystem);
}
protected function registerPasswordCheckers()
{
$this->app->singleton('flarum.user.password_checkers', function () {
return [
'standard' => function (User $user, $password) {
if ($this->app->make('hash')->check($password, $user->password)) {
return true;
}
}
];
});
}
/**
* {@inheritdoc}
*/
@@ -97,12 +111,13 @@ class UserServiceProvider extends AbstractServiceProvider
User::addGroupProcessor(ContainerUtil::wrapCallback($callback, $this->app));
}
$events = $this->app->make('events');
User::setPasswordCheckers($this->app->make('flarum.user.password_checkers'));
User::setHasher($this->app->make('hash'));
User::setGate($this->app->makeWith(Access\Gate::class, ['policyClasses' => $this->app->make('flarum.policies')]));
User::setDisplayNameDriver($this->app->make('flarum.user.display_name.driver'));
$events = $this->app->make('events');
$events->listen(Saving::class, SelfDemotionGuard::class);
$events->listen(Registered::class, AccountActivationMailer::class);
$events->listen(EmailChangeRequested::class, EmailConfirmationMailer::class);