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

Commit 057b5fd

Browse files
committed
Deny soft deleted users from authenticating
- Closes #181
1 parent 2e5e696 commit 057b5fd

File tree

3 files changed

+58
-18
lines changed

3 files changed

+58
-18
lines changed

src/AdldapAuthUserProvider.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ public function retrieveByCredentials(array $credentials)
5656
// Construct / retrieve the eloquent model from our Adldap user.
5757
$model = $this->getModelFromAdldap($user, $password);
5858

59+
if (method_exists($model, 'trashed') && $model->trashed()) {
60+
// We won't allow deleted users to authenticate.
61+
return;
62+
}
63+
5964
// Perform other authenticated tasks.
6065
$this->handleAuthenticatedWithCredentials($user, $model);
6166

@@ -74,9 +79,9 @@ public function retrieveByCredentials(array $credentials)
7479
*/
7580
public function validateCredentials(Authenticatable $user, array $credentials)
7681
{
77-
// Check if we already have an authenticated AD user.
82+
// Check if we have an authenticated AD user.
7883
if ($this->user instanceof User) {
79-
// We'll save the model in case of changes.
84+
// We'll save the authenticated model in case of changes.
8085
$this->saveModel($user);
8186

8287
return true;

src/Commands/Import.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function isLogging()
116116
}
117117

118118
/**
119-
* Returns the limitation filter for importing users.
119+
* Returns the limitation filter for the user query.
120120
*
121121
* @return string
122122
*/

src/Traits/ImportsUsers.php

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,30 @@ abstract public function createModel();
2424
* @return \Illuminate\Database\Eloquent\Model
2525
*/
2626
protected function getModelFromAdldap(User $user, $password = null)
27+
{
28+
$model = $this->findOrCreateModelFromAdldap($user);
29+
30+
// Sync the users password (if enabled). If no password is
31+
// given, we'll pass in a random 16 character string.
32+
$model = $this->syncModelPassword($model, $password ?: str_random());
33+
34+
// Synchronize other active directory attributes on the model.
35+
$model = $this->syncModelFromAdldap($user, $model);
36+
37+
// Bind the Adldap model to the eloquent model (if enabled).
38+
$model = ($this->getBindUserToModel() ? $this->bindAdldapToModel($user, $model) : $model);
39+
40+
return $model;
41+
}
42+
43+
/**
44+
* Finds an Eloquent model from the specified Adldap user.
45+
*
46+
* @param \Adldap\Models\User $user
47+
*
48+
* @return \Illuminate\Database\Eloquent\Model
49+
*/
50+
protected function findOrCreateModelFromAdldap(User $user)
2751
{
2852
// Get the model key.
2953
$attributes = $this->getUsernameAttribute();
@@ -34,29 +58,18 @@ protected function getModelFromAdldap(User $user, $password = null)
3458
// Get the username from the AD model.
3559
$username = $user->{$attributes[$key]};
3660

37-
// Make sure we retrieve the first username
38-
// result if it's an array.
61+
// Make sure we retrieve the first username result if it's an array.
3962
$username = (is_array($username) ? array_get($username, 0) : $username);
4063

41-
// Try to retrieve the model from the model key and AD username.
42-
$model = $this->createModel()->newQuery()->where([$key => $username])->first();
64+
// Try to find the local database user record.
65+
$model = $this->newEloquentQuery($key, $username)->first();
4366

44-
// Create the model instance of it isn't found.
67+
// Create a new model instance of it isn't found.
4568
$model = ($model instanceof Model ? $model : $this->createModel());
4669

4770
// Set the username in case of changes in active directory.
4871
$model->{$key} = $username;
4972

50-
// Sync the users password (if enabled). If no password is
51-
// given, we'll assign a random 16 character string.
52-
$model = $this->syncModelPassword($model, $password ?: str_random());
53-
54-
// Synchronize other active directory attributes on the model.
55-
$model = $this->syncModelFromAdldap($user, $model);
56-
57-
// Bind the Adldap model to the eloquent model (if enabled).
58-
$model = ($this->getBindUserToModel() ? $this->bindAdldapToModel($user, $model) : $model);
59-
6073
return $model;
6174
}
6275

@@ -213,6 +226,28 @@ protected function newAdldapUserQuery($provider = null, $filter = null)
213226
return $query->select($this->getSelectAttributes());
214227
}
215228

229+
/**
230+
* Returns a new Eloquent user query.
231+
*
232+
* @param string $key
233+
* @param string $username
234+
*
235+
* @return \Illuminate\Database\Eloquent\Builder
236+
*/
237+
protected function newEloquentQuery($key, $username)
238+
{
239+
$model = $this->createModel();
240+
241+
if (method_exists($model, 'trashed')) {
242+
// If the trashed method exists on our User model, then we must be
243+
// using soft deletes. We need to make sure we include these
244+
// results so we don't create duplicate user records.
245+
$model = $model->withTrashed();
246+
}
247+
248+
return $model->where([$key => $username]);
249+
}
250+
216251
/**
217252
* Returns Adldap's current attribute schema.
218253
*

0 commit comments

Comments
 (0)