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

Commit d488282

Browse files
committed
Only log exceptions when logging is enabled
#737
1 parent 0bfd446 commit d488282

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

src/AdldapServiceProvider.php

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

55
use Adldap\Adldap;
6+
use Adldap\AdldapException;
67
use Adldap\AdldapInterface;
7-
use Adldap\Auth\BindException;
88
use Adldap\Connections\Provider;
99
use Adldap\Connections\ConnectionInterface;
10-
use Adldap\Connections\ConnectionException;
1110
use Illuminate\Container\Container;
1211
use Illuminate\Support\Facades\Config;
1312
use Illuminate\Support\ServiceProvider;
1413

1514
class AdldapServiceProvider extends ServiceProvider
1615
{
1716
/**
18-
* We'll defer loading this service provider so our LDAP connection
19-
* isn't instantiated unless requested to speed up our application.
17+
* We'll defer loading this service provider so our
18+
* LDAP connection isn't instantiated unless
19+
* requested to speed up our application.
2020
*
2121
* @var bool
2222
*/
@@ -29,7 +29,7 @@ class AdldapServiceProvider extends ServiceProvider
2929
*/
3030
public function boot()
3131
{
32-
if (Config::get('ldap.logging')) {
32+
if ($this->isLogging()) {
3333
Adldap::setLogger(logger());
3434
}
3535

@@ -85,12 +85,12 @@ public function provides()
8585
* If a provider is configured to auto connect,
8686
* this method will throw a BindException.
8787
*
88-
* @param Adldap $adldap
88+
* @param Adldap $ldap
8989
* @param array $connections
9090
*
9191
* @return Adldap
9292
*/
93-
protected function addProviders(Adldap $adldap, array $connections = [])
93+
protected function addProviders(AdldapInterface $ldap, array $connections = [])
9494
{
9595
// Go through each connection and construct a Provider.
9696
foreach ($connections as $name => $config) {
@@ -100,21 +100,24 @@ protected function addProviders(Adldap $adldap, array $connections = [])
100100
new $config['connection']
101101
);
102102

103+
// If auto connect is enabled, an attempt will be made to bind to
104+
// the LDAP server with the configured credentials. If this
105+
// fails then the exception will be logged (if enabled).
103106
if ($this->shouldAutoConnect($config)) {
104107
try {
105108
$provider->connect();
106-
} catch (BindException $e) {
107-
logger()->error($e);
108-
} catch (ConnectionException $e) {
109-
logger()->error($e);
109+
} catch (AdldapException $e) {
110+
if ($this->isLogging()) {
111+
logger()->error($e);
112+
}
110113
}
111114
}
112115

113-
// Add the provider to the Adldap container.
114-
$adldap->addProvider($provider, $name);
116+
// Add the provider to the LDAP container.
117+
$ldap->addProvider($provider, $name);
115118
}
116119

117-
return $adldap;
120+
return $ldap;
118121
}
119122

120123
/**
@@ -128,9 +131,9 @@ protected function newAdldap()
128131
}
129132

130133
/**
131-
* Returns a new Provider instance.
134+
* Returns a new LDAP Provider instance.
132135
*
133-
* @param array $configuration
136+
* @param array $configuration
134137
* @param ConnectionInterface|null $connection
135138
*
136139
* @return Provider
@@ -141,7 +144,7 @@ protected function newProvider($configuration = [], ConnectionInterface $connect
141144
}
142145

143146
/**
144-
* Determine if the given settings is configured for auto-connecting.
147+
* Determines if the given settings has auto connect enabled.
145148
*
146149
* @param array $settings
147150
*
@@ -154,7 +157,17 @@ protected function shouldAutoConnect(array $settings)
154157
}
155158

156159
/**
157-
* Determines if the current application is Lumen.
160+
* Determines whether logging is enabled.
161+
*
162+
* @return bool
163+
*/
164+
protected function isLogging()
165+
{
166+
return Config::get('ldap.logging', false);
167+
}
168+
169+
/**
170+
* Determines if the current application is a Lumen instance.
158171
*
159172
* @return bool
160173
*/

0 commit comments

Comments
 (0)