1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace A17\Twill\Repositories\Behaviors; |
4
|
|
|
|
5
|
|
|
use A17\Twill\Models\Role; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use A17\Twill\Models\Permission; |
8
|
|
|
|
9
|
|
|
trait HandleRolePermissions |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Retrieve role permissions fields |
13
|
|
|
* |
14
|
|
|
* @param Model|Role $object |
|
|
|
|
15
|
|
|
* @param array $fields |
16
|
|
|
* @return array |
17
|
|
|
*/ |
18
|
|
|
public function getFormFieldsHandleRolePermissions($object, $fields) |
19
|
|
|
{ |
20
|
|
|
$object->permissions()->get(); |
|
|
|
|
21
|
|
|
|
22
|
|
|
foreach ($object->permissions()->global()->pluck('name')->toArray() as $permissionName) { |
23
|
|
|
$fields[$permissionName] = true; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
foreach (Permission::permissionableModules() as $moduleName) { |
27
|
|
|
$modulePermission = $object->permissions()->module()->ofModuleName($moduleName)->first(); |
28
|
|
|
if ($modulePermission) { |
29
|
|
|
$fields['module_' . $moduleName . '_permissions'] = $modulePermission->name; |
30
|
|
|
} else { |
31
|
|
|
$fields['module_' . $moduleName . '_permissions'] = 'none'; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return $fields; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Function executed after save on role form |
40
|
|
|
* |
41
|
|
|
* @param Model|Role $object |
42
|
|
|
* @param array $fields |
43
|
|
|
*/ |
44
|
|
|
public function afterSaveHandleRolePermissions($object, $fields) |
45
|
|
|
{ |
46
|
|
|
$this->addOrRemoveUsersToEveryoneGroup($object); |
47
|
|
|
|
48
|
|
|
$this->updateRolePermissions($object, $fields); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function addOrRemoveUsersToEveryoneGroup($role) |
52
|
|
|
{ |
53
|
|
|
$everyoneGroup = twillModel('group')::getEveryoneGroup(); |
54
|
|
|
$roleUserIds = $role->users->pluck('id')->toArray(); |
55
|
|
|
|
56
|
|
|
if ($role->in_everyone_group) { |
57
|
|
|
$everyoneGroup->users()->syncWithoutDetaching($roleUserIds); |
58
|
|
|
} else { |
59
|
|
|
$everyoneGroup->users()->detach($roleUserIds); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function updateRolePermissions($role, $fields) |
64
|
|
|
{ |
65
|
|
|
foreach (Permission::available(Permission::SCOPE_GLOBAL) as $permissionName) { |
66
|
|
|
if (isset($fields[$permissionName]) && $fields[$permissionName] === true) { |
67
|
|
|
$role->grantGlobalPermission($permissionName); |
68
|
|
|
} else { |
69
|
|
|
$role->revokeGlobalPermission($permissionName); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
foreach ($fields as $key => $permissionName) { |
74
|
|
|
if (Str::startsWith($key, 'module_') && Str::endsWith($key, '_permissions')) { |
75
|
|
|
$modulePermissions = Permission::available(Permission::SCOPE_MODULE); |
76
|
|
|
$model = getModelByModuleName($moduleName = explode('_', $key)[1]); |
77
|
|
|
|
78
|
|
|
$currentPermission = $role->permissions() |
79
|
|
|
->where('permissionable_type', $model) |
80
|
|
|
->whereIn('name', $modulePermissions) |
81
|
|
|
->first(); |
82
|
|
|
|
83
|
|
|
if (!$currentPermission || $permissionName != $currentPermission->name) { |
84
|
|
|
$role->revokeAllModulePermission($model); |
85
|
|
|
if (in_array($permissionName, $modulePermissions)) { |
86
|
|
|
$role->grantModulePermission($permissionName, $model); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$role->save(); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths