Skip to content

Commit b265cb7

Browse files
committed
Handle user resolution the same across providers
1 parent 41445c7 commit b265cb7

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed

src/Auth/DatabaseUserProvider.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,6 @@ class DatabaseUserProvider extends UserProvider
3434
*/
3535
protected $user;
3636

37-
/**
38-
* The user resolver to use for finding the authenticating user.
39-
*
40-
* @var \Closure
41-
*/
42-
protected $userResolver;
43-
4437
/**
4538
* Whether falling back to Eloquent auth is being used.
4639
*
@@ -66,10 +59,6 @@ public function __construct(
6659

6760
$this->importer = $importer;
6861
$this->eloquent = $eloquent;
69-
70-
$this->userResolver = function ($credentials) {
71-
return $this->users->findByCredentials($credentials);
72-
};
7362
}
7463

7564
/**
@@ -158,14 +147,10 @@ public function updateRememberToken(Authenticatable $user, $token)
158147
*/
159148
public function retrieveByCredentials(array $credentials)
160149
{
161-
$user = rescue(function () use ($credentials) {
162-
return call_user_func($this->userResolver, $credentials);
163-
});
164-
165150
// If an LDAP user is not located by their credentials and fallback
166151
// is enabled, we will attempt to locate the local database user
167152
// instead and perform validation on their password normally.
168-
if (! $user) {
153+
if (! $user = $this->fetchLdapUserByCredentials($credentials)) {
169154
return isset($credentials['fallback'])
170155
? $this->retrieveByCredentialsUsingEloquent($credentials)
171156
: null;

src/Auth/NoDatabaseUserProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function updateRememberToken(Authenticatable $user, $token)
3636
*/
3737
public function retrieveByCredentials(array $credentials)
3838
{
39-
return $this->users->findByCredentials($credentials);
39+
return $this->fetchLdapUserByCredentials($credentials);
4040
}
4141

4242
/**

src/Auth/UserProvider.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace LdapRecord\Laravel\Auth;
44

55
use Illuminate\Contracts\Auth\UserProvider as LaravelUserProvider;
6+
use Illuminate\Validation\ValidationException;
67
use LdapRecord\Laravel\LdapUserAuthenticator;
78
use LdapRecord\Laravel\LdapUserRepository;
89

@@ -22,6 +23,13 @@ abstract class UserProvider implements LaravelUserProvider
2223
*/
2324
protected $auth;
2425

26+
/**
27+
* The user resolver to use for finding the authenticating user.
28+
*
29+
* @var \Closure
30+
*/
31+
protected $userResolver;
32+
2533
/**
2634
* Constructor.
2735
*
@@ -32,6 +40,42 @@ public function __construct(LdapUserRepository $users, LdapUserAuthenticator $au
3240
{
3341
$this->users = $users;
3442
$this->auth = $auth;
43+
44+
$this->userResolver = function ($credentials) {
45+
return $this->users->findByCredentials($credentials);
46+
};
47+
}
48+
49+
/**
50+
* Attempt to retrieve the user by their credentials.
51+
*
52+
* @param array $credentials
53+
*
54+
* @return mixed
55+
*
56+
* @throws ValidationException
57+
*/
58+
protected function fetchLdapUserByCredentials($credentials)
59+
{
60+
return rescue(function () use ($credentials) {
61+
return call_user_func($this->userResolver, $credentials);
62+
}, function ($e) {
63+
$this->handleException($e);
64+
});
65+
}
66+
67+
/**
68+
* Handle exceptions during user resolution.
69+
*
70+
* @param \Exception $e
71+
*
72+
* @throws ValidationException
73+
*/
74+
protected function handleException($e)
75+
{
76+
if ($e instanceof ValidationException) {
77+
throw $e;
78+
}
3579
}
3680

3781
/**

0 commit comments

Comments
 (0)