-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathRolesMigrator.php
265 lines (234 loc) · 6.92 KB
/
RolesMigrator.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
<?php
namespace Statamic\Migrator;
use Statamic\Migrator\Exceptions\MigratorSkippedException;
use Statamic\Support\Str;
class RolesMigrator extends Migrator
{
use Concerns\MigratesFile;
protected $roles;
/**
* Perform migration.
*/
public function migrate()
{
$this
->setNewPath(resource_path('users/roles.yaml'))
->validate()
->parseRoles()
->migrateRoles()
->saveMigratedYaml($this->roles, $this->newPath());
}
/**
* Validate whether roles file has been modified.
*
* @return $this
*
* @throws MigratorSkippedException
*/
protected function validate()
{
if ($this->overwrite || ! $this->files->exists($this->newPath())) {
return $this;
}
$currentRoles = $this->files->get($this->newPath());
$defaultRoles = $this->files->get('vendor/statamic/cms/resources/users/roles.yaml');
if ($currentRoles !== $defaultRoles) {
throw new MigratorSkippedException('Roles file [resources/users/roles.yaml] has already been modified.');
}
return $this;
}
/**
* Parse roles.
*
* @return $this
*/
protected function parseRoles()
{
$this->roles = $this->getSourceYaml('settings/users/roles.yaml', true);
return $this;
}
/**
* Migrate roles.
*
* @return $this
*/
protected function migrateRoles()
{
$this->roles = $this->roles
->keyBy(function ($role) {
return $this->migrateSlug($role);
})
->map(function ($role) {
return $this->migrateRole($role);
});
return $this;
}
/**
* Migrate slug.
*
* @param array $role
* @return string
*/
public function migrateSlug($role)
{
return $role['slug'] ?? Str::snake($role['title']);
}
/**
* Migrate role.
*
* @param array $role
* @return array
*/
protected function migrateRole($role)
{
$role['permissions'] = collect($role['permissions'] ?? [])
->map(function ($permission) {
return $this->migratePermission($permission);
})
->flatten()
->filter()
->values()
->all();
return $role;
}
/**
* Migrate permission.
*
* @param string $permission
* @return string|array
*/
protected function migratePermission($permission)
{
switch (true) {
case $permission === 'cp:access':
return 'access cp';
case preg_match('/^(pages):(view|edit|create|delete|reorder)$/', $permission, $matches):
case preg_match('/^collections:(\w+):(view|edit|create|delete)$/', $permission, $matches):
return $this->migrateCollectionPermission($matches[1], $matches[2]);
case preg_match('/^taxonomies:(\w+):(view|edit|create|delete)$/', $permission, $matches):
return $this->migrateTaxonomyPermission($matches[1], $matches[2]);
case preg_match('/^assets:(\w+):(view|edit|create|delete)$/', $permission, $matches):
return $this->migrateAssetContainerPermission($matches[1], $matches[2]);
case preg_match('/^globals:(\w+):(view|edit)$/', $permission, $matches):
return $this->migrateGlobalSetPermission($matches[1], $matches[2]);
case $permission === 'updater':
return 'view updates';
case $permission === 'updater:update':
return 'perform updates';
case $permission === 'resolve_duplicates':
return 'resolve duplicate ids';
case preg_match('/^users:(view|edit|create|delete|edit-passwords|edit-roles)$/', $permission, $matches):
return $this->migrateUserPermission($matches[1]);
case $permission === 'forms':
return $this->migrateFormPermission();
default:
return $permission;
}
}
/**
* Migrate collection permission.
*
* @param string $collection
* @param string $action
* @return string|array
*/
protected function migrateCollectionPermission($collection, $action)
{
if ($action === 'edit' && $collection !== 'pages') {
return [
"edit {$collection} entries",
"reorder {$collection} entries",
];
}
if ($action === 'create') {
return [
"create {$collection} entries",
"publish {$collection} entries",
];
}
return "{$action} {$collection} entries";
}
/**
* Migrate taxonomy permission.
*
* @param string $taxonomy
* @param string $action
* @return string
*/
protected function migrateTaxonomyPermission($taxonomy, $action)
{
return "{$action} {$taxonomy} terms";
}
/**
* Migrate asset container permission.
*
* @param string $container
* @param string $action
* @return string|array
*/
protected function migrateAssetContainerPermission($container, $action)
{
if ($action === 'edit') {
return [
"edit {$container} assets",
"move {$container} assets",
"rename {$container} assets",
];
}
if ($action === 'create') {
return "upload {$container} assets";
}
return "{$action} {$container} assets";
}
/**
* Migrate global set permission.
*
* @param string $globalSet
* @param string $action
* @return string|null
*/
protected function migrateGlobalSetPermission($globalSet, $action)
{
if ($action === 'view') {
return null;
}
return "{$action} {$globalSet} globals";
}
/**
* Migrate user permission.
*
* @param string $action
* @return string
*/
protected function migrateUserPermission($action)
{
if ($action === 'edit-passwords') {
return 'change passwords';
}
if ($action === 'edit-roles') {
return 'edit roles';
}
return "{$action} users";
}
/**
* Migrate access form permission.
*
* @return array
*/
protected function migrateFormPermission()
{
if (! $this->files->exists($path = $this->sitePath('settings/formsets'))) {
return [];
}
return collect($this->files->files($path))
->map
->getFilenameWithoutExtension()
->flatMap(function ($handle) {
return [
"view {$handle} form submissions",
"delete {$handle} form submissions",
];
})
->all();
}
}