Passed
Push — feature/permission-manager ( 41e93d )
by
unknown
09:00
created

getFormFieldsHandleGroupPermissions()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 8
eloc 17
c 3
b 0
f 0
nc 6
nop 2
dl 0
loc 32
rs 8.4444
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) {
0 ignored issues
show
Bug introduced by
The call to Illuminate\Support\Collection::get() has too few arguments starting with key. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

23
            foreach ($object->permissions()->global()->/** @scrutinizer ignore-call */ get()->pluck('name') as $permissionName) {

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.

Loading history...
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) {
0 ignored issues
show
Bug introduced by
The property subdomains_access does not seem to exist on A17\Twill\Models\Model. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method getById() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
                $item = getRepositoryByModuleName($item_name)->/** @scrutinizer ignore-call */ getById($item_id);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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;
0 ignored issues
show
Bug introduced by
The property subdomains_access does not seem to exist on A17\Twill\Models\Model. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
107
        $object->save();
108
    }
109
}
110