|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rojtjo\SentinelGuard; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Auth\GuardHelpers; |
|
6
|
|
|
use Illuminate\Contracts\Auth\Authenticatable; |
|
7
|
|
|
use Illuminate\Contracts\Auth\StatefulGuard; |
|
8
|
|
|
|
|
9
|
|
|
class SentinelGuard implements StatefulGuard |
|
10
|
|
|
{ |
|
11
|
|
|
use GuardHelpers; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var Sentinel |
|
15
|
|
|
*/ |
|
16
|
|
|
private $sentinel; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var bool |
|
20
|
|
|
*/ |
|
21
|
|
|
private $viaRemember = false; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param GuardableSentinel $sentinel |
|
25
|
|
|
*/ |
|
26
|
60 |
|
public function __construct(GuardableSentinel $sentinel) |
|
27
|
|
|
{ |
|
28
|
60 |
|
$this->sentinel = $sentinel; |
|
|
|
|
|
|
29
|
60 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Determine if the current user is authenticated. |
|
33
|
|
|
* |
|
34
|
|
|
* @return bool |
|
35
|
|
|
*/ |
|
36
|
3 |
|
public function check() |
|
37
|
|
|
{ |
|
38
|
3 |
|
if ($this->sentinel->getUser() !== null) { |
|
39
|
|
|
return true; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
3 |
|
$persistences = $this->sentinel->getPersistenceRepository(); |
|
43
|
3 |
|
if (! $code = $persistences->check()) { |
|
44
|
|
|
return false; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
3 |
|
if (! $user = $persistences->findUserByPersistenceCode($code)) { |
|
48
|
|
|
return false; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
3 |
|
$this->viaRemember = true; |
|
52
|
|
|
|
|
53
|
3 |
|
if (! $this->sentinel->doCycleCheckpoints('check', $user)) { |
|
54
|
|
|
return false; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
3 |
|
$this->user = $user; |
|
58
|
|
|
|
|
59
|
3 |
|
return true; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Determine if the current user is a guest. |
|
64
|
|
|
* |
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
3 |
|
public function guest() |
|
68
|
|
|
{ |
|
69
|
3 |
|
return $this->sentinel->guest(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get the currently authenticated user. |
|
74
|
|
|
* |
|
75
|
|
|
* @return \Illuminate\Contracts\Auth\Authenticatable|null |
|
76
|
|
|
*/ |
|
77
|
9 |
|
public function user() |
|
78
|
|
|
{ |
|
79
|
9 |
|
return $this->sentinel->getUser(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get the ID for the currently authenticated user. |
|
84
|
|
|
* |
|
85
|
|
|
* @return int|null |
|
86
|
|
|
*/ |
|
87
|
6 |
|
public function id() |
|
88
|
|
|
{ |
|
89
|
6 |
|
$user = $this->user(); |
|
90
|
|
|
|
|
91
|
6 |
|
return $user ? $user->getAuthIdentifier() : null; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Validate a user's credentials. |
|
96
|
|
|
* |
|
97
|
|
|
* @param array $credentials |
|
98
|
|
|
* @return bool |
|
99
|
|
|
*/ |
|
100
|
6 |
|
public function validate(array $credentials = []) |
|
101
|
|
|
{ |
|
102
|
6 |
|
$users = $this->sentinel->getUserRepository(); |
|
103
|
6 |
|
$user = $users->findByCredentials($credentials); |
|
104
|
|
|
|
|
105
|
2 |
|
return $user |
|
106
|
5 |
|
? $users->validateCredentials($user, $credentials) |
|
107
|
6 |
|
: false; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Set the current user. |
|
112
|
|
|
* |
|
113
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user |
|
114
|
|
|
* @return void |
|
115
|
|
|
*/ |
|
116
|
3 |
|
public function setUser(Authenticatable $user) |
|
117
|
|
|
{ |
|
118
|
3 |
|
$this->sentinel->setUser($user); |
|
119
|
3 |
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Attempt to authenticate a user using the given credentials. |
|
123
|
|
|
* |
|
124
|
|
|
* @param array $credentials |
|
125
|
|
|
* @param bool $remember |
|
126
|
|
|
* @return bool |
|
127
|
|
|
*/ |
|
128
|
6 |
|
public function attempt(array $credentials = [], $remember = false) |
|
129
|
|
|
{ |
|
130
|
6 |
|
return (bool)$this->sentinel->authenticate($credentials, $remember); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Log a user into the application without sessions or cookies. |
|
135
|
|
|
* |
|
136
|
|
|
* @param array $credentials |
|
137
|
|
|
* @return bool |
|
138
|
|
|
*/ |
|
139
|
3 |
|
public function once(array $credentials = []) |
|
140
|
|
|
{ |
|
141
|
3 |
|
return $this->sentinel->stateless($credentials); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Log a user into the application. |
|
146
|
|
|
* |
|
147
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user |
|
148
|
|
|
* @param bool $remember |
|
149
|
|
|
* @return void |
|
150
|
|
|
*/ |
|
151
|
12 |
|
public function login(Authenticatable $user, $remember = false) |
|
152
|
|
|
{ |
|
153
|
12 |
|
$this->sentinel->login($user, $remember); |
|
154
|
12 |
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Log the given user ID into the application. |
|
158
|
|
|
* |
|
159
|
|
|
* @param mixed $id |
|
160
|
|
|
* @param bool $remember |
|
161
|
|
|
* @return \Illuminate\Contracts\Auth\Authenticatable |
|
162
|
|
|
*/ |
|
163
|
9 |
|
public function loginUsingId($id, $remember = false) |
|
164
|
|
|
{ |
|
165
|
9 |
|
$user = $this->sentinel->getUserRepository()->findById($id); |
|
166
|
9 |
|
if (! $user) { |
|
167
|
3 |
|
return false; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
6 |
|
$this->login($user, $remember); |
|
171
|
|
|
|
|
172
|
6 |
|
return $user; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Log the given user ID into the application without sessions or cookies. |
|
177
|
|
|
* |
|
178
|
|
|
* @param mixed $id |
|
179
|
|
|
* @return bool |
|
180
|
|
|
*/ |
|
181
|
6 |
|
public function onceUsingId($id) |
|
182
|
|
|
{ |
|
183
|
6 |
|
$user = $this->sentinel->getUserRepository()->findById($id); |
|
184
|
|
|
|
|
185
|
2 |
|
return $user |
|
186
|
5 |
|
? (bool)$this->sentinel->stateless($user) |
|
187
|
6 |
|
: false; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Determine if the user was authenticated via "remember me" cookie. |
|
192
|
|
|
* |
|
193
|
|
|
* @return bool |
|
194
|
|
|
*/ |
|
195
|
6 |
|
public function viaRemember() |
|
196
|
|
|
{ |
|
197
|
6 |
|
return $this->viaRemember; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Log the user out of the application. |
|
202
|
|
|
* |
|
203
|
|
|
* @return void |
|
204
|
|
|
*/ |
|
205
|
3 |
|
public function logout() |
|
206
|
|
|
{ |
|
207
|
3 |
|
$this->sentinel->logout(); |
|
208
|
3 |
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..