Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.

Commit 1a4fc9b

Browse files
committed
Added method, some tweaks
- Added `discoverAdldapFromModel()` method to retrieve the Adldap user from the eloquent model - Users are now searched for only if `getBindUserToModel()` is true, to prevent extra queries for users who do not have it enabled
1 parent de38e76 commit 1a4fc9b

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

src/AdldapAuthUserProvider.php

+40-25
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,27 @@
1313
class AdldapAuthUserProvider extends EloquentUserProvider
1414
{
1515
/**
16-
* Retrieve a user by their unique identifier.
17-
*
18-
* @param mixed $identifier
19-
*
20-
* @return Authenticatable|null
16+
* {@inheritDoc}
2117
*/
2218
public function retrieveById($identifier)
2319
{
2420
$model = parent::retrieveById($identifier);
2521

26-
if ($model instanceof Authenticatable) {
27-
$attributes = $this->getUsernameAttribute();
28-
29-
$key = key($attributes);
30-
31-
$query = Adldap::users()->search();
32-
33-
$query->whereEquals($attributes[$key], $model->{$key});
34-
35-
$user = $query->first();
22+
return $this->discoverAdldapFromModel($model);
23+
}
3624

37-
if ($user instanceof User && $this->getBindUserToModel()) {
38-
$model = $this->bindAdldapToModel($user, $model);
39-
}
40-
}
25+
/**
26+
* {@inheritDoc}
27+
*/
28+
public function retrieveByToken($identifier, $token)
29+
{
30+
$model = parent::retrieveByToken($identifier, $token);
4131

42-
return $model;
32+
return $this->discoverAdldapFromModel($model);
4333
}
4434

4535
/**
46-
* Retrieve a user by the given credentials.
47-
*
48-
* @param array $credentials
49-
*
50-
* @return Authenticatable|null
36+
* {@inheritDoc}
5137
*/
5238
public function retrieveByCredentials(array $credentials)
5339
{
@@ -146,6 +132,35 @@ protected function syncModelFromAdldap(User $user, Authenticatable $model)
146132
return $model;
147133
}
148134

135+
/**
136+
* Retrieves the Adldap User model from the
137+
* specified Laravel model.
138+
*
139+
* @param mixed $model
140+
*
141+
* @return null|Authenticatable
142+
*/
143+
protected function discoverAdldapFromModel($model)
144+
{
145+
if ($model instanceof Authenticatable && $this->getBindUserToModel()) {
146+
$attributes = $this->getUsernameAttribute();
147+
148+
$key = key($attributes);
149+
150+
$query = Adldap::users()->search();
151+
152+
$query->whereEquals($attributes[$key], $model->{$key});
153+
154+
$user = $query->first();
155+
156+
if ($user instanceof User) {
157+
$model = $this->bindAdldapToModel($user, $model);
158+
}
159+
}
160+
161+
return $model;
162+
}
163+
149164
/**
150165
* Binds the Adldap User instance to the Eloquent model instance
151166
* by setting its `adldapUser` public property.

0 commit comments

Comments
 (0)