composer package updates
[openemr.git] / vendor / adldap2 / adldap2 / src / Connections / Provider.php
blob40359862f3c22165d7b74b19f9e90f0b3b0ddabd
1 <?php
3 namespace Adldap\Connections;
5 use InvalidArgumentException;
6 use Adldap\Auth\Guard;
7 use Adldap\Auth\GuardInterface;
8 use Adldap\Schemas\ActiveDirectory;
9 use Adldap\Schemas\SchemaInterface;
10 use Adldap\Models\Factory as ModelFactory;
11 use Adldap\Query\Factory as SearchFactory;
12 use Adldap\Configuration\DomainConfiguration;
14 /**
15 * Class Provider
17 * Contains the LPAP connection and domain configuration to
18 * instantiate factories for retrieving and creating
19 * LDAP records as well as authentication (binding).
21 * @package Adldap\Connections
23 class Provider implements ProviderInterface
25 /**
26 * The providers connection.
28 * @var ConnectionInterface
30 protected $connection;
32 /**
33 * The providers configuration.
35 * @var DomainConfiguration
37 protected $configuration;
39 /**
40 * The providers schema.
42 * @var SchemaInterface
44 protected $schema;
46 /**
47 * The providers auth guard instance.
49 * @var GuardInterface
51 protected $guard;
53 /**
54 * {@inheritdoc}
56 public function __construct($configuration = [], ConnectionInterface $connection = null, SchemaInterface $schema = null)
58 $this->setConfiguration($configuration)
59 ->setConnection($connection)
60 ->setSchema($schema);
63 /**
64 * Close the LDAP connection (if bound) upon destruction.
66 * @return void
68 public function __destruct()
70 if (
71 $this->connection instanceof ConnectionInterface &&
72 $this->connection->isBound()
73 ) {
74 $this->connection->close();
78 /**
79 * {@inheritdoc}
81 public function setConfiguration($configuration = [])
83 if (is_array($configuration)) {
84 $configuration = new DomainConfiguration($configuration);
87 if ($configuration instanceof DomainConfiguration) {
88 $this->configuration = $configuration;
90 return $this;
93 $class = DomainConfiguration::class;
95 throw new InvalidArgumentException(
96 "Configuration must be array or instance of $class"
101 * {@inheritdoc}
103 public function setConnection(ConnectionInterface $connection = null)
105 // We'll create a standard connection if one isn't given.
106 $this->connection = $connection ?: new Ldap();
108 // Prepare the connection.
109 $this->prepareConnection();
111 // Instantiate the LDAP connection.
112 $this->connection->connect(
113 $this->configuration->get('domain_controllers'),
114 $this->configuration->get('port')
117 return $this;
121 * {@inheritdoc}
123 public function setSchema(SchemaInterface $schema = null)
125 $this->schema = $schema ?: new ActiveDirectory();
127 return $this;
131 * {@inheritdoc}
133 public function setGuard(GuardInterface $guard)
135 $this->guard = $guard;
137 return $this;
141 * {@inheritdoc}
143 public function getConfiguration()
145 return $this->configuration;
149 * {@inheritdoc}
151 public function getConnection()
153 return $this->connection;
157 * {@inheritdoc}
159 public function getSchema()
161 return $this->schema;
165 * {@inheritdoc}
167 public function getGuard()
169 if (!$this->guard instanceof GuardInterface) {
170 $this->setGuard($this->getDefaultGuard($this->connection, $this->configuration));
173 return $this->guard;
177 * {@inheritdoc}
179 public function getDefaultGuard(ConnectionInterface $connection, DomainConfiguration $configuration)
181 return new Guard($connection, $configuration);
185 * {@inheritdoc}
187 public function make()
189 return new ModelFactory(
190 $this->search()->getQuery()
195 * {@inheritdoc}
197 public function search()
199 return new SearchFactory(
200 $this->connection,
201 $this->schema,
202 $this->configuration->get('base_dn')
207 * {@inheritdoc}
209 public function auth()
211 return $this->getGuard();
215 * {@inheritdoc}
217 public function connect($username = null, $password = null)
219 // Get the default guard instance.
220 $guard = $this->getGuard();
222 if (is_null($username) && is_null($password)) {
223 // If both the username and password are null, we'll connect to the server
224 // using the configured administrator username and password.
225 $guard->bindAsAdministrator();
226 } else {
227 // Bind to the server with the specified username and password otherwise.
228 $guard->bind($username, $password);
231 return $this;
235 * Prepares the connection by setting configured parameters.
237 * @return void
239 * @throws \Adldap\Configuration\ConfigurationException When configuration options requested do not exist
241 protected function prepareConnection()
243 if ($this->configuration->get('use_ssl')) {
244 $this->connection->ssl();
245 } elseif ($this->configuration->get('use_tls')) {
246 $this->connection->tls();
249 $options = array_replace(
250 $this->configuration->get('custom_options'),
252 LDAP_OPT_PROTOCOL_VERSION => $this->configuration->get('version'),
253 LDAP_OPT_NETWORK_TIMEOUT => $this->configuration->get('timeout'),
254 LDAP_OPT_REFERRALS => $this->configuration->get('follow_referrals')
258 $this->connection->setOptions($options);