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

User Extender (prepareGroups functionality) (#2110)

This commit is contained in:
Alexander Skvortsov
2020-07-17 06:18:35 -04:00
committed by GitHub
parent 71abac0323
commit 37ebeb5705
5 changed files with 93 additions and 2 deletions

View File

@@ -83,6 +83,12 @@ class User extends AbstractModel
*/
protected $session;
/**
* An array of callables, through each of which the user's list of groups is passed
* before being returned.
*/
protected static $groupProcessors = [];
/**
* An array of registered user preferences. Each preference is defined with
* a key, and its value is an array containing the following keys:.
@@ -660,8 +666,13 @@ class User extends AbstractModel
$groupIds = array_merge($groupIds, [Group::MEMBER_ID], $this->groups->pluck('id')->all());
}
// Deprecated, remove in beta 14.
event(new PrepareUserGroups($this, $groupIds));
foreach (static::$groupProcessors as $processor) {
$groupIds = $processor($this, $groupIds);
}
return Permission::whereIn('group_id', $groupIds);
}
@@ -751,6 +762,17 @@ class User extends AbstractModel
static::$preferences[$key] = compact('transformer', 'default');
}
/**
* Register a callback that processes a user's list of groups.
*
* @param callable $callback
* @return array $groupIds
*/
public static function addGroupProcessor($callback)
{
static::$groupProcessors[] = $callback;
}
/**
* Get the key for a preference which flags whether or not the user will
* receive a notification for $type via $method.

View File

@@ -31,6 +31,10 @@ class UserServiceProvider extends AbstractServiceProvider
{
$this->registerAvatarsFilesystem();
$this->registerDisplayNameDrivers();
$this->app->singleton('flarum.user.group_processors', function () {
return [];
});
}
protected function registerDisplayNameDrivers()
@@ -72,6 +76,14 @@ class UserServiceProvider extends AbstractServiceProvider
*/
public function boot()
{
foreach ($this->app->make('flarum.user.group_processors') as $callback) {
if (is_string($callback)) {
$callback = $this->app->make($callback);
}
User::addGroupProcessor($callback);
}
User::setHasher($this->app->make('hash'));
User::setGate($this->app->make(Gate::class));
User::setDisplayNameDriver($this->app->make('flarum.user.display_name.driver'));