2020-05-19 18:45:56 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Flarum.
|
|
|
|
*
|
|
|
|
* For detailed copyright and license information, please view the
|
|
|
|
* LICENSE file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Flarum\Extend;
|
|
|
|
|
|
|
|
use Flarum\Extension\Extension;
|
|
|
|
use Illuminate\Contracts\Container\Container;
|
|
|
|
|
|
|
|
class User implements ExtenderInterface
|
|
|
|
{
|
|
|
|
private $displayNameDrivers = [];
|
2020-07-17 06:18:35 -04:00
|
|
|
private $groupProcessors = [];
|
2020-05-19 18:45:56 -04:00
|
|
|
|
|
|
|
/**
|
2020-07-17 06:18:35 -04:00
|
|
|
* Add a display name driver.
|
2020-05-19 18:45:56 -04:00
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
public function displayNameDriver(string $identifier, $driver)
|
|
|
|
{
|
2020-06-29 19:32:08 -04:00
|
|
|
$this->displayNameDrivers[$identifier] = $driver;
|
2020-05-19 18:45:56 -04:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-07-17 06:18:35 -04:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2020-05-19 18:45:56 -04:00
|
|
|
public function extend(Container $container, Extension $extension = null)
|
|
|
|
{
|
|
|
|
$container->extend('flarum.user.display_name.supported_drivers', function ($existingDrivers) {
|
2020-06-29 19:32:08 -04:00
|
|
|
return array_merge($existingDrivers, $this->displayNameDrivers);
|
2020-05-19 18:45:56 -04:00
|
|
|
});
|
2020-07-17 06:18:35 -04:00
|
|
|
|
|
|
|
$container->extend('flarum.user.group_processors', function ($existingRelations) {
|
|
|
|
return array_merge($existingRelations, $this->groupProcessors);
|
|
|
|
});
|
2020-05-19 18:45:56 -04:00
|
|
|
}
|
|
|
|
}
|