generated from yardinternet/skeleton-package
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserRoles.php
271 lines (222 loc) · 6.01 KB
/
UserRoles.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
declare(strict_types=1);
namespace Yard\UserRoles;
use Role_Command;
use Webmozart\Assert\Assert;
use WP_CLI;
use WP_CLI\ExitException;
use WP_Role;
class UserRoles
{
private string $prefix;
/**
* @param array<mixed> $config
*
* @throws ExitException
*/
public function __construct(private array $config, private Role_Command $roleCommand, private WP_CLI $wpCli)
{
$this->prefix = $this->prefixValidate($config);
}
/**
* (Re)creates roles based on the provided configuration.
*/
public function createRoles(): void
{
$this->deleteCustomRoles();
$this->resetCoreRoles();
$this->addCustomRoles();
$this->deleteCoreRoles();
}
/**
* @param array<mixed> $config
*
* @throws ExitException
*/
private function prefixValidate(array $config): string
{
$prefix = $config['prefix'] ?? '';
if (! is_string($prefix) || '' === $prefix) {
$this->wpCli::error('No prefix found in configuration file. Aborting role creation.');
}
return $prefix . '_';
}
private function deleteCustomRoles(): void
{
$this->wpCli::log($this->wpCli::colorize('%MDelete custom roles:%n'));
$customRoles = $this->currentCustomRoles();
if (0 === count($customRoles)) {
$this->wpCli::warning("No custom roles with prefix '" . $this->prefix . "' found in database. Skipping custom role deletion.");
return;
}
foreach ($customRoles as $role) {
$this->roleCommand->delete([$role]);
}
}
/**
* @return array<int, string>
*/
private function currentCustomRoles(): array
{
return array_filter(
array_keys(wp_roles()->roles),
fn (string $role): bool => str_starts_with($role, $this->prefix)
);
}
private function addCustomRoles(): void
{
$this->wpCli::log($this->wpCli::colorize('%MCreate custom roles:%n'));
if (false === $this->rolesValid()) {
$this->wpCli::warning('No roles found in config. Skipping custom role creation.');
return;
}
$roles = $this->config['roles'];
foreach ($roles as $role => $props) {
if (! isset($props['display_name']) || ! is_string($props['display_name'])) {
$this->wpCli::warning("No display name configured for role $role. Skipping role creation.");
continue;
}
if ($this->cloneValid($props)) {
$this->addClone($role, $props);
} else {
$this->addRole($role, $props);
}
}
}
/**
* @param array<mixed> $props
*/
private function addRole(string $role, array $props): void
{
$role = $this->createRole($role, $props);
$capabilities = $this->capsFromRoleProps($props);
$this->addCaps($role, $capabilities);
}
/**
* @param array<mixed> $props
*/
private function addClone(string $role, array $props): void
{
$role = $this->createRole($role, $props, ['clone' => $props['clone']['from']]);
if (isset($props['clone']['add']) && is_array($props['clone']['add'])) {
$caps = $this->capsFromRoleProps($props['clone']['add']);
$this->addCaps($role, $caps);
}
if (isset($props['clone']['remove']) && is_array($props['clone']['remove'])) {
$removeCaps = $this->capsFromRoleProps($props['clone']['remove']);
$this->removeCaps($role, $removeCaps);
}
}
/**
* @param array<mixed> $caps
*/
private function addCaps(WP_Role $role, array $caps): void
{
foreach ($caps as $cap => $grant) {
$role->add_cap((string)$cap, $grant);
}
}
/**
* @param array<mixed> $caps
*/
private function removeCaps(WP_Role $role, array $caps): void
{
foreach ($caps as $cap => $grant) {
$role->remove_cap((string)$cap);
}
}
/**
* @param array<mixed> $props
*/
private function cloneValid(array $props): bool
{
return isset($props['clone']['from'])
&& is_string($props['clone']['from']);
}
/**
* @param array<mixed> $props
* @param array<mixed> $assocArgs
*/
private function createRole(string $role, array $props, array $assocArgs = []): WP_Role
{
$this->roleCommand->create([
$this->prefix . $role,
$props['display_name'],
], $assocArgs);
$wpRole = get_role($this->prefix . $role);
Assert::notNull($wpRole);
return $wpRole;
}
private function rolesValid(): bool
{
return isset($this->config['roles'])
&& is_array($this->config['roles'])
&& 0 !== count($this->config['roles']);
}
private function resetCoreRoles(): void
{
$this->wpCli::log($this->wpCli::colorize('%MReset core roles:%n'));
$this->roleCommand->reset([], ['all' => true]);
}
private function deleteCoreRoles(): void
{
$this->wpCli::log($this->wpCli::colorize('%MDelete core roles:%n'));
if (! $this->coreRolesValid()) {
$this->wpCli::warning('No core roles found in config. Skipping core role deletion.');
return;
}
$coreRoles = $this->config['core_roles'];
foreach ($coreRoles as $role => $shouldStay) {
if (false === $shouldStay) {
$this->roleCommand->delete([$role]);
}
}
}
private function coreRolesValid(): bool
{
return isset($this->config['core_roles'])
&& is_array($this->config['core_roles'])
&& 0 !== count($this->config['core_roles']);
}
/**
* @param array<mixed> $props
*
* @return array<mixed>
*/
public function capsFromRoleProps(array $props): array
{
$capabilities = [];
if (! empty($props['cap_groups'])) {
$capGroups = $this->config['cap_groups'];
Assert::isArray($capGroups);
Assert::isArray($props['cap_groups']);
foreach ($props['cap_groups'] as $group) {
$groupCaps = $capGroups[$group];
Assert::isArray($groupCaps);
foreach ($groupCaps as $cap) {
$capabilities[$cap] = true;
}
}
}
if (! empty($props['post_type_caps'])) {
Assert::isArray($props['post_type_caps']);
foreach ($props['post_type_caps'] as $postType) {
$postTypeCaps = get_post_type_object($postType)?->cap;
if (null === $postTypeCaps) {
$this->wpCli::warning("Post type '$postType' does not exist. Skipping post type caps.");
continue;
}
Assert::object($postTypeCaps);
foreach ((array)$postTypeCaps as $cap) {
$capabilities[$cap] = true;
}
}
}
if (! empty($props['caps'])) {
foreach ($props['caps'] as $cap) {
$capabilities[$cap] = true;
}
}
return $capabilities;
}
}