1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace A17\Twill\Repositories\Behaviors; |
4
|
|
|
|
5
|
|
|
use A17\Twill\Models\Group; |
6
|
|
|
use A17\Twill\Models\Model; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use A17\Twill\Models\Permission; |
9
|
|
|
|
10
|
|
|
trait HandleGroupPermissions |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Retrieve group permissions fields |
14
|
|
|
* |
15
|
|
|
* @param Model|Group $object |
16
|
|
|
* @param array $fields |
17
|
|
|
* @return array |
18
|
|
|
*/ |
19
|
|
|
public function getFormFieldsHandleGroupPermissions($object, $fields) |
20
|
|
|
{ |
21
|
|
|
if (config('twill.permissions.level') == 'roleGroup') { |
22
|
|
|
// Add active global permissions |
23
|
|
|
foreach ($object->permissions()->global()->get()->pluck('name') as $permissionName) { |
|
|
|
|
24
|
|
|
$fields[$permissionName] = true; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
// Add active module permissions |
28
|
|
|
foreach (Permission::permissionableModules() as $moduleName) { |
29
|
|
|
$modulePermission = $object->permissions()->module()->ofModuleName($moduleName)->first(); |
30
|
|
|
if ($modulePermission) { |
31
|
|
|
$fields['module_' . $moduleName . '_permissions'] = $modulePermission->name; |
32
|
|
|
} else { |
33
|
|
|
$fields['module_' . $moduleName . '_permissions'] = 'none'; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
} elseif (config('twill.permissions.level') == 'roleGroupItem') { |
37
|
|
|
// Add active item permissions |
38
|
|
|
foreach ($object->permissions()->moduleItem()->get() as $permission) { |
39
|
|
|
$model = $permission->permissionable()->first(); |
40
|
|
|
$moduleName = getModuleNameByModel($model); |
41
|
|
|
$fields[$moduleName . '_' . $model->id . '_permission'] = $permission->name; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// Add active subdomain permissions |
46
|
|
|
foreach ($object->subdomains_access ?? [] as $subdomain) { |
|
|
|
|
47
|
|
|
$fields['subdomain_access_' . $subdomain] = true; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $fields; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Function executed after save on group form |
55
|
|
|
* |
56
|
|
|
* @param Model|Group $object |
57
|
|
|
* @param array $fields |
58
|
|
|
*/ |
59
|
|
|
public function afterSaveHandleGroupPermissions($object, $fields) |
60
|
|
|
{ |
61
|
|
|
// Assign global permissions |
62
|
|
|
foreach (Permission::available(Permission::SCOPE_GLOBAL) as $permissionName) { |
63
|
|
|
if (isset($fields[$permissionName]) && $fields[$permissionName] === true) { |
64
|
|
|
$object->grantGlobalPermission($permissionName); |
65
|
|
|
} else { |
66
|
|
|
$object->revokeGlobalPermission($permissionName); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$subdomainsAccess = []; |
71
|
|
|
|
72
|
|
|
// Assign item permissions + subdomain permission |
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 = $object->permissions() |
79
|
|
|
->where('permissionable_type', $model) |
80
|
|
|
->whereIn('name', $modulePermissions) |
81
|
|
|
->first() |
82
|
|
|
; |
83
|
|
|
|
84
|
|
|
if (!$currentPermission || $permissionName != $currentPermission->name) { |
85
|
|
|
$object->revokeAllModulePermission($model); |
86
|
|
|
if (in_array($permissionName, $modulePermissions)) { |
87
|
|
|
$object->grantModulePermission($permissionName, $model); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} elseif (Str::endsWith($key, '_permission')) { |
91
|
|
|
$item_name = explode('_', $key)[0]; |
92
|
|
|
$item_id = explode('_', $key)[1]; |
93
|
|
|
$item = getRepositoryByModuleName($item_name)->getById($item_id); |
|
|
|
|
94
|
|
|
|
95
|
|
|
// Only permissionName existed, do update or create |
96
|
|
|
if ($permissionName) { |
97
|
|
|
$object->grantModuleItemPermission($permissionName, $item); |
98
|
|
|
} else { |
99
|
|
|
$object->revokeModuleItemAllPermissions($item); |
100
|
|
|
} |
101
|
|
|
} elseif (Str::startsWith($key, 'subdomain_access_') && $permissionName) { |
102
|
|
|
array_push($subdomainsAccess, substr($key, strlen('subdomain_access_'))); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$object->subdomains_access = $subdomainsAccess; |
|
|
|
|
107
|
|
|
$object->save(); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.