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

Commit ea14abb

Browse files
committed
Lazy connection before searching for user while trying to authenticate
1 parent 0c82877 commit ea14abb

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

src/Resolvers/UserResolver.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,15 @@ protected function getPasswordFromCredentials($credentials)
201201
*/
202202
protected function getLdapAuthProvider(): ProviderInterface
203203
{
204-
return $this->ldap->getProvider($this->connection ?? $this->getLdapAuthConnectionName());
204+
$provider = $this->ldap->getProvider($this->connection ?? $this->getLdapAuthConnectionName());
205+
206+
if (!$provider->getConnection()->isBound()) {
207+
// We'll make sure we have a bound connection before
208+
// allowing dynamic calls on the default provider.
209+
$provider->connect();
210+
}
211+
212+
return $provider;
205213
}
206214

207215
/**

tests/DatabaseProviderTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
namespace Adldap\Laravel\Tests;
44

55
use Adldap\AdldapInterface;
6+
use Adldap\Connections\ConnectionInterface;
7+
use Adldap\Connections\Provider;
8+
use Adldap\Connections\ProviderInterface;
69
use Adldap\Laravel\Commands\Import;
710
use Adldap\Laravel\Facades\Resolver;
11+
use Adldap\Laravel\Resolvers\ResolverInterface;
12+
use Adldap\Laravel\Resolvers\UserResolver;
813
use Adldap\Laravel\Tests\Handlers\LdapAttributeHandler;
914
use Adldap\Laravel\Tests\Models\TestUser as EloquentUser;
1015
use Adldap\Laravel\Tests\Scopes\JohnDoeScope;
@@ -13,6 +18,7 @@
1318
use Illuminate\Support\Facades\App;
1419
use Illuminate\Support\Facades\Auth;
1520
use Illuminate\Support\Facades\Hash;
21+
use Mockery as m;
1622

1723
class DatabaseProviderTest extends DatabaseTestCase
1824
{
@@ -89,8 +95,20 @@ public function auth_fails_when_user_not_found()
8995
/** @test */
9096
public function config_scopes_are_applied()
9197
{
98+
$ldapMock = m::mock(AdldapInterface::class);
99+
App::instance(AdldapInterface::class, $ldapMock);
100+
/** @var Provider $provider */
101+
$provider = App::make(Provider::class);
92102
config(['ldap_auth.scopes' => [JohnDoeScope::class]]);
93103

104+
$providerMock = m::mock(ProviderInterface::class);
105+
$connectionMock = m::mock(ConnectionInterface::class);
106+
107+
$providerMock->shouldReceive('getConnection')->once()->andReturn($connectionMock);
108+
$connectionMock->shouldReceive('isBound')->once()->andReturn(true);
109+
$ldapMock->shouldReceive('getProvider')->once()->andReturn($providerMock);
110+
$providerMock->shouldReceive('search')->once()->andReturn($provider->search());
111+
94112
$expectedFilter = '(&(objectclass=\75\73\65\72)(objectcategory=\70\65\72\73\6f\6e)(!(objectclass=\63\6f\6e\74\61\63\74))(cn=\4a\6f\68\6e\20\44\6f\65))';
95113

96114
$this->assertEquals($expectedFilter, Resolver::query()->getQuery());
@@ -219,6 +237,10 @@ public function auth_attempts_fallback_using_config_option()
219237
/** @test */
220238
public function auth_attempts_using_fallback_does_not_require_connection()
221239
{
240+
$ldapMock = m::mock(AdldapInterface::class);
241+
App::instance(AdldapInterface::class, $ldapMock);
242+
/** @var Provider $provider */
243+
$provider = App::make(Provider::class);
222244
config(['ldap_auth.login_fallback' => true]);
223245

224246
EloquentUser::create([
@@ -232,6 +254,14 @@ public function auth_attempts_using_fallback_does_not_require_connection()
232254
'password' => 'Password123',
233255
];
234256

257+
$providerMock = m::mock(ProviderInterface::class);
258+
$connectionMock = m::mock(ConnectionInterface::class);
259+
260+
$providerMock->shouldReceive('getConnection')->times(3)->andReturn($connectionMock);
261+
$connectionMock->shouldReceive('isBound')->times(3)->andReturn(true);
262+
$ldapMock->shouldReceive('getProvider')->times(3)->andReturn($providerMock);
263+
$providerMock->shouldReceive('search')->times(3)->andReturn($provider->search());
264+
235265
$this->assertTrue(Auth::attempt($credentials));
236266

237267
$user = Auth::user();

tests/UserResolverTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Adldap\Laravel\Tests;
44

55
use Adldap\AdldapInterface;
6+
use Adldap\Connections\ConnectionInterface;
67
use Adldap\Connections\ProviderInterface;
78
use Adldap\Laravel\Auth\NoDatabaseUserProvider;
89
use Adldap\Laravel\Resolvers\UserResolver;
@@ -85,6 +86,11 @@ public function scopes_are_applied_when_query_is_called()
8586
->shouldReceive('users')->once()->andReturn($builder);
8687

8788
$ad = m::mock(AdldapInterface::class);
89+
$ldapConnection = m::mock(ConnectionInterface::class);
90+
$ldapConnection->shouldReceive('isBound')->once()->andReturn(false);
91+
92+
$provider->shouldReceive('getConnection')->once()->andReturn($ldapConnection);
93+
$provider->shouldReceive('connect')->once();
8894

8995
$ad->shouldReceive('getProvider')->with('default')->andReturn($provider);
9096

@@ -99,8 +105,14 @@ public function connection_is_set_when_retrieving_provider()
99105
Config::shouldReceive('get')->once()->with('ldap_auth.connection', 'default')->andReturn('other-domain');
100106

101107
$ad = m::mock(AdldapInterface::class);
108+
$provider = m::mock(ProviderInterface::class);
102109

103-
$ad->shouldReceive('getProvider')->andReturn(m::mock(ProviderInterface::class))->with('other-domain');
110+
$ad->shouldReceive('getProvider')->with('other-domain')->andReturn($provider);
111+
$ldapConnection = m::mock(ConnectionInterface::class);
112+
$ldapConnection->shouldReceive('isBound')->once()->andReturn(false);
113+
114+
$provider->shouldReceive('getConnection')->once()->andReturn($ldapConnection);
115+
$provider->shouldReceive('connect')->once();
104116

105117
$r = m::mock(UserResolver::class, [$ad])->makePartial();
106118

@@ -130,6 +142,11 @@ public function by_credentials_retrieves_alternate_username_attribute_depending_
130142
->shouldReceive('users')->once()->andReturn($query);
131143

132144
$ad = m::mock(AdldapInterface::class);
145+
$ldapConnection = m::mock(ConnectionInterface::class);
146+
$ldapConnection->shouldReceive('isBound')->once()->andReturn(false);
147+
148+
$ldapProvider->shouldReceive('getConnection')->once()->andReturn($ldapConnection);
149+
$ldapProvider->shouldReceive('connect')->once();
133150

134151
$ad->shouldReceive('getProvider')->once()->andReturn($ldapProvider);
135152

0 commit comments

Comments
 (0)