1
0
mirror of https://github.com/flarum/core.git synced 2025-07-26 03:01:22 +02: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 b89ccaf83c
commit c10ebea00c
5 changed files with 93 additions and 2 deletions

View File

@@ -15,9 +15,10 @@ use Illuminate\Contracts\Container\Container;
class User implements ExtenderInterface
{
private $displayNameDrivers = [];
private $groupProcessors = [];
/**
* Add a mail driver.
* Add a display name driver.
*
* @param string $identifier Identifier for display name driver. E.g. 'username' for UserNameDriver
* @param string $driver ::class attribute of driver class, which must implement Flarum\User\DisplayName\DriverInterface
@@ -29,10 +30,35 @@ class User implements ExtenderInterface
return $this;
}
/**
* Dynamically process a user's list of groups when calculating permissions.
* This can be used to give a user permissions for groups they aren't actually in, based on context.
* It will not change the group badges displayed for the user.
*
* @param callable $callable
*
* The callable can be a closure or invokable class, and should accept:
* - \Flarum\User\User $user: the user in question.
* - array $groupIds: an array of ids for the groups the user belongs to.
*
* The callable should return:
* - array $groupIds: an array of ids for the groups the user belongs to.
*/
public function permissionGroups(callable $callable)
{
$this->groupProcessors[] = $callable;
return $this;
}
public function extend(Container $container, Extension $extension = null)
{
$container->extend('flarum.user.display_name.supported_drivers', function ($existingDrivers) {
return array_merge($existingDrivers, $this->displayNameDrivers);
});
$container->extend('flarum.user.group_processors', function ($existingRelations) {
return array_merge($existingRelations, $this->groupProcessors);
});
}
}