|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Saito - The Threaded Web Forum |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copyright (c) the Saito Project Developers |
|
9
|
|
|
* @link https://github.com/Schlaefer/Saito |
|
10
|
|
|
* @license http://opensource.org/licenses/MIT |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Saito\User\Permission; |
|
14
|
|
|
|
|
15
|
|
|
use RuntimeException; |
|
16
|
|
|
|
|
17
|
|
|
class Roles |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Old mylittleforum string based user roles |
|
21
|
|
|
* |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
private $roles = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Shadows $this->roles for easy access with integer ID |
|
28
|
|
|
* |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
private $rolesAsInt = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Adds a new role |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $role Short title like 'user', 'mod', or 'admin' |
|
37
|
|
|
* @param int $id A unique id for this role |
|
38
|
|
|
* @param array $subroles Other roles this role represents |
|
39
|
|
|
* @return self |
|
40
|
|
|
*/ |
|
41
|
|
|
public function add(string $role, int $id, array $subroles = []): self |
|
42
|
|
|
{ |
|
43
|
|
|
$this->roles[$role] = ['id' => $id, 'type' => $role]; |
|
44
|
|
|
$this->roles[$role]['subroles'] = $subroles; |
|
45
|
|
|
$this->rolesAsInt[$id] = $this->roles[$role]; |
|
46
|
|
|
|
|
47
|
|
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get all roles for role |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $role Role |
|
54
|
|
|
* @param bool $includeAnon Include anon user |
|
55
|
|
|
* @return array All roles a role has |
|
56
|
|
|
*/ |
|
57
|
|
|
public function get(string $role, bool $includeAnon = true): array |
|
58
|
|
|
{ |
|
59
|
|
|
if (!isset($this->roles[$role])) { |
|
60
|
|
|
return []; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$roles = [$role]; |
|
64
|
|
|
foreach ($this->roles[$role]['subroles'] as $role) { |
|
65
|
|
|
if ($role === 'anon' && !$includeAnon) { |
|
66
|
|
|
continue; |
|
67
|
|
|
} |
|
68
|
|
|
$roles[] = $role; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $roles; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get all configured roles |
|
76
|
|
|
* |
|
77
|
|
|
* @param bool $includeAnon Include anon user |
|
78
|
|
|
* @return array |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getAvailable(bool $includeAnon = false): array |
|
81
|
|
|
{ |
|
82
|
|
|
$roles = $this->rolesAsInt; |
|
83
|
|
|
if (!$includeAnon) { |
|
84
|
|
|
unset($roles[0]); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $roles; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Get role id for type |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $type Type |
|
94
|
|
|
* @return int |
|
95
|
|
|
*/ |
|
96
|
|
|
public function typeToId(string $type): int |
|
97
|
|
|
{ |
|
98
|
|
|
if (isset($this->roles[$type])) { |
|
99
|
|
|
return $this->roles[$type]['id']; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
throw new RuntimeException(sprintf('Role "%s" not found.', $type)); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|