1
|
|
|
<?php namespace Anomaly\UsersModule\User; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Addon\Extension\ExtensionCollection; |
4
|
|
|
use Anomaly\UsersModule\User\Authenticator\Contract\AuthenticatorExtensionInterface; |
5
|
|
|
use Anomaly\UsersModule\User\Contract\UserInterface; |
6
|
|
|
use Anomaly\UsersModule\User\Event\UserWasKickedOut; |
7
|
|
|
use Anomaly\UsersModule\User\Event\UserWasLoggedIn; |
8
|
|
|
use Anomaly\UsersModule\User\Event\UserWasLoggedOut; |
9
|
|
|
use Illuminate\Contracts\Auth\Guard; |
10
|
|
|
use Illuminate\Contracts\Container\Container; |
11
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
12
|
|
|
use Illuminate\Http\RedirectResponse; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class UserAuthenticator |
16
|
|
|
* |
17
|
|
|
* @link http://pyrocms.com/ |
18
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
19
|
|
|
* @author Ryan Thompson <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class UserAuthenticator |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Laravel's authentication. |
26
|
|
|
* |
27
|
|
|
* @var Guard |
28
|
|
|
*/ |
29
|
|
|
protected $guard; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The event dispatcher. |
33
|
|
|
* |
34
|
|
|
* @var Dispatcher |
35
|
|
|
*/ |
36
|
|
|
protected $events; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The service container. |
40
|
|
|
* |
41
|
|
|
* @var Container |
42
|
|
|
*/ |
43
|
|
|
protected $container; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The extension collection. |
47
|
|
|
* |
48
|
|
|
* @var ExtensionCollection |
49
|
|
|
*/ |
50
|
|
|
protected $extensions; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Create a new Authenticator instance. |
54
|
|
|
* |
55
|
|
|
* @param Guard $guard |
56
|
|
|
* @param Dispatcher $events |
57
|
|
|
* @param Container $container |
58
|
|
|
* @param ExtensionCollection $extensions |
59
|
|
|
*/ |
60
|
|
View Code Duplication |
public function __construct(Guard $guard, Dispatcher $events, Container $container, ExtensionCollection $extensions) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$this->guard = $guard; |
63
|
|
|
$this->events = $events; |
64
|
|
|
$this->container = $container; |
65
|
|
|
$this->extensions = $extensions; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Attempt to login a user. |
70
|
|
|
* |
71
|
|
|
* @param array $credentials |
72
|
|
|
* @param bool $remember |
73
|
|
|
* @return bool|UserInterface |
74
|
|
|
*/ |
75
|
|
|
public function attempt(array $credentials, $remember = false) |
76
|
|
|
{ |
77
|
|
|
if ($response = $this->authenticate($credentials)) { |
78
|
|
|
|
79
|
|
|
if ($response instanceof UserInterface) { |
80
|
|
|
$this->login($response, $remember); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $response; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Attempt to authenticate the credentials. |
91
|
|
|
* |
92
|
|
|
* @param array $credentials |
93
|
|
|
* @return bool|UserInterface |
94
|
|
|
*/ |
95
|
|
|
public function authenticate(array $credentials) |
96
|
|
|
{ |
97
|
|
|
$authenticators = $this->extensions->search('anomaly.module.users::authenticator.*'); |
98
|
|
|
|
99
|
|
|
/* @var AuthenticatorExtensionInterface $authenticator */ |
100
|
|
|
foreach ($authenticators as $authenticator) { |
101
|
|
|
|
102
|
|
|
$response = $authenticator->authenticate($credentials); |
103
|
|
|
|
104
|
|
|
if ($response instanceof UserInterface) { |
105
|
|
|
return $response; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if ($response instanceof RedirectResponse) { |
|
|
|
|
109
|
|
|
return $response; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Force login a user. |
118
|
|
|
* |
119
|
|
|
* @param UserInterface $user |
120
|
|
|
* @param bool $remember |
121
|
|
|
*/ |
122
|
|
|
public function login(UserInterface $user, $remember = false) |
123
|
|
|
{ |
124
|
|
|
$this->guard->login($user, $remember); |
125
|
|
|
|
126
|
|
|
$this->events->fire(new UserWasLoggedIn($user)); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Logout a user. |
131
|
|
|
* |
132
|
|
|
* @param UserInterface $user |
133
|
|
|
*/ |
134
|
|
|
public function logout(UserInterface $user = null) |
135
|
|
|
{ |
136
|
|
|
if (!$user) { |
137
|
|
|
$user = $this->guard->user(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if (!$user) { |
141
|
|
|
return; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$this->guard->logout($user); |
145
|
|
|
|
146
|
|
|
$this->events->fire(new UserWasLoggedOut($user)); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Kick out a user. They've been bad. |
151
|
|
|
* |
152
|
|
|
* @param UserInterface $user |
153
|
|
|
*/ |
154
|
|
|
public function kickOut(UserInterface $user, $reason) |
155
|
|
|
{ |
156
|
|
|
$this->guard->logout($user); |
157
|
|
|
|
158
|
|
|
$this->events->fire(new UserWasKickedOut($user, $reason)); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
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.