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

Commit 9c58cb3

Browse files
committed
Retrieve model from configuration, or the configured auth providers
- Closes #645
1 parent 2e9e87f commit 9c58cb3

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/AdldapAuthServiceProvider.php

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

33
namespace Adldap\Laravel;
44

5+
use RuntimeException;
56
use Adldap\AdldapInterface;
67
use Adldap\Laravel\Resolvers\UserResolver;
78
use Adldap\Laravel\Resolvers\ResolverInterface;
@@ -81,7 +82,7 @@ public function register()
8182
* @param Hasher $hasher
8283
* @param array $config
8384
*
84-
* @throws \RuntimeException
85+
* @throws RuntimeException
8586
*
8687
* @return \Illuminate\Contracts\Auth\UserProvider
8788
*/
@@ -90,12 +91,14 @@ protected function makeUserProvider(Hasher $hasher, array $config)
9091
$provider = Config::get('ldap_auth.provider', DatabaseUserProvider::class);
9192

9293
// The DatabaseUserProvider requires a model to be configured
93-
// in the configuration. We'll validate this here.
94+
// in the configuration. We will validate this here.
9495
if (is_a($provider, DatabaseUserProvider::class, $allowString = true)) {
95-
$model = array_get($config, 'model');
96+
// We will try to retrieve their model from the config file,
97+
// otherwise we will try to use the providers config array.
98+
$model = Config::get('ldap_auth.model') ?? array_get($config, 'model');
9699

97100
if (!$model) {
98-
throw new \RuntimeException(
101+
throw new RuntimeException(
99102
"No model is configured. You must configure a model to use with the {$provider}."
100103
);
101104
}

src/Commands/Console/Import.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
namespace Adldap\Laravel\Commands\Console;
44

55
use Exception;
6+
use UnexpectedValueException;
67
use Adldap\Models\User;
78
use Adldap\Laravel\Events\Imported;
89
use Adldap\Laravel\Facades\Resolver;
910
use Adldap\Laravel\Commands\SyncPassword;
1011
use Adldap\Laravel\Commands\Import as ImportUser;
1112
use Illuminate\Console\Command;
1213
use Illuminate\Support\Facades\Bus;
13-
use Illuminate\Support\Facades\Auth;
1414
use Illuminate\Support\Facades\Event;
15+
use Illuminate\Support\Facades\Config;
1516
use Illuminate\Database\Eloquent\Model;
1617

1718
class Import extends Command
@@ -329,7 +330,34 @@ protected function delete(User $user, Model $model)
329330
*/
330331
protected function model() : Model
331332
{
332-
return Auth::getProvider()->createModel();
333+
$model = Config::get('ldap_auth.model') ?? $this->determineModel();
334+
335+
return new $model;
336+
}
337+
338+
/**
339+
* Retrieves the model by checking the configured LDAP authentication providers.
340+
*
341+
* @return string
342+
*
343+
* @throws UnexpectedValueException
344+
*/
345+
protected function determineModel()
346+
{
347+
// Retrieve all of the configured authentication providers that
348+
// use the LDAP driver and have a configured model.
349+
$providers = array_where(Config::get('auth.providers'), function ($value, $key) {
350+
return $value['driver'] == 'ldap' && array_key_exists('model', $value);
351+
});
352+
353+
// Pull the first driver and return a new model instance.
354+
if ($ldap = reset($providers)) {
355+
return $ldap['model'];
356+
}
357+
358+
throw new UnexpectedValueException(
359+
'Unable to retrieve LDAP authentication driver. Did you forget to configure it?'
360+
);
333361
}
334362

335363
/**

0 commit comments

Comments
 (0)