3 namespace Adldap\Connections
;
5 use InvalidArgumentException
;
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
;
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
26 * The providers connection.
28 * @var ConnectionInterface
30 protected $connection;
33 * The providers configuration.
35 * @var DomainConfiguration
37 protected $configuration;
40 * The providers schema.
42 * @var SchemaInterface
47 * The providers auth guard instance.
56 public function __construct($configuration = [], ConnectionInterface
$connection = null, SchemaInterface
$schema = null)
58 $this->setConfiguration($configuration)
59 ->setConnection($connection)
64 * Close the LDAP connection (if bound) upon destruction.
68 public function __destruct()
71 $this->connection
instanceof ConnectionInterface
&&
72 $this->connection
->isBound()
74 $this->connection
->close();
81 public function setConfiguration($configuration = [])
83 if (is_array($configuration)) {
84 $configuration = new DomainConfiguration($configuration);
87 if ($configuration instanceof DomainConfiguration
) {
88 $this->configuration
= $configuration;
93 $class = DomainConfiguration
::class;
95 throw new InvalidArgumentException(
96 "Configuration must be array or instance of $class"
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')
123 public function setSchema(SchemaInterface
$schema = null)
125 $this->schema
= $schema ?
: new ActiveDirectory();
133 public function setGuard(GuardInterface
$guard)
135 $this->guard
= $guard;
143 public function getConfiguration()
145 return $this->configuration
;
151 public function getConnection()
153 return $this->connection
;
159 public function getSchema()
161 return $this->schema
;
167 public function getGuard()
169 if (!$this->guard
instanceof GuardInterface
) {
170 $this->setGuard($this->getDefaultGuard($this->connection
, $this->configuration
));
179 public function getDefaultGuard(ConnectionInterface
$connection, DomainConfiguration
$configuration)
181 return new Guard($connection, $configuration);
187 public function make()
189 return new ModelFactory(
190 $this->search()->getQuery()
197 public function search()
199 return new SearchFactory(
202 $this->configuration
->get('base_dn')
209 public function auth()
211 return $this->getGuard();
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();
227 // Bind to the server with the specified username and password otherwise.
228 $guard->bind($username, $password);
235 * Prepares the connection by setting configured parameters.
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);