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