1
|
|
|
<?php namespace Anomaly\UsersModule\Http\Middleware; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Message\MessageBag; |
4
|
|
|
use Anomaly\Streams\Platform\Support\Authorizer; |
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Contracts\Auth\Guard; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Illuminate\Routing\Redirector; |
9
|
|
|
use Illuminate\Routing\Route; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class AuthorizeRoutePermission |
13
|
|
|
* |
14
|
|
|
* @link http://pyrocms.com/ |
15
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
16
|
|
|
* @author Ryan Thompson <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class AuthorizeRoutePermission |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The route object. |
23
|
|
|
* |
24
|
|
|
* @var Route |
25
|
|
|
*/ |
26
|
|
|
protected $route; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The redirect utility. |
30
|
|
|
* |
31
|
|
|
* @var Redirector |
32
|
|
|
*/ |
33
|
|
|
protected $redirect; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The message bag. |
37
|
|
|
* |
38
|
|
|
* @var MessageBag |
39
|
|
|
*/ |
40
|
|
|
protected $messages; |
41
|
|
|
/** |
42
|
|
|
* The authorizer utility. |
43
|
|
|
* |
44
|
|
|
* @var Authorizer |
45
|
|
|
*/ |
46
|
|
|
protected $authorizer; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Create a new AuthorizeModuleAccess instance. |
50
|
|
|
* |
51
|
|
|
* @param Route $route |
52
|
|
|
* @param Redirector $redirect |
53
|
|
|
* @param MessageBag $messages |
54
|
|
|
* @param Authorizer $authorizer |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
57
|
|
|
Guard $auth, |
|
|
|
|
58
|
|
|
Route $route, |
59
|
|
|
Redirector $redirect, |
60
|
|
|
MessageBag $messages, |
61
|
|
|
Authorizer $authorizer |
62
|
|
|
) { |
63
|
|
|
$this->route = $route; |
64
|
|
|
$this->redirect = $redirect; |
65
|
|
|
$this->messages = $messages; |
66
|
|
|
$this->authorizer = $authorizer; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Check the authorization of module access. |
71
|
|
|
* |
72
|
|
|
* @param Request $request |
73
|
|
|
* @param \Closure $next |
74
|
|
|
* @return \Illuminate\Http\RedirectResponse |
75
|
|
|
*/ |
76
|
|
|
public function handle(Request $request, Closure $next) |
77
|
|
|
{ |
78
|
|
|
if (in_array($request->path(), ['admin/login', 'admin/logout'])) { |
79
|
|
|
return $next($request); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ($request->segment(1) == 'admin' && !$this->authorizer->authorize( |
83
|
|
|
'anomaly.module.users::general.control_panel' |
84
|
|
|
) |
85
|
|
|
) { |
86
|
|
|
abort(403); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$permission = array_get($this->route->getAction(), 'anomaly.module.users::permission'); |
90
|
|
|
$redirect = array_get($this->route->getAction(), 'anomaly.module.users::redirect'); |
91
|
|
|
$message = array_get($this->route->getAction(), 'anomaly.module.users::message'); |
92
|
|
|
|
93
|
|
|
if ($permission && !$this->authorizer->authorizeAny((array)$permission, null, true)) { |
94
|
|
|
|
95
|
|
|
if ($message) { |
96
|
|
|
$this->messages->error($message); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($redirect) { |
100
|
|
|
return $this->redirect->to($redirect); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
abort(403); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $next($request); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
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.