1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Laravel Platfourm package. |
4
|
|
|
* |
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Longman\Platfourm\Auth\Services; |
12
|
|
|
|
13
|
|
|
use Illuminate\Contracts\Auth\Factory as AuthContract; |
14
|
|
|
use Illuminate\Contracts\Config\Repository as ConfigContract; |
15
|
|
|
use Illuminate\Foundation\Auth\ThrottlesLogins; |
16
|
|
|
use Illuminate\Http\Request; |
17
|
|
|
use InvalidArgumentException; |
18
|
|
|
use Longman\Platfourm\Auth\Exceptions\AuthException; |
19
|
|
|
use Longman\Platfourm\Auth\Exceptions\ForbiddenException; |
20
|
|
|
use Longman\Platfourm\Auth\Exceptions\UnauthorizedException; |
21
|
|
|
use Longman\Platfourm\Contracts\Auth\AuthUserService as AuthUserServiceContract; |
22
|
|
|
use Longman\Platfourm\User\Models\Eloquent\User; |
23
|
|
|
use Longman\Platfourm\User\Repositories\Eloquent\UserRepository; |
24
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface as SessionContract; |
25
|
|
|
|
26
|
|
|
class AuthUserService implements AuthUserServiceContract |
27
|
|
|
{ |
28
|
|
|
use ThrottlesLogins; |
29
|
|
|
|
30
|
|
|
private $user = null; |
31
|
|
|
private $guard = null; |
32
|
|
|
|
33
|
|
|
private $repository; |
34
|
|
|
private $auth; |
35
|
|
|
private $config; |
36
|
|
|
|
37
|
|
|
public function __construct( |
38
|
|
|
UserRepository $repository, |
39
|
|
|
AuthContract $auth, |
40
|
|
|
ConfigContract $config |
41
|
|
|
) { |
42
|
|
|
$this->repository = $repository; |
43
|
|
|
$this->auth = $auth; |
44
|
|
|
$this->config = $config; |
45
|
|
|
$this->setUser(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function setGuard($guard) |
49
|
|
|
{ |
50
|
|
|
$this->guard = $guard; |
51
|
|
|
$this->setUser(); |
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function setUser() |
56
|
|
|
{ |
57
|
|
|
$this->user = $this->auth->guard($this->guard)->user(); |
58
|
|
|
if ($this->user && property_exists($this->user, 'loginasData')) { |
59
|
|
|
$this->user->setLoginAsData($this->getSession()->get('loginas.user.data')); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function login(Request $request, $remember = false, array $input = ['email', 'password']) |
64
|
|
|
{ |
65
|
|
|
return $this->attempt($request, $remember, $input); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function loginByModel(User $user, $remember = false) |
69
|
|
|
{ |
70
|
|
|
return $this->auth->guard($this->guard)->login($user, $remember); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function attempt(Request $request, $remember = false, array $input = ['email', 'password']) |
74
|
|
|
{ |
75
|
|
|
// validation here |
76
|
|
|
|
77
|
|
|
if ($lockedOut = $this->hasTooManyLoginAttempts($request)) { |
78
|
|
|
$this->fireLockoutEvent($request); |
79
|
|
|
|
80
|
|
|
$seconds = $this->secondsRemainingOnLockout($request); |
|
|
|
|
81
|
|
|
|
82
|
|
|
throw new AuthException('Too many login attempts. Please try again in ' . $seconds . ' seconds.'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$credentials = $request->only($input); |
86
|
|
|
|
87
|
|
|
if ($this->auth->guard($this->guard)->attempt($credentials, $remember)) { |
88
|
|
|
$this->clearLoginAttempts($request); |
89
|
|
|
$user = $this->auth->guard($this->guard)->user(); |
90
|
|
|
return $user; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (!$lockedOut) { |
94
|
|
|
$this->incrementLoginAttempts($request); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
throw new AuthException('These credentials do not match our records.'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function logout() |
101
|
|
|
{ |
102
|
|
|
$this->getSession()->remove('loginas.user'); |
103
|
|
|
|
104
|
|
|
$this->auth->guard($this->guard)->logout(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function loginUsername() |
108
|
|
|
{ |
109
|
|
|
return 'email'; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function isConsole() |
113
|
|
|
{ |
114
|
|
|
return App::runningInConsole(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return \Longman\Platfourm\User\Models\Eloquent\User |
119
|
|
|
*/ |
120
|
|
|
public function getUser() |
121
|
|
|
{ |
122
|
|
|
if (empty($this->user)) { |
123
|
|
|
throw new UnauthorizedException; |
124
|
|
|
} |
125
|
|
|
return $this->user; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return \Longman\Platfourm\User\Models\Eloquent\User |
130
|
|
|
*/ |
131
|
|
|
public function user() |
132
|
|
|
{ |
133
|
|
|
if (empty($this->user)) { |
134
|
|
|
throw new UnauthorizedException; |
135
|
|
|
} |
136
|
|
|
return $this->user; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function check() |
140
|
|
|
{ |
141
|
|
|
if ($this->auth->guard($this->guard)->check()) { |
142
|
|
|
return true; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return false; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function guest() |
149
|
|
|
{ |
150
|
|
|
|
151
|
|
|
return !$this->check(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return mixed |
156
|
|
|
* @throws \Longman\Platfourm\Auth\Exceptions\UnauthorizedException |
157
|
|
|
*/ |
158
|
|
|
public function guard() |
159
|
|
|
{ |
160
|
|
|
if (empty($this->auth->guard($this->guard))) { |
161
|
|
|
throw new UnauthorizedException; |
162
|
|
|
} |
163
|
|
|
return $this->auth->guard($this->guard); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function hasRole($role) |
167
|
|
|
{ |
168
|
|
|
return $this->getUser()->hasRole($role); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function should($perm) |
172
|
|
|
{ |
173
|
|
|
if (!$this->getUser()->can($perm)) { |
174
|
|
|
throw new ForbiddenException('Do not have permission to ' . str_replace('.', ' ', $perm)); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function can($perm) |
179
|
|
|
{ |
180
|
|
|
return $this->getUser()->can($perm); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
View Code Duplication |
public function canLoginAs(User $user) |
|
|
|
|
184
|
|
|
{ |
185
|
|
|
if (!$this->can('user.loginas')) { |
186
|
|
|
return false; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
if ($this->getSession()->has('loginas.user')) { |
190
|
|
|
return false; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
if ($this->getUser()->id == $user->id) { |
|
|
|
|
194
|
|
|
return false; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return true; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
View Code Duplication |
public function canDeleteUser(User $user) |
|
|
|
|
201
|
|
|
{ |
202
|
|
|
if (!$this->can('user.delete')) { |
203
|
|
|
return false; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
if ($this->getUser()->id == $user->id) { |
|
|
|
|
207
|
|
|
return false; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
if ($this->getSession()->has('loginas.user')) { |
211
|
|
|
return false; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return true; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
View Code Duplication |
public function canUpdateUser(User $user, $status = 1) |
|
|
|
|
218
|
|
|
{ |
219
|
|
|
if (!$this->can('user.update')) { |
220
|
|
|
return false; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
if ($this->getUser()->id == $user->id) { |
|
|
|
|
224
|
|
|
return false; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
if ($this->getSession()->has('loginas.user')) { |
228
|
|
|
return false; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return true; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function loginAs($id) |
235
|
|
|
{ |
236
|
|
|
$user = $this->repository->find($id); |
237
|
|
|
if (!$this->canLoginAs($user)) { |
238
|
|
|
throw new ForbiddenException('Do not have permission to login as user: ' . $id); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
$this->getSession()->put('loginas.user.id', $this->user()->id); |
|
|
|
|
242
|
|
|
$this->getSession()->put('loginas.user.name', $this->user()->getFullname()); |
243
|
|
|
$this->getSession()->put('loginas.user.data', $this->user()->toArray()); |
244
|
|
|
|
245
|
|
|
$user->setLoginAsData($this->getSession()->get('loginas.user.data')); |
246
|
|
|
|
247
|
|
|
$this->auth->login($user); |
|
|
|
|
248
|
|
|
|
249
|
|
|
$this->user = $user; |
250
|
|
|
|
251
|
|
|
return $user; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function logoutAs() |
255
|
|
|
{ |
256
|
|
|
|
257
|
|
|
$id = $this->getSession()->get('loginas.user.id'); |
258
|
|
|
|
259
|
|
|
if (!$id) { |
260
|
|
|
throw new \RuntimeException('You are not switched to other user!'); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
$user = $this->repository->find($id); |
264
|
|
|
|
265
|
|
|
$this->getSession()->forget('loginas.user'); |
266
|
|
|
|
267
|
|
|
$this->auth->login($user); |
|
|
|
|
268
|
|
|
|
269
|
|
|
$this->user = $user; |
270
|
|
|
return $user; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
protected function getSession() |
274
|
|
|
{ |
275
|
|
|
return app(SessionContract::class); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
public function __get($key) |
279
|
|
|
{ |
280
|
|
|
if (isset($this->user->$key)) { |
281
|
|
|
return $this->user->$key; |
282
|
|
|
} |
283
|
|
|
throw new InvalidArgumentException('Attribute ' . $key . ' not found in ' . __CLASS__); |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.