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

PermissionAuthServiceProvider::boot()   D

Complexity

Conditions 43
Paths 1

Size

Total Lines 193
Code Lines 99

Duplication

Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 0 Features 0
Metric Value
cc 43
eloc 99
nc 1
nop 0
dl 0
loc 193
rs 4.1666
c 9
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 static $useCache = true;
14
15
    /**
16
     * Disable gate caching for integration tests
17
     */
18
    public static function disableCache()
19
    {
20
        self::$useCache = false;
21
    }
22
23
    /**
24
     * For compatibility with legacy AuthServiceProvider
25
     */
26
    protected function define($ability, $callback)
27
    {
28
        Gate::define($ability, $callback);
29
    }
30
31
    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

31
    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...
32
    {
33
        if ($user->is_superadmin) {
34
            return true;
35
        }
36
37
        if (!$user->published) {
38
            return false;
39
        }
40
41
        return $callback($user);
42
    }
43
44
    public function boot()
45
    {
46
        /***
47
         *
48
         *    Global permissions
49
         *
50
         ***/
51
52
        $this->define('edit-settings', function ($user) {
53
            return $this->authorize($user, function ($user) {
54
                return $user->role->permissions()->global()->where('name', 'edit-settings')->exists();
55
            });
56
        });
57
58
        $this->define('edit-users', function ($user) {
59
            return $this->authorize($user, function ($user) {
60
                return $user->role->permissions()->global()->where('name', 'edit-users')->exists();
61
            });
62
        });
63
64
        $this->define('edit-user', function ($user, $editedUser) {
65
            return $this->authorize($user, function ($user) use ($editedUser) {
66
                return ($user->id === $editedUser->id)
67
                    || ($editedUser->role->position >= $user->role->position && $user->can('edit-users'));
68
            });
69
        });
70
71
        $this->define('edit-user-roles', function ($user) {
72
            return $this->authorize($user, function ($user) {
73
                return $user->role->permissions()->global()->where('name', 'edit-user-roles')->exists();
74
            });
75
        });
76
77
        $this->define('edit-role', function ($user, $editedRole) {
78
            return $this->authorize($user, function ($user) use ($editedRole) {
79
                return ($editedRole->position >= $user->role->position) && $user->can('edit-user-roles');
80
            });
81
        });
82
83
        $this->define('edit-user-groups', function ($user) {
84
            if (!in_array(Config::get('twill.permissions.level'), ['roleGroup', 'roleGroupItem'])) {
85
                return false;
86
            }
87
88
            return $this->authorize($user, function ($user) {
89
                return $user->role->permissions()->global()->where('name', 'edit-user-groups')->exists();
90
            });
91
        });
92
93
        $this->define('edit-group', function ($user, $editedGroup) {
94
            return $this->authorize($user, function ($user) use ($editedGroup) {
95
                return !$editedGroup->isEveryoneGroup() && $user->can('edit-user-groups');
96
            });
97
        });
98
99
        $this->define('access-user-management', function ($user) {
100
            return $this->authorize($user, function ($user) {
101
                return $user->can('edit-users') || $user->can('edit-user-roles') || $user->can('edit-user-groups');
102
            });
103
        });
104
105
        $this->define('manage-modules', function ($user) {
106
            if (self::$useCache && isset(self::$cache['manage-modules'])) {
107
                return self::$cache['manage-modules'];
108
            }
109
110
            return self::$cache['manage-modules'] = $this->authorize($user, function ($user) {
111
                return $user->role->permissions()->global()->where('name', 'manage-modules')->exists()
112
                || isUserGroupPermissionModuleExists($user, 'global', 'manage-modules');
113
            });
114
        });
115
116
        $this->define('access-media-library', function ($user) {
117
            return $this->authorize($user, function ($user) {
118
                return $user->can('edit-media-library')
119
                || $user->role->permissions()->global()->where('name', 'access-media-library')->exists();
120
            });
121
        });
122
123
        $this->define('edit-media-library', function ($user) {
124
            return $this->authorize($user, function ($user) {
125
                return $user->role->permissions()->global()->where('name', 'edit-media-library')->exists();
126
            });
127
        });
128
129
        $this->define('impersonate', function ($user) {
130
            return $this->authorize($user, function ($user) {
131
                return $user->is_superadmin;
132
            });
133
        });
134
135
        /***
136
         *
137
         *    Module permissions
138
         *
139
         ***/
140
141
        $this->define('access-module-list', function ($user, $moduleName) {
142
            if (self::$useCache && isset(self::$cache['access-module-list-' . $moduleName])) {
143
                return self::$cache['access-module-list-' . $moduleName];
144
            }
145
146
            return self::$cache['access-module-list-' . $moduleName] = $this->authorize($user, function ($user) use ($moduleName) {
147
                return $user->can('view-module', $moduleName)
148
                || $user->allPermissions()->ofModuleName($moduleName)->exists();
149
            });
150
        });
151
152
        // The gate of accessing module list page,
153
        $this->define('view-module', function ($user, $moduleName) {
154
            if (self::$useCache && isset(self::$cache['view-module-' . $moduleName])) {
155
                return self::$cache['view-module-' . $moduleName];
156
            }
157
158
            return self::$cache['view-module-' . $moduleName] = $this->authorize($user, function ($user) use ($moduleName) {
159
                return $user->can('edit-module', $moduleName)
160
                || $user->role->permissions()->ofModuleName($moduleName)->where('name', 'view-module')->exists()
161
                || isUserGroupPermissionModuleExists($user, $moduleName, 'view-module');
162
            });
163
        });
164
165
        $this->define('edit-module', function ($user, $moduleName) {
166
            if (self::$useCache && isset(self::$cache['edit-module-' . $moduleName])) {
167
                return self::$cache['edit-module-' . $moduleName];
168
            }
169
170
            return self::$cache['edit-module-' . $moduleName] = $this->authorize($user, function ($user) use ($moduleName) {
171
                return $user->can('manage-module', $moduleName)
172
                || $user->role->permissions()->module()->ofModuleName($moduleName)->where('name', 'edit-module')->exists()
173
                || isUserGroupPermissionModuleExists($user, $moduleName, 'edit-module');
174
            });
175
        });
176
177
        $this->define('manage-module', function ($user, $moduleName) {
178
            if (self::$useCache && isset(self::$cache['manage-module-' . $moduleName])) {
179
                return self::$cache['manage-module-' . $moduleName];
180
            }
181
182
            return self::$cache['manage-module-' . $moduleName] = $this->authorize($user, function ($user) use ($moduleName) {
183
                if (!isPermissionableModule($moduleName)) {
184
                    return true;
185
                }
186
                return $user->can('manage-modules')
187
                || $user->role->permissions()->module()->ofModuleName($moduleName)->where('name', 'manage-module')->exists()
188
                || isUserGroupPermissionModuleExists($user, $moduleName, 'manage-module');
189
            });
190
        });
191
192
        /***
193
         *
194
         *    Module item permissions
195
         *
196
         ***/
197
198
        $this->define('view-item', function ($user, $item) {
199
            $key = 'view-item-' . str_replace("\\", "-", get_class($item)) . '-' . $item->id;
200
            if (self::$useCache && isset(self::$cache[$key])) {
201
                return self::$cache[$key];
202
            }
203
204
            return self::$cache[$key] = $this->authorize($user, function ($user) use ($item) {
205
                return $item->public
206
                || $user->can('edit-item', $item)
207
                || $user->can('view-module', getModuleNameByModel(get_class($item)))
208
                || $user->permissions()->ofItem($item)->where('name', 'view-item')->exists()
209
                || isUserGroupPermissionItemExists($user, $item, 'view-item');
210
            });
211
        });
212
213
        $this->define('edit-item', function ($user, $item) {
214
            $key = 'edit-item-' . str_replace("\\", "-", get_class($item)) . '-' . $item->id;
215
            if (self::$useCache && isset(self::$cache[$key])) {
216
                return self::$cache[$key];
217
            }
218
219
            return self::$cache[$key] = $this->authorize($user, function ($user) use ($item) {
220
                return $user->can('manage-item', $item)
221
                || $user->can('edit-module', getModuleNameByModel(get_class($item)))
222
                || $user->permissions()->ofItem($item)->where('name', 'edit-item')->exists()
223
                || isUserGroupPermissionItemExists($user, $item, 'edit-item');
224
            });
225
        });
226
227
        $this->define('manage-item', function ($user, $item) {
228
            $key = 'manage-item-' . str_replace("\\", "-", get_class($item)) . '-' . $item->id;
229
            if (self::$useCache && isset(self::$cache[$key])) {
230
                return self::$cache[$key];
231
            }
232
233
            return self::$cache[$key] = $this->authorize($user, function ($user) use ($item) {
234
                return $user->can('manage-module', getModuleNameByModel(get_class($item)))
235
                || $user->permissions()->ofItem($item)->where('name', 'manage-item')->exists()
236
                || isUserGroupPermissionItemExists($user, $item, 'manage-item');
237
            });
238
        });
239
    }
240
}
241