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