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

HandleRolePermissions::updateRolePermissions()   B

Complexity

Conditions 10
Paths 15

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 18
c 0
b 0
f 0
nc 15
nop 2
dl 0
loc 30
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
0 ignored issues
show
Bug introduced by
The type A17\Twill\Repositories\Behaviors\Model was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
     * @param array $fields
16
     * @return array
17
     */
18
    public function getFormFieldsHandleRolePermissions($object, $fields)
19
    {
20
        $object->permissions()->get();
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

20
        $object->permissions()->/** @scrutinizer ignore-call */ get();

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...
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