This repository was archived by the owner on Jul 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathAdldapServiceProvider.php
182 lines (160 loc) · 4.61 KB
/
AdldapServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
namespace Adldap\Laravel;
use Adldap\Adldap;
use Adldap\AdldapException;
use Adldap\AdldapInterface;
use Adldap\Connections\ConnectionInterface;
use Adldap\Connections\Provider;
use Illuminate\Container\Container;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
class AdldapServiceProvider extends ServiceProvider
{
/**
* We'll defer loading this service provider so our
* LDAP connection isn't instantiated unless
* requested to speed up our application.
*
* @var bool
*/
protected $defer = true;
/**
* Run service provider boot operations.
*
* @return void
*/
public function boot()
{
if ($this->isLogging()) {
Adldap::setLogger(logger());
}
if ($this->isLumen()) {
return;
}
if ($this->app->runningInConsole()) {
$config = __DIR__.'/Config/config.php';
$this->publishes([
$config => config_path('ldap.php'),
]);
}
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
// Bind the Adldap contract to the Adldap object
// in the IoC for dependency injection.
$this->app->singleton(AdldapInterface::class, function (Container $app) {
$config = $app->make('config')->get('ldap');
// Verify configuration exists.
if (is_null($config)) {
$message = 'Adldap configuration could not be found. Try re-publishing using `php artisan vendor:publish`.';
throw new \RuntimeException($message);
}
return $this->addProviders($this->newAdldap(), $config['connections']);
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [AdldapInterface::class];
}
/**
* Adds providers to the specified Adldap instance.
*
* If a provider is configured to auto connect,
* this method will throw a BindException.
*
* @param Adldap $ldap
* @param array $connections
*
* @return Adldap
*/
protected function addProviders(AdldapInterface $ldap, array $connections = [])
{
// Go through each connection and construct a Provider.
foreach ($connections as $name => $config) {
// Create a new connection with its configured name.
$connection = new $config['connection']($name);
// Create a new provider.
$provider = $this->newProvider(
$config['settings'],
$connection
);
// If auto connect is enabled, an attempt will be made to bind to
// the LDAP server with the configured credentials. If this
// fails then the exception will be logged (if enabled).
if ($this->shouldAutoConnect($config)) {
try {
$provider->connect();
} catch (AdldapException $e) {
if ($this->isLogging()) {
logger()->error($e);
}
}
}
// Add the provider to the LDAP container.
$ldap->addProvider($provider, $name);
}
return $ldap;
}
/**
* Returns a new Adldap instance.
*
* @return Adldap
*/
protected function newAdldap()
{
return new Adldap();
}
/**
* Returns a new LDAP Provider instance.
*
* @param array $configuration
* @param ConnectionInterface|null $connection
*
* @return Provider
*/
protected function newProvider($configuration = [], ConnectionInterface $connection = null)
{
return new Provider($configuration, $connection);
}
/**
* Determines if the given settings has auto connect enabled.
*
* @param array $settings
*
* @return bool
*/
protected function shouldAutoConnect(array $settings)
{
return array_key_exists('auto_connect', $settings)
&& $settings['auto_connect'] === true;
}
/**
* Determines whether logging is enabled.
*
* @return bool
*/
protected function isLogging()
{
return Config::get('ldap.logging', false);
}
/**
* Determines if the current application is a Lumen instance.
*
* @return bool
*/
protected function isLumen()
{
return Str::contains($this->app->version(), 'Lumen');
}
}