|
1
|
|
|
<?php namespace Anomaly\UsersModule\Http\Middleware; |
|
2
|
|
|
|
|
3
|
|
|
use Anomaly\UsersModule\User\UserSecurity; |
|
4
|
|
|
use Closure; |
|
5
|
|
|
use Illuminate\Contracts\Auth\Guard; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
use Illuminate\Routing\Redirector; |
|
8
|
|
|
use Illuminate\Routing\ResponseFactory; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class CheckSecurityRequest |
|
13
|
|
|
* |
|
14
|
|
|
* This class replaces the Laravel version in app/ |
|
15
|
|
|
* |
|
16
|
|
|
* @link http://pyrocms.com/ |
|
17
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
|
18
|
|
|
* @author Ryan Thompson <[email protected]> |
|
19
|
|
|
* @package Anomaly\UsersModule\Http\Middleware |
|
20
|
|
|
*/ |
|
21
|
|
|
class CheckSecurity |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The Guard implementation. |
|
26
|
|
|
* |
|
27
|
|
|
* @var Guard |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $guard; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The security utility. |
|
33
|
|
|
* |
|
34
|
|
|
* @var UserSecurity |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $security; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The response factory. |
|
40
|
|
|
* |
|
41
|
|
|
* @var ResponseFactory |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $response; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* The redirector utility. |
|
47
|
|
|
* |
|
48
|
|
|
* @var Redirector |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $redirect; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Create a new CheckSecurityRequest instance. |
|
54
|
|
|
* |
|
55
|
|
|
* @param Guard $guard |
|
56
|
|
|
* @param Redirector $redirect |
|
57
|
|
|
* @param ResponseFactory $response |
|
58
|
|
|
* @param UserSecurity $security |
|
59
|
|
|
*/ |
|
60
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
|
|
61
|
|
|
Guard $guard, |
|
62
|
|
|
Redirector $redirect, |
|
63
|
|
|
ResponseFactory $response, |
|
64
|
|
|
UserSecurity $security |
|
65
|
|
|
) { |
|
66
|
|
|
$this->guard = $guard; |
|
67
|
|
|
$this->redirect = $redirect; |
|
68
|
|
|
$this->response = $response; |
|
69
|
|
|
$this->security = $security; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Handle an incoming request. |
|
74
|
|
|
* |
|
75
|
|
|
* @param \Illuminate\Http\Request $request |
|
76
|
|
|
* @param \Closure $next |
|
77
|
|
|
* @return mixed |
|
78
|
|
|
*/ |
|
79
|
|
|
public function handle(Request $request, Closure $next) |
|
80
|
|
|
{ |
|
81
|
|
|
$response = $this->security->check($this->guard->user()); |
|
82
|
|
|
|
|
83
|
|
|
if ($response instanceof Response) { |
|
|
|
|
|
|
84
|
|
|
return $response; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $next($request); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
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.