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

Commit 7daa878

Browse files
committed
Check instance of auth provider rather than configuration.
- Closes #575 (cherry picked from commit b342fa7)
1 parent a48185c commit 7daa878

File tree

2 files changed

+58
-15
lines changed

2 files changed

+58
-15
lines changed

src/Resolvers/UserResolver.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
use Adldap\Laravel\Events\Authenticated;
1010
use Adldap\Laravel\Events\Authenticating;
1111
use Adldap\Laravel\Events\AuthenticationFailed;
12-
use Adldap\Laravel\Auth\DatabaseUserProvider;
1312
use Adldap\Laravel\Auth\NoDatabaseUserProvider;
13+
use Illuminate\Support\Facades\Auth;
1414
use Illuminate\Support\Facades\Event;
1515
use Illuminate\Support\Facades\Config;
16+
use Illuminate\Contracts\Auth\UserProvider;
1617
use Illuminate\Contracts\Auth\Authenticatable;
1718

1819
class UserResolver implements ResolverInterface
@@ -25,7 +26,7 @@ class UserResolver implements ResolverInterface
2526
protected $ldap;
2627

2728
/**
28-
* The LDAP connection to utilize.
29+
* The name of the LDAP connection to utilize.
2930
*
3031
* @var string
3132
*/
@@ -38,7 +39,7 @@ public function __construct(AdldapInterface $ldap)
3839
{
3940
$this->ldap = $ldap;
4041

41-
$this->setConnection($this->getAuthConnection());
42+
$this->setConnection($this->getLdapAuthConnectionName());
4243
}
4344

4445
/**
@@ -66,12 +67,10 @@ public function byCredentials(array $credentials = [])
6667
return;
6768
}
6869

69-
$provider = Config::get('adldap_auth.provider', DatabaseUserProvider::class);
70-
7170
// Depending on the configured user provider, the
7271
// username field will differ for retrieving
7372
// users by their credentials.
74-
if ($provider == NoDatabaseUserProvider::class) {
73+
if ($this->getAppAuthProvider() instanceof NoDatabaseUserProvider) {
7574
$username = $credentials[$this->getLdapDiscoveryAttribute()];
7675
} else {
7776
$username = $credentials[$this->getEloquentUsernameAttribute()];
@@ -114,7 +113,7 @@ public function authenticate(User $user, array $credentials = [])
114113

115114
Event::fire(new Authenticating($user, $username));
116115

117-
if ($this->getProvider()->auth()->attempt($username, $password)) {
116+
if ($this->getLdapAuthProvider()->auth()->attempt($username, $password)) {
118117
Event::fire(new Authenticated($user));
119118

120119
return true;
@@ -130,7 +129,7 @@ public function authenticate(User $user, array $credentials = [])
130129
*/
131130
public function query() : Builder
132131
{
133-
$query = $this->getProvider()->search()->users();
132+
$query = $this->getLdapAuthProvider()->search()->users();
134133

135134
$scopes = Config::get('adldap_auth.scopes', []);
136135

@@ -192,19 +191,29 @@ protected function getPasswordFromCredentials($credentials)
192191
*
193192
* @throws \Adldap\AdldapException
194193
*
195-
* @return \Adldap\Connections\ProviderInterface
194+
* @return ProviderInterface
196195
*/
197-
protected function getProvider() : ProviderInterface
196+
protected function getLdapAuthProvider() : ProviderInterface
198197
{
199198
return $this->ldap->getProvider($this->connection);
200199
}
201200

201+
/**
202+
* Returns the default guards provider instance.
203+
*
204+
* @return UserProvider
205+
*/
206+
protected function getAppAuthProvider() : UserProvider
207+
{
208+
return Auth::guard()->getProvider();
209+
}
210+
202211
/**
203212
* Returns the connection name of the authentication provider.
204213
*
205214
* @return string
206215
*/
207-
protected function getAuthConnection()
216+
protected function getLdapAuthConnectionName()
208217
{
209218
return Config::get('adldap_auth.connection', 'default');
210219
}

tests/UserResolverTest.php

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
use Adldap\AdldapInterface;
88
use Adldap\Schemas\SchemaInterface;
99
use Adldap\Connections\ProviderInterface;
10+
use Adldap\Laravel\Auth\NoDatabaseUserProvider;
1011
use Adldap\Laravel\Resolvers\UserResolver;
12+
use Illuminate\Support\Facades\Auth;
1113
use Illuminate\Support\Facades\Config;
1214

1315
class UserResolverTest extends TestCase
@@ -71,7 +73,7 @@ public function scopes_are_applied_when_query_is_called()
7173

7274
$ad = m::mock(AdldapInterface::class);
7375

74-
$ad->shouldReceive('getProvider')->withArgs(['default'])->andReturn($provider);
76+
$ad->shouldReceive('getProvider')->with('default')->andReturn($provider);
7577

7678
$resolver = new UserResolver($ad);
7779

@@ -81,12 +83,44 @@ public function scopes_are_applied_when_query_is_called()
8183
/** @test */
8284
public function connection_is_set_upon_creation()
8385
{
84-
Config::shouldReceive('get')->once()->withArgs(['adldap_auth.connection', 'default']);
86+
Config::shouldReceive('get')->once()->with('adldap_auth.connection', 'default')->andReturn('other-test');
8587

8688
$ad = m::mock(AdldapInterface::class);
8789

88-
$ad->shouldReceive('getProvider')->withArgs(['other-connection']);
89-
9090
new UserResolver($ad);
9191
}
92+
93+
/** @test */
94+
public function by_credentials_retrieves_alternate_username_attribute_depending_on_user_provider()
95+
{
96+
// Setup our builder.
97+
$query = m::mock(Builder::class);
98+
99+
//
100+
$query->shouldReceive('whereEquals')->once()->with('userprincipalname', 'jdoe')->andReturnSelf()
101+
->shouldReceive('first')->andReturnNull();
102+
103+
$ldapProvider = m::mock(ProviderInterface::class);
104+
105+
$ldapProvider->shouldReceive('search')->once()->andReturnSelf()
106+
->shouldReceive('users')->once()->andReturn($query);
107+
108+
$ad = m::mock(AdldapInterface::class);
109+
110+
$ad->shouldReceive('getProvider')->once()->andReturn($ldapProvider);
111+
112+
$ad->shouldReceive('getProvider')->andReturnSelf();
113+
114+
$authProvider = m::mock(NoDatabaseUserProvider::class);
115+
116+
Auth::shouldReceive('guard')->once()->andReturnSelf()->shouldReceive('getProvider')->once()->andReturn($authProvider);
117+
118+
Config::shouldReceive('get')->with('adldap_auth.connection', 'default')->andReturn('default')
119+
->shouldReceive('get')->with('adldap_auth.usernames.ldap.discover', 'userprincipalname')->andReturn('userprincipalname')
120+
->shouldReceive('get')->with('adldap_auth.scopes', [])->andReturn([]);
121+
122+
$resolver = new UserResolver($ad);
123+
124+
$resolver->byCredentials(['userprincipalname' => 'jdoe', 'password' => 'Password1']);
125+
}
92126
}

0 commit comments

Comments
 (0)