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

Commit 8017039

Browse files
committed
Refactoring AuthServiceProvider
1 parent fb0738b commit 8017039

File tree

1 file changed

+37
-57
lines changed

1 file changed

+37
-57
lines changed

src/AdldapAuthServiceProvider.php

Lines changed: 37 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
namespace Adldap\Laravel;
44

5+
use Adldap\AdldapInterface;
56
use InvalidArgumentException;
6-
use Adldap\Laravel\Facades\Adldap;
77
use Adldap\Laravel\Resolvers\UserResolver;
88
use Adldap\Laravel\Resolvers\ResolverInterface;
99
use Adldap\Laravel\Commands\Console\Import;
1010
use Adldap\Laravel\Auth\DatabaseUserProvider;
11-
use Adldap\Laravel\Auth\NoDatabaseUserProvider;
12-
use Adldap\Laravel\Listeners\BindsLdapUserModel;
1311
use Illuminate\Support\Facades\Auth;
1412
use Illuminate\Support\Facades\Event;
1513
use Illuminate\Support\Facades\Config;
@@ -28,7 +26,6 @@ public function boot()
2826
{
2927
$config = __DIR__.'/Config/auth.php';
3028

31-
// Add publishable configuration.
3229
$this->publishes([
3330
$config => config_path('adldap_auth.php'),
3431
], 'adldap');
@@ -39,11 +36,11 @@ public function boot()
3936

4037
if (method_exists($auth, 'provider')) {
4138
$auth->provider('adldap', function ($app, array $config) {
42-
return $this->newUserProvider($app['hash'], $config);
39+
return $this->makeUserProvider($app['hash'], $config);
4340
});
4441
} else {
4542
$auth->extend('adldap', function ($app) {
46-
return $this->newUserProvider($app['hash'], $app['config']['auth']);
43+
return $this->makeUserProvider($app['hash'], $app['config']['auth']);
4744
});
4845
}
4946

@@ -80,7 +77,9 @@ public function provides()
8077
protected function registerBindings()
8178
{
8279
$this->app->bind(ResolverInterface::class, function () {
83-
return $this->newUserResolver();
80+
$ad = $this->app->make(AdldapInterface::class);
81+
82+
return new UserResolver($ad);
8483
});
8584
}
8685

@@ -91,7 +90,18 @@ protected function registerBindings()
9190
*/
9291
protected function registerListeners()
9392
{
94-
Event::listen(Authenticated::class, BindsLdapUserModel::class);
93+
// Here we will register the event listener that will bind the users LDAP
94+
// model to their Eloquent model upon authentication (if configured).
95+
// This allows us to utilize their LDAP model right
96+
// after authentication has passed.
97+
Event::listen(Authenticated::class, Listeners\BindsLdapUserModel::class);
98+
99+
if ($this->isLogging()) {
100+
Event::listen(Events\Importing::class, Listeners\LogImport::class);
101+
Event::listen(Events\Synchronized::class, Listeners\LogSynchronized::class);
102+
Event::listen(Events\Synchronizing::class, Listeners\LogSynchronizing::class);
103+
Event::listen(Events\DiscoveredWithCredentials::class, Listeners\LogDiscovery::class);
104+
}
95105
}
96106

97107
/**
@@ -100,69 +110,39 @@ protected function registerListeners()
100110
* @param Hasher $hasher
101111
* @param array $config
102112
*
103-
* @return \Illuminate\Contracts\Auth\UserProvider
104-
*
105113
* @throws InvalidArgumentException
114+
*
115+
* @return \Illuminate\Contracts\Auth\UserProvider
106116
*/
107-
protected function newUserProvider(Hasher $hasher, array $config)
117+
protected function makeUserProvider(Hasher $hasher, array $config)
108118
{
109-
$provider = $this->userProvider();
119+
$provider = Config::get('adldap_auth.provider', DatabaseUserProvider::class);
110120

111-
switch ($provider) {
112-
case DatabaseUserProvider::class:
113-
if ($model = array_get($config, 'model')) {
114-
return new $provider($hasher, $model);
115-
}
121+
// The DatabaseUserProvider has some extra dependencies needed,
122+
// so we will validate that we have them before
123+
// constructing a new instance.
124+
if ($provider == DatabaseUserProvider::class) {
125+
$model = array_get($config, 'model');
116126

127+
if (!$model) {
117128
throw new InvalidArgumentException(
118129
"No model is configured. You must configure a model to use with the {$provider}."
119130
);
120-
case NoDatabaseUserProvider::class:
121-
return new $provider;
122-
}
131+
}
123132

124-
throw new InvalidArgumentException(
125-
"The configured Adldap provider [{$provider}] is not supported or does not exist."
126-
);
127-
}
128-
129-
/**
130-
* Returns a new user resolver.
131-
*
132-
* @return ResolverInterface
133-
*/
134-
protected function newUserResolver()
135-
{
136-
return new UserResolver($this->ldapProvider());
137-
}
138-
139-
/**
140-
* Retrieves a connection provider from the Adldap instance.
141-
*
142-
* @return \Adldap\Connections\ProviderInterface
143-
*/
144-
protected function ldapProvider()
145-
{
146-
return Adldap::getProvider($this->connection());
147-
}
148-
149-
/**
150-
* Returns the configured user provider class.
151-
*
152-
* @return string
153-
*/
154-
protected function userProvider()
155-
{
156-
return Config::get('adldap_auth.provider', DatabaseUserProvider::class);
133+
return new $provider($hasher, $model);
134+
}
135+
136+
return new $provider;
157137
}
158138

159139
/**
160-
* Returns the configured default connection name.
140+
* Determines if authentication requests are logged.
161141
*
162-
* @return string
142+
* @return bool
163143
*/
164-
protected function connection()
144+
protected function isLogging()
165145
{
166-
return Config::get('adldap_auth.connection', 'default');
146+
return Config::get('adldap_auth.logging', false);
167147
}
168148
}

0 commit comments

Comments
 (0)