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

PermissionAuthServiceProvider::boot()   D

Complexity

Conditions 30
Paths 1

Size

Total Lines 173
Code Lines 88

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 30
eloc 88
nc 1
nop 0
dl 0
loc 173
rs 4.1666
c 3
b 0
f 0

How to fix   Long Method    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;
4
5
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
6
use Illuminate\Support\Facades\Config;
7
use Illuminate\Support\Facades\Gate;
8
9
class PermissionAuthServiceProvider extends ServiceProvider
10
{
11
    protected static $cache = [];
12
13
    protected function authorize($user, $callback, $moduleName = null)
0 ignored issues
show
Unused Code introduced by
The parameter $moduleName is not used and could be removed. ( Ignorable by Annotation )

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

13
    protected function authorize($user, $callback, /** @scrutinizer ignore-unused */ $moduleName = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
14
    {
15
        if ($user->is_superadmin) {
16
            return true;
17
        }
18
19
        if (!$user->published) {
20
            return false;
21
        }
22
23
        return $callback($user);
24
    }
25
26
    public function boot()
27
    {
28
        /***
29
         *
30
         *    Global permissions
31
         *
32
         ***/
33
34
        Gate::define('edit-settings', function ($user) {
35
            return $this->authorize($user, function ($user) {
36
                return $user->role->permissions()->global()->where('name', 'edit-settings')->exists();
37
            });
38
        });
39
40
        Gate::define('edit-users', function ($user) {
41
            return $this->authorize($user, function ($user) {
42
                return $user->role->permissions()->global()->where('name', 'edit-users')->exists();
43
            });
44
        });
45
46
        Gate::define('edit-user-role', function ($user) {
47
            return $this->authorize($user, function ($user) {
48
                return $user->role->permissions()->global()->where('name', 'edit-user-role')->exists();
49
            });
50
        });
51
52
        Gate::define('edit-user-groups', function ($user) {
53
            if (!in_array(Config::get('twill.permissions.level'), ['roleGroup', 'roleGroupModule'])) {
54
                return false;
55
            }
56
57
            return $this->authorize($user, function ($user) {
58
                return $user->role->permissions()->global()->where('name', 'edit-user-groups')->exists();
59
            });
60
        });
61
62
        Gate::define('access-user-management', function ($user) {
63
            return $this->authorize($user, function ($user) {
64
                return $user->can('edit-users') || $user->can('edit-user-role') || $user->can('edit-user-groups');
65
            });
66
        });
67
68
        Gate::define('manage-modules', function ($user) {
69
            if (isset(self::$cache['manage-modules'])) {
70
                return self::$cache['manage-modules'];
71
            }
72
73
            return self::$cache['manage-modules'] = $this->authorize($user, function ($user) {
74
                return $user->role->permissions()->global()->where('name', 'manage-modules')->exists()
75
                || isUserGroupPermissionModuleExists($user, 'global', 'manage-modules');
76
            });
77
        });
78
79
        Gate::define('access-media-library', function ($user) {
80
            return $this->authorize($user, function ($user) {
81
                return $user->role->permissions()->global()->where('name', 'access-media-library')->exists();
82
            });
83
        });
84
85
        Gate::define('edit-media-library', function ($user) {
86
            return $this->authorize($user, function ($user) {
87
                return $user->role->permissions()->global()->where('name', 'edit-media-library')->exists();
88
            });
89
        });
90
91
        Gate::define('impersonate', function ($user) {
92
            return $this->authorize($user, function ($user) {
93
                return $user->is_superadmin;
94
            });
95
        });
96
97
        /***
98
         *
99
         *    Module permissions
100
         *
101
         ***/
102
103
        Gate::define('access-module-list', function ($user, $moduleName) {
104
            if (isset(self::$cache['access-module-list-' . $moduleName])) {
105
                return self::$cache['access-module-list-' . $moduleName];
106
            }
107
108
            return self::$cache['access-module-list-' . $moduleName] = $this->authorize($user, function ($user) use ($moduleName) {
109
                return $user->can('view-module', $moduleName)
110
                || $user->allPermissions()->ofModuleName($moduleName)->exists();
111
            });
112
        });
113
114
        // The gate of accessing module list page,
115
        Gate::define('view-module', function ($user, $moduleName) {
116
            if (isset(self::$cache['view-module-' . $moduleName])) {
117
                return self::$cache['view-module-' . $moduleName];
118
            }
119
120
            return self::$cache['view-module-' . $moduleName] = $this->authorize($user, function ($user) use ($moduleName) {
121
                return $user->can('edit-module', $moduleName)
122
                || $user->role->permissions()->ofModuleName($moduleName)->where('name', 'view-module')->exists()
123
                || isUserGroupPermissionModuleExists($user, $moduleName, 'view-module');
124
            });
125
        });
126
127
        Gate::define('edit-module', function ($user, $moduleName) {
128
            if (isset(self::$cache['edit-module-' . $moduleName])) {
129
                return self::$cache['edit-module-' . $moduleName];
130
            }
131
132
            return self::$cache['edit-module-' . $moduleName] = $this->authorize($user, function ($user) use ($moduleName) {
133
                return $user->can('manage-module', $moduleName)
134
                || $user->role->permissions()->module()->ofModuleName($moduleName)->where('name', 'edit-module')->exists()
135
                || isUserGroupPermissionModuleExists($user, $moduleName, 'edit-module');
136
            });
137
        });
138
139
        Gate::define('manage-module', function ($user, $moduleName) {
140
            if (isset(self::$cache['manage-module-' . $moduleName])) {
141
                return self::$cache['manage-module-' . $moduleName];
142
            }
143
144
            return self::$cache['manage-module-' . $moduleName] = $this->authorize($user, function ($user) use ($moduleName) {
145
                if (!isPermissionableModule($moduleName)) {
146
                    return true;
147
                }
148
                return $user->can('manage-modules')
149
                || $user->role->permissions()->module()->ofModuleName($moduleName)->where('name', 'manage-module')->exists()
150
                || isUserGroupPermissionModuleExists($user, $moduleName, 'manage-module');
151
            });
152
        });
153
154
        /***
155
         *
156
         *    Module item permissions
157
         *
158
         ***/
159
160
        Gate::define('view-item', function ($user, $item) {
161
            $key = 'view-item-' . str_replace("\\", "-", get_class($item)) . '-' . $item->id;
162
            if (isset(self::$cache[$key])) {
163
                return self::$cache[$key];
164
            }
165
166
            return self::$cache[$key] = $this->authorize($user, function ($user) use ($item) {
167
                return $item->public
168
                || $user->can('edit-item', $item)
169
                || $user->can('view-module', getModuleNameByModel(get_class($item)))
170
                || $user->permissions()->ofItem($item)->where('name', 'view-item')->exists()
171
                || isUserGroupPermissionItemExists($user, $item, 'view-item');
172
            });
173
        });
174
175
        Gate::define('edit-item', function ($user, $item) {
176
            $key = 'edit-item-' . str_replace("\\", "-", get_class($item)) . '-' . $item->id;
177
            if (isset(self::$cache[$key])) {
178
                return self::$cache[$key];
179
            }
180
181
            return self::$cache[$key] = $this->authorize($user, function ($user) use ($item) {
182
                return $user->can('manage-item', $item)
183
                || $user->can('edit-module', getModuleNameByModel(get_class($item)))
184
                || $user->permissions()->ofItem($item)->where('name', 'edit-item')->exists()
185
                || isUserGroupPermissionItemExists($user, $item, 'edit-item');
186
            });
187
        });
188
189
        Gate::define('manage-item', function ($user, $item) {
190
            $key = 'manage-item-' . str_replace("\\", "-", get_class($item)) . '-' . $item->id;
191
            if (isset(self::$cache[$key])) {
192
                return self::$cache[$key];
193
            }
194
195
            return self::$cache[$key] = $this->authorize($user, function ($user) use ($item) {
196
                return $user->can('manage-module', getModuleNameByModel(get_class($item)))
197
                || $user->permissions()->ofItem($item)->where('name', 'manage-item')->exists()
198
                || isUserGroupPermissionItemExists($user, $item, 'manage-item');
199
            });
200
        });
201
    }
202
}
203