/
DatabaseUserProvider.php
213 lines (178 loc) · 5.61 KB
/
DatabaseUserProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
namespace LdapRecord\Laravel\Auth;
use Closure;
use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Traits\ForwardsCalls;
use LdapRecord\Laravel\Events\Auth\Completed;
use LdapRecord\Laravel\Events\Import\Imported;
use LdapRecord\Laravel\Events\Import\Saved;
use LdapRecord\Laravel\Import\UserSynchronizer;
use LdapRecord\Laravel\LdapUserAuthenticator;
use LdapRecord\Laravel\LdapUserRepository;
use LdapRecord\Models\Model;
/** @mixin EloquentUserProvider */
class DatabaseUserProvider extends UserProvider
{
use ForwardsCalls;
/**
* The LDAP user synchronizer instance.
*/
protected UserSynchronizer $synchronizer;
/**
* The eloquent user provider.
*/
protected EloquentUserProvider $eloquent;
/**
* The authenticating LDAP user.
*/
protected ?Model $user = null;
/**
* Whether falling back to Eloquent auth is being used.
*/
protected bool $fallback = false;
/**
* Create a new LDAP user provider.
*/
public function __construct(
LdapUserRepository $users,
LdapUserAuthenticator $auth,
UserSynchronizer $synchronizer,
EloquentUserProvider $eloquent
) {
parent::__construct($users, $auth);
$this->synchronizer = $synchronizer;
$this->eloquent = $eloquent;
}
/**
* Dynamically pass missing methods to the Eloquent user provider.
*/
public function __call(string $method, array $parameters): mixed
{
return $this->forwardCallTo($this->eloquent, $method, $parameters);
}
/**
* Get the Eloquent user provider.
*/
public function eloquent(): EloquentUserProvider
{
return $this->eloquent;
}
/**
* Get the LDAP user importer.
*/
public function getLdapUserSynchronizer(): UserSynchronizer
{
return $this->synchronizer;
}
/**
* Set the authenticating LDAP user.
*/
public function setAuthenticatingUser(Model $user): void
{
$this->user = $user;
}
/**
* Fallback to Eloquent authentication after failing to locate an LDAP user.
*/
public function shouldFallback(): void
{
$this->fallback = true;
}
/**
* Set the callback to be used to resolve LDAP users.
*/
public function resolveUsersUsing(Closure $callback): static
{
$this->userResolver = $callback;
return $this;
}
/**
* {@inheritdoc}
*/
public function retrieveById($identifier): ?Authenticatable
{
return $this->eloquent->retrieveById($identifier);
}
/**
* {@inheritdoc}
*/
public function retrieveByToken($identifier, $token): ?Authenticatable
{
return $this->eloquent->retrieveByToken($identifier, $token);
}
/**
* {@inheritdoc}
*/
public function updateRememberToken(Authenticatable $user, $token): void
{
$this->eloquent->updateRememberToken($user, $token);
}
/**
* {@inheritdoc}
*/
public function retrieveByCredentials(array $credentials): ?Authenticatable
{
$this->fallback = isset($credentials['fallback']);
$fetch = function () use ($credentials) {
return $this->fetchLdapUserByCredentials($credentials);
};
// If fallback is enabled, we want to make sure we catch
// any exceptions that may occur so we do not interrupt
// the fallback process and exit authentication early.
$user = $this->fallback ? rescue($fetch) : value($fetch);
// If the user was unable to be located and fallback is
// enabled, we will attempt to fetch the database user
// and perform validation on their password normally.
if (! $user) {
return $this->fallback
? $this->retrieveByCredentialsUsingEloquent($credentials['fallback'])
: null;
}
$this->setAuthenticatingUser($user);
return $this->synchronizer->run($user, $credentials);
}
/**
* Retrieve the user from their credentials using Eloquent.
*
* @throws \Exception
*/
protected function retrieveByCredentialsUsingEloquent(array $credentials): ?Authenticatable
{
return $this->eloquent->retrieveByCredentials($credentials);
}
/**
* {@inheritdoc}
*/
public function validateCredentials(Authenticatable $user, array $credentials): bool
{
// If an LDAP user has not been located, fallback is enabled, and
// the given Eloquent model exists, we will attempt to validate
// the users password normally via the Eloquent user provider.
if (! $this->user instanceof Model) {
return $this->fallback && $user->exists
? $this->eloquent->validateCredentials($user, $credentials)
: false;
}
$this->auth->setEloquentModel($user);
if (! $this->auth->attempt($this->user, $credentials['password'])) {
return false;
}
$user->save();
event(new Saved($this->user, $user));
if ($user->wasRecentlyCreated) {
event(new Imported($this->user, $user));
}
event(new Completed($this->user, $user));
return true;
}
/**
* {@inheritdoc}
*/
public function rehashPasswordIfRequired(Authenticatable $user, array $credentials, bool $force = false): void
{
if (($this->synchronizer->getConfig()['password_column'] ?? 'password') !== false) {
$this->eloquent->rehashPasswordIfRequired($user, $credentials, $force);
}
}
}