1
|
|
|
<?php namespace Anomaly\UsersModule\User; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Addon\Extension\ExtensionCollection; |
4
|
|
|
use Anomaly\UsersModule\User\Contract\UserInterface; |
5
|
|
|
use Anomaly\UsersModule\User\Event\SecurityCheckHasFailed; |
6
|
|
|
use Anomaly\UsersModule\User\Security\Contract\SecurityCheckInterface; |
7
|
|
|
use Illuminate\Contracts\Container\Container; |
8
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
9
|
|
|
use Illuminate\Routing\Redirector; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class UserSecurity |
13
|
|
|
* |
14
|
|
|
* @link http://pyrocms.com/ |
15
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
16
|
|
|
* @author Ryan Thompson <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class UserSecurity |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The event dispatcher. |
23
|
|
|
* |
24
|
|
|
* @var Dispatcher |
25
|
|
|
*/ |
26
|
|
|
protected $events; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The redirect service. |
30
|
|
|
* |
31
|
|
|
* @var Redirector |
32
|
|
|
*/ |
33
|
|
|
protected $redirect; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The service container. |
37
|
|
|
* |
38
|
|
|
* @var Container |
39
|
|
|
*/ |
40
|
|
|
protected $container; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The extension collection. |
44
|
|
|
* |
45
|
|
|
* @var ExtensionCollection |
46
|
|
|
*/ |
47
|
|
|
protected $extensions; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Create a new SecurityChecker instance. |
51
|
|
|
* |
52
|
|
|
* @param Dispatcher $events |
53
|
|
|
* @param Redirector $redirect |
54
|
|
|
* @param Container $container |
55
|
|
|
* @param ExtensionCollection $extensions |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
58
|
|
|
Dispatcher $events, |
59
|
|
|
Redirector $redirect, |
60
|
|
|
Container $container, |
61
|
|
|
ExtensionCollection $extensions |
62
|
|
|
) { |
63
|
|
|
$this->events = $events; |
64
|
|
|
$this->redirect = $redirect; |
65
|
|
|
$this->container = $container; |
66
|
|
|
$this->extensions = $extensions; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Check a login attempt. |
71
|
|
|
* |
72
|
|
|
* @return bool|\Illuminate\Http\RedirectResponse|mixed|string |
73
|
|
|
*/ |
74
|
|
View Code Duplication |
public function attempt() |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$extensions = $this->extensions->search('anomaly.module.users::security_check.*'); |
77
|
|
|
|
78
|
|
|
/* @var SecurityCheckInterface $extension */ |
79
|
|
|
foreach ($extensions as $extension) { |
80
|
|
|
|
81
|
|
|
/* |
82
|
|
|
* If the security check does not return |
83
|
|
|
* false then we can assume it passed. |
84
|
|
|
*/ |
85
|
|
|
|
86
|
|
|
$response = $extension->attempt(); |
87
|
|
|
|
88
|
|
|
if ($response === true) { |
89
|
|
|
continue; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->events->fire(new SecurityCheckHasFailed($extension)); |
93
|
|
|
|
94
|
|
|
return $response; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Check authorization. |
102
|
|
|
* |
103
|
|
|
* @param UserInterface $user |
104
|
|
|
* @return bool|\Illuminate\Http\RedirectResponse|mixed|string |
105
|
|
|
*/ |
106
|
|
View Code Duplication |
public function check(UserInterface $user = null) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$extensions = $this->extensions->search('anomaly.module.users::security_check.*'); |
109
|
|
|
|
110
|
|
|
/* @var SecurityCheckInterface $extension */ |
111
|
|
|
foreach ($extensions as $extension) { |
112
|
|
|
|
113
|
|
|
/* |
114
|
|
|
* If the security check does not return |
115
|
|
|
* false then we can assume it passed. |
116
|
|
|
*/ |
117
|
|
|
|
118
|
|
|
$response = $extension->check($user); |
119
|
|
|
|
120
|
|
|
if ($response === true) { |
121
|
|
|
continue; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$this->events->fire(new SecurityCheckHasFailed($extension)); |
125
|
|
|
|
126
|
|
|
return $response; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return true; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
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.