Passed
Pull Request — 2.x (#1049)
by
unknown
05:53
created

getFormFieldsHandleGroupPermissions()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 15
nc 6
nop 2
dl 0
loc 25
rs 8.8333
c 1
b 0
f 0
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::get('twill.permissions.level') == 'roleGroup') {
0 ignored issues
show
Bug introduced by
The type A17\Twill\Repositories\Behaviors\Config was not found. Did you mean Config? If so, make sure to prefix the type with \.
Loading history...
22
            foreach (Permission::permissionableModules() as $moduleName) {
23
                $modulePermission = $object->permissions()->module()->ofModuleName($moduleName)->first();
24
                if ($modulePermission) {
25
                    $fields['module_' . $moduleName . '_permissions'] = $modulePermission->name;
26
                } else {
27
                    $fields['module_' . $moduleName . '_permissions'] = 'none';
28
                }
29
            }
30
        } elseif (Config::get('twill.permissions.level') == 'roleGroupModule') {
31
            // looking for item permissions
32
            foreach ($object->permissions()->moduleItem()->get() as $permission) {
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

32
            foreach ($object->permissions()->moduleItem()->/** @scrutinizer ignore-call */ get() as $permission) {

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...
33
                $model = $permission->permissionable()->first();
34
                $moduleName = getModuleNameByModel($model);
35
                $fields[$moduleName . '_' . $model->id . '_permission'] = $permission->name;
36
            }
37
        }
38
39
        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...
40
            $fields['subdomain_access_' . $subdomain] = true;
41
        }
42
43
        return $fields;
44
    }
45
46
    /**
47
     * Function executed after save on group form
48
     *
49
     * @param Model|Group $object
50
     * @param array $fields
51
     */
52
    public function afterSaveHandleGroupPermissions($object, $fields)
53
    {
54
        foreach (Permission::available(Permission::SCOPE_GLOBAL) as $permissionName) {
55
            if (isset($fields[$permissionName]) && $fields[$permissionName] === true) {
56
                $object->grantGlobalPermission($permissionName);
57
            } else {
58
                $object->revokeGlobalPermission($permissionName);
59
            }
60
        }
61
62
        $subdomainsAccess = [];
63
64
        foreach ($fields as $key => $permissionName) {
65
            // Used for the roleGroup mode
66
            if (Str::startsWith($key, 'module_') && Str::endsWith($key, '_permissions')) {
67
                $modulePermissions = Permission::available(Permission::SCOPE_MODULE);
68
                $model = getModelByModuleName($moduleName = explode('_', $key)[1]);
69
70
                $currentPermission = $object->permissions()
71
                    ->where('permissionable_type', $model)
72
                    ->whereIn('name', $modulePermissions)
73
                    ->first()
74
                ;
75
76
                if (!$currentPermission || $permissionName != $currentPermission->name) {
77
                    $object->revokeAllModulePermission($model);
78
                    if (in_array($permissionName, $modulePermissions)) {
79
                        $object->grantModulePermission($permissionName, $model);
80
                    }
81
                }
82
            } elseif (Str::endsWith($key, '_permission')) {
83
                $item_name = explode('_', $key)[0];
84
                $item_id = explode('_', $key)[1];
85
                $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

85
                $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...
86
87
                // Only permissionName existed, do update or create
88
                if ($permissionName) {
89
                    $object->grantModuleItemPermission($permissionName, $item);
90
                } else {
91
                    $object->revokeModuleItemAllPermissions($item);
92
                }
93
            } elseif (Str::startsWith($key, 'subdomain_access_') && $permissionName) {
94
                array_push($subdomainsAccess, substr($key, strlen('subdomain_access_')));
95
            }
96
        }
97
98
        $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...
99
        $object->save();
100
    }
101
}
102