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

Commit 8d08f2a

Browse files
committed
Updated for Adldap v6.0 compatibility
- Updated for Adldap upcoming version `v6.0` - Updated tests - Updated configuration files to allow multiple connection support
1 parent 4b16a74 commit 8d08f2a

File tree

6 files changed

+393
-237
lines changed

6 files changed

+393
-237
lines changed

src/AdldapAuthUserProvider.php

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ protected function bindAdldapToModel(User $user, Authenticatable $model)
241241
*/
242242
protected function newAdldapUserQuery()
243243
{
244-
return Adldap::users()->search()->select($this->getSelectAttributes());
244+
return $this->getAdldap()->search()->select($this->getSelectAttributes());
245245
}
246246

247247
/**
@@ -254,7 +254,7 @@ protected function newAdldapUserQuery()
254254
*/
255255
protected function authenticate($username, $password)
256256
{
257-
return Adldap::authenticate($username, $password);
257+
return $this->getAdldap()->auth()->attempt($username, $password);
258258
}
259259

260260
/**
@@ -302,7 +302,7 @@ protected function handleAttributeCallback(User $user, $callback)
302302
*/
303303
protected function handleAttributeRetrieval(User $user, $field)
304304
{
305-
if ($field === ActiveDirectory::THUMBNAIL) {
305+
if ($field === $this->getSchema()->thumbnail()) {
306306
// If the field we're retrieving is the users thumbnail photo, we need
307307
// to retrieve it encoded so we're able to save it to the database.
308308
$value = $user->getThumbnailEncoded();
@@ -319,14 +319,43 @@ protected function handleAttributeRetrieval(User $user, $field)
319319
return $value;
320320
}
321321

322+
/**
323+
* Returns Adldap's current attribute schema.
324+
*
325+
* @return \Adldap\Contracts\Schemas\SchemaInterface
326+
*/
327+
protected function getSchema()
328+
{
329+
return $this->getAdldap()->getSchema();
330+
}
331+
332+
/**
333+
* Returns the root Adldap instance.
334+
*
335+
* @param string $provider
336+
*
337+
* @return \Adldap\Contracts\Connections\ProviderInterface
338+
*/
339+
protected function getAdldap($provider = null)
340+
{
341+
/** @var \Adldap\Adldap $ad */
342+
$ad = Adldap::getFacadeRoot();
343+
344+
if (is_null($provider)) {
345+
$provider = $this->getDefaultConnectionName();
346+
}
347+
348+
return $ad->getManager()->get($provider);
349+
}
350+
322351
/**
323352
* Returns the username attribute for discovering LDAP users.
324353
*
325354
* @return array
326355
*/
327356
protected function getUsernameAttribute()
328357
{
329-
return Config::get('adldap_auth.username_attribute', ['username' => ActiveDirectory::ACCOUNT_NAME]);
358+
return Config::get('adldap_auth.username_attribute', ['username' => $this->getSchema()->accountName()]);
330359
}
331360

332361
/**
@@ -347,7 +376,7 @@ protected function getPasswordKey()
347376
*/
348377
protected function getLoginAttribute()
349378
{
350-
return Config::get('adldap_auth.login_attribute', ActiveDirectory::ACCOUNT_NAME);
379+
return Config::get('adldap_auth.login_attribute', $this->getSchema()->accountName());
351380
}
352381

353382
/**
@@ -369,7 +398,7 @@ protected function getBindUserToModel()
369398
*/
370399
protected function getSyncAttributes()
371400
{
372-
return Config::get('adldap_auth.sync_attributes', ['name' => ActiveDirectory::COMMON_NAME]);
401+
return Config::get('adldap_auth.sync_attributes', ['name' => $this->getSchema()->commonName()]);
373402
}
374403

375404
/**
@@ -393,4 +422,14 @@ protected function getLoginFallback()
393422
{
394423
return Config::get('adldap_auth.login_fallback', false);
395424
}
425+
426+
/**
427+
* Retrieves the default connection name from the configuration.
428+
*
429+
* @return mixed
430+
*/
431+
protected function getDefaultConnectionName()
432+
{
433+
return Config::get('adldap_auth.connection', 'default');
434+
}
396435
}

src/AdldapServiceProvider.php

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
namespace Adldap\Laravel;
44

55
use Adldap\Adldap;
6+
use Adldap\Connections\Configuration;
7+
use Adldap\Connections\Manager;
8+
use Adldap\Contracts\AdldapInterface;
69
use Adldap\Laravel\Exceptions\ConfigurationMissingException;
10+
use Adldap\Connections\Provider;
711
use Illuminate\Contracts\Foundation\Application;
812
use Illuminate\Support\ServiceProvider;
913

@@ -37,31 +41,46 @@ public function register()
3741
{
3842
// Bind the Adldap instance to the IoC
3943
$this->app->bind('adldap', function (Application $app) {
40-
$config = $app->make('config');
44+
$config = $app->make('config')->get('adldap');
4145

42-
$settings = $config->get('adldap');
43-
44-
// Verify configuration.
45-
if (is_null($settings)) {
46+
// Verify configuration exists.
47+
if (is_null($config)) {
4648
$message = 'Adldap configuration could not be found. Try re-publishing using `php artisan vendor:publish --tag="adldap"`.';
4749

4850
throw new ConfigurationMissingException($message);
4951
}
5052

51-
// Create a new Adldap instance.
52-
$ad = new Adldap($settings['connection_settings'], new $settings['connection'](), $settings['auto_connect']);
53+
// Create a new connection Manager.
54+
$manager = new Manager();
55+
56+
// Retrieve the LDAP connections.
57+
$connections = $config['connections'];
58+
59+
// Go through each connection and construct a Provider.
60+
foreach ($connections as $name => $settings) {
61+
62+
$ldap = new $settings['connection']();
63+
$configuration = new Configuration($settings['connection_settings']);
64+
$schema = new $settings['schema'];
65+
66+
// Construct a new connection Provider with its settings.
67+
$provider = new Provider($ldap, $configuration, $schema);
68+
69+
if ($settings['auto_connect'] === true) {
70+
// Try connecting to the provider if `auto_connect` is true.
71+
$provider->connect();
72+
}
5373

54-
if ($config->get('app.debug')) {
55-
// If the application is set to debug mode, we'll display LDAP error messages.
56-
$ad->getConnection()->showErrors();
74+
// Add the Provider to the Manager.
75+
$manager->add($name, $provider);
5776
}
5877

59-
return $ad;
78+
return new Adldap($manager);
6079
});
6180

6281
// Bind the Adldap contract to the Adldap object
6382
// in the IoC for dependency injection.
64-
$this->app->bind('Adldap\Contracts\Adldap', 'adldap');
83+
$this->app->bind(AdldapInterface::class, 'adldap');
6584
}
6685

6786
/**

src/Config/auth.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22

33
return [
44

5+
/*
6+
|--------------------------------------------------------------------------
7+
| Connection
8+
|--------------------------------------------------------------------------
9+
|
10+
| The connection to use for authentication.
11+
|
12+
| You must specify connections in your `config/adldap.php` configuration file.
13+
|
14+
*/
15+
16+
'connection' => 'default',
17+
18+
519
/*
620
|--------------------------------------------------------------------------
721
| Username Attribute

0 commit comments

Comments
 (0)