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

Commit 964793a

Browse files
committed
Fix Login Fallback
- Closes #84, check if connection is bound before querying LDAP server
1 parent b22d52a commit 964793a

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

src/AdldapAuthUserProvider.php

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Adldap\Laravel;
44

5-
use Adldap\Laravel\Facades\Adldap;
65
use Adldap\Laravel\Traits\ImportsUsers;
76
use Adldap\Models\User;
87
use Illuminate\Auth\EloquentUserProvider;
@@ -40,40 +39,44 @@ public function retrieveByToken($identifier, $token)
4039
*/
4140
public function retrieveByCredentials(array $credentials)
4241
{
43-
// Get the search query for users only
42+
// Get the search query for users only.
4443
$query = $this->newAdldapUserQuery();
4544

46-
// Get the username input attributes
47-
$attributes = $this->getUsernameAttribute();
48-
49-
// Get the input key
50-
$key = key($attributes);
51-
52-
// Filter the query by the username attribute
53-
$query->whereEquals($attributes[$key], $credentials[$key]);
54-
55-
// Retrieve the first user result
56-
$user = $query->first();
45+
// Make sure the connection is bound
46+
// before we try to utilize it.
47+
if ($query->getConnection()->isBound()) {
48+
// Get the username input attributes.
49+
$attributes = $this->getUsernameAttribute();
5750

58-
// If the user is an Adldap User model instance.
59-
if ($user instanceof User) {
60-
// Retrieve the users login attribute.
61-
$username = $user->{$this->getLoginAttribute()};
51+
// Get the input key.
52+
$key = key($attributes);
6253

63-
if (is_array($username)) {
64-
// We'll make sure we retrieve the users first username
65-
// attribute if it's contained in an array.
66-
$username = Arr::get($username, 0);
67-
}
54+
// Filter the query by the username attribute.
55+
$query->whereEquals($attributes[$key], $credentials[$key]);
6856

69-
// Get the password input array key.
70-
$key = $this->getPasswordKey();
57+
// Retrieve the first user result.
58+
$user = $query->first();
7159

72-
// Try to log the user in.
73-
if ($this->authenticate($username, $credentials[$key])) {
74-
// Login was successful, we'll create a new
75-
// Laravel model with the Adldap user.
76-
return $this->getModelFromAdldap($user, $credentials[$key]);
60+
// If the user is an Adldap User model instance.
61+
if ($user instanceof User) {
62+
// Retrieve the users login attribute.
63+
$username = $user->{$this->getLoginAttribute()};
64+
65+
if (is_array($username)) {
66+
// We'll make sure we retrieve the users first username
67+
// attribute if it's contained in an array.
68+
$username = Arr::get($username, 0);
69+
}
70+
71+
// Get the password input array key.
72+
$key = $this->getPasswordKey();
73+
74+
// Try to log the user in.
75+
if ($this->authenticate($username, $credentials[$key])) {
76+
// Login was successful, we'll create a new
77+
// Laravel model with the Adldap user.
78+
return $this->getModelFromAdldap($user, $credentials[$key]);
79+
}
7780
}
7881
}
7982

0 commit comments

Comments
 (0)