Completed
Push — master ( 7e2d40...45edf2 )
by Ryan
05:37
created

Authorizer   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 244
Duplicated Lines 26.23 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 64
loc 244
rs 8.8
c 0
b 0
f 0
wmc 36
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B authorize() 0 20 6
B authorizeAny() 18 18 5
B authorizeAll() 18 18 5
D checkPermission() 28 86 17
A getGuest() 0 4 1
A setGuest() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Anomaly\Streams\Platform\Support;
2
3
use Anomaly\UsersModule\Role\Contract\RoleInterface;
4
use Anomaly\UsersModule\User\Contract\UserInterface;
5
use Illuminate\Config\Repository;
6
use Illuminate\Contracts\Auth\Guard;
7
use Illuminate\Http\Request;
8
9
class Authorizer
10
{
11
12
    /**
13
     * The auth utility.
14
     *
15
     * @var null|Guard
16
     */
17
    protected $guard = null;
18
19
    /**
20
     * The guest role.
21
     *
22
     * @var RoleInterface
23
     */
24
    protected $guest;
25
26
    /**
27
     * The config repository.
28
     *
29
     * @var Repository
30
     */
31
    protected $config;
32
33
    /**
34
     * The request object.
35
     *
36
     * @var Request
37
     */
38
    protected $request;
39
40
    /**
41
     * Create a new Authorizer instance.
42
     *
43
     * @param Guard      $guard
44
     * @param Repository $config
45
     * @param Request    $request
46
     */
47
    public function __construct(Guard $guard, Repository $config, Request $request)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
48
    {
49
        $this->guard   = $guard;
50
        $this->config  = $config;
51
        $this->request = $request;
52
    }
53
54
    /**
55
     * Authorize a user against a permission.
56
     *
57
     * @param                $permission
58
     * @param  UserInterface $user
59
     * @return bool
60
     */
61
    public function authorize($permission, UserInterface $user = null)
62
    {
63
        if (!$user) {
64
            $user = $this->guard->user();
65
        }
66
67
        if (!$user) {
68
            $user = $this->request->user();
69
        }
70
71
        if (!$user && $guest = $this->getGuest()) {
72
            return $guest->hasPermission($permission);
73
        }
74
75
        if (!$user) {
76
            return false;
77
        }
78
79
        return $this->checkPermission($permission, $user);
80
    }
81
82
    /**
83
     * Authorize a user against any permission.
84
     *
85
     * @param  array         $permissions
86
     * @param  UserInterface $user
87
     * @param  bool          $strict
88
     * @return bool
89
     */
90 View Code Duplication
    public function authorizeAny(array $permissions, UserInterface $user = null, $strict = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92
        if (!$user) {
93
            $user = $this->guard->user();
94
        }
95
96
        if (!$user) {
97
            return !$strict;
98
        }
99
100
        foreach ($permissions as $permission) {
101
            if ($this->checkPermission($permission, $user)) {
102
                return true;
103
            }
104
        }
105
106
        return false;
107
    }
108
109
    /**
110
     * Authorize a user against all permission.
111
     *
112
     * @param  array         $permissions
113
     * @param  UserInterface $user
114
     * @param  bool          $strict
115
     * @return bool
116
     */
117 View Code Duplication
    public function authorizeAll(array $permissions, UserInterface $user = null, $strict = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
    {
119
        if (!$user) {
120
            $user = $this->guard->user();
121
        }
122
123
        if (!$user) {
124
            return !$strict;
125
        }
126
127
        foreach ($permissions as $permission) {
128
            if (!$this->checkPermission($permission, $user)) {
129
                return false;
130
            }
131
        }
132
133
        return true;
134
    }
135
136
    /**
137
     * Return a user's permission.
138
     *
139
     * @param                $permission
140
     * @param  UserInterface $user
141
     * @return bool
142
     */
143
    protected function checkPermission($permission, UserInterface $user)
144
    {
145
        /*
146
         * No permission, let it proceed.
147
         */
148
        if (!$permission) {
149
            return true;
150
        }
151
152
        /*
153
         * If the permission does not actually exist
154
         * then we cant really do anything with it.
155
         */
156
        if (str_is('*::*.*', $permission) && !ends_with($permission, '*')) {
157
            $parts = explode('.', str_replace('::', '.', $permission));
158
            $end   = array_pop($parts);
159
            $group = array_pop($parts);
160
            $parts = explode('::', $permission);
161
162
            // If it does not exist, we are authorized.
163
            if (!in_array($end, (array)$this->config->get($parts[0] . '::permissions.' . $group))) {
164
                return true;
165
            }
166
        } elseif (ends_with($permission, '*')) {
167
            $parts = explode('::', $permission);
168
169
            $addon = array_shift($parts);
170
171
            /*
172
             * Check vendor.module.slug::group.*
173
             * then check vendor.module.slug::*
174
             */
175
            if (str_is('*.*.*::*.*.*', $permission)) {
176
                $end = trim(substr($permission, strpos($permission, '::') + 2), '.*');
177
178
                if (!$permissions = $this->config->get($addon . '::permissions.' . $end)) {
179
                    return true;
180
                } else {
181
                    return $user->hasAnyPermission($permissions);
182
                }
183
            } elseif (str_is('*.*.*::*.*', $permission)) {
184
                $end = trim(substr($permission, strpos($permission, '::') + 2), '.*');
185
186 View Code Duplication
                if (!$permissions = $this->config->get($addon . '::permissions.' . $end)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
187
                    return true;
188
                } else {
189
                    $check = [];
190
191
                    foreach ($permissions as &$permission) {
192
                        $check[] = $addon . '::' . $end . '.' . $permission;
193
                    }
194
195
                    return $user->hasAnyPermission($check);
196
                }
197 View Code Duplication
            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
198
                if (!$permissions = $this->config->get($addon . '::permissions')) {
199
                    return true;
200
                } else {
201
                    $check = [];
202
203
                    foreach ($permissions as $group => &$permission) {
204
                        foreach ($permission as $access) {
205
                            $check[] = $addon . '::' . $group . '.' . $access;
206
                        }
207
                    }
208
209
                    return $user->hasAnyPermission($check);
210
                }
211
            }
212
        } else {
213
            $parts = explode('::', $permission);
214
215
            $end = array_pop($parts);
216
217
            if (!in_array($end, (array)$this->config->get($parts[0] . '::permissions'))) {
218
                return true;
219
            }
220
        }
221
222
        // Check if the user actually has permission.
223
        if (!$user || !$user->hasPermission($permission)) {
224
            return false;
225
        }
226
227
        return true;
228
    }
229
230
    /**
231
     * Get the guest role.
232
     *
233
     * @return RoleInterface
234
     */
235
    public function getGuest()
236
    {
237
        return $this->guest;
238
    }
239
240
    /**
241
     * Set the guest role.
242
     *
243
     * @param  RoleInterface $guest
244
     * @return $this
245
     */
246
    public function setGuest(RoleInterface $guest)
247
    {
248
        $this->guest = $guest;
249
250
        return $this;
251
    }
252
}
253