-
-
Notifications
You must be signed in to change notification settings - Fork 829
/
User.php
89 lines (76 loc) · 2.9 KB
/
User.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?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 Flarum\User\DisplayName\DriverInterface;
use Flarum\User\User as FlarumUser;
use Illuminate\Contracts\Container\Container;
class User implements ExtenderInterface
{
private array $displayNameDrivers = [];
private array $groupProcessors = [];
private array $preferences = [];
/**
* Add a display name driver.
*
* @param string $identifier: Identifier for display name driver. E.g. 'username' for UserNameDriver
* @param class-string<DriverInterface> $driver: ::class attribute of driver class, which must implement Flarum\User\DisplayName\DriverInterface
* @return self
*/
public function displayNameDriver(string $identifier, string $driver): self
{
$this->displayNameDrivers[$identifier] = $driver;
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(\Flarum\User\User $user, int[] $groupIds): int[])|class-string $callback
*
* 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.
*
* @return self
*/
public function permissionGroups(callable|string $callback): self
{
$this->groupProcessors[] = $callback;
return $this;
}
/**
* Register a new user preference.
*
* @param string $key
* @param callable|null $transformer
* @param mixed|null $default
* @return self
*/
public function registerPreference(string $key, ?callable $transformer = null, mixed $default = null): self
{
$this->preferences[$key] = compact('transformer', 'default');
return $this;
}
public function extend(Container $container, Extension $extension = null): void
{
$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);
});
foreach ($this->preferences as $key => $preference) {
FlarumUser::registerPreference($key, $preference['transformer'], $preference['default']);
}
}
}