# Directory

> Registers named service-account connections for native LdapRecord models and explicit membership changes.

[API reference](/docs/api/index.md) › [LDAP](/docs/extensions/ldap/reference/index.md) › Directory

| class | `yii2\extensions\ldap\Directory` |
| --- | --- |
| Extends | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\BootstrapInterface`](/docs/api/yii/base/bootstrapinterface/index.md), [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Source | [`src/Directory.php`](https://github.com/yii2-extensions/ldap/blob/main/src/Directory.php#L41) |

Registers named service-account connections for native LdapRecord models and explicit membership changes.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-connections"></span>`$connections` | `array<array-key, mixed>` | Trusted application configuration keyed by connection name. Default: `[]`. | `Directory` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/baseobject/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/baseobject/index.md#method-__get) | Returns the value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/baseobject/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/baseobject/index.md#method-__set) | Sets value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/baseobject/index.md#method-__unset) | Sets an object property to null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`bootstrap()`](#method-bootstrap) | Registers this application's connections without replacing any existing connection or changing the default. | `Directory` |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`disconnect()`](#method-disconnect) | Closes this component's open LDAP handles without changing the registry. | `Directory` |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/baseobject/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/baseobject/index.md#method-hasProperty) | Returns a value indicating whether a property is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`isReadConnection()`](#method-isReadConnection) | Confirms that a query uses one of this component's read connections. | `Directory` |
| [`setMembership()`](#method-setMembership) | Adds or removes one direct membership through an explicitly writable connection. | `Directory` |
| [`unregister()`](#method-unregister) | Releases owned names and closes their handles before the application is recreated. | `Directory` |
| [`createConnection()`](#method-createConnection) protected | Creates an unconnected LdapRecord connection using this package's service-account transport. | `Directory` |

## Method details

### bootstrap() <span id="method-bootstrap"></span>

```php
public function bootstrap(yii\base\Application $app): void
```

Registers this application's connections without replacing any existing connection or changing the default.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$app` | [`yii\base\Application`](/docs/api/yii/base/application/index.md) | Application bootstrapping this component. |

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if a name or endpoint is invalid or the name is already registered.

### disconnect() <span id="method-disconnect"></span>

```php
public function disconnect(): void
```

Closes this component's open LDAP handles without changing the registry.

Subsequent operations reconnect as the original configured service account. Every handle is closed even if one
of them fails; the first failure is then rethrown.

**Throws**

- [`Throwable`](https://www.php.net/manual/en/class.throwable.php): if a connection cannot be closed.

### isReadConnection() <span id="method-isReadConnection"></span>

```php
public function isReadConnection(string $name, LdapRecord\Connection $connection): bool
```

Confirms that a query uses one of this component's read connections.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | Name used to register the connection. |
| `$connection` | `LdapRecord\Connection` | Connection instance used by the query. |

**Returns**

`bool`: Whether the registry still owns the exact connection and it is not enabled for writes.

### setMembership() <span id="method-setMembership"></span>

```php
public function setMembership(LdapRecord\Models\ActiveDirectory\Group|LdapRecord\Models\OpenLDAP\Group $group, LdapRecord\Models\ActiveDirectory\User|LdapRecord\Models\OpenLDAP\User $user, bool $member): bool
```

Adds or removes one direct membership through an explicitly writable connection.

Both persisted models must be loaded on that connection and use the same supported schema. This does not change
AD primary groups, follow nested groups, replace an entire member list, or delete any directory entry.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$group` | `LdapRecord\Models\ActiveDirectory\Group\|LdapRecord\Models\OpenLDAP\Group` | Persisted group to modify. |
| `$user` | `LdapRecord\Models\ActiveDirectory\User\|LdapRecord\Models\OpenLDAP\User` | Persisted user whose direct membership is changed. |
| `$member` | `bool` | Whether to add the membership instead of removing it. |

**Returns**

`bool`: Whether the LDAP modification succeeded.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if the models or write connection do not meet those requirements.
- [`Exception`](https://www.php.net/manual/en/class.exception.php): if LdapRecord cannot bind or perform the requested write.

### unregister() <span id="method-unregister"></span>

```php
public function unregister(): void
```

Releases owned names and closes their handles before the application is recreated.

Does not remove another component's replacement connection. Call only after the application
has stopped using native LDAP models and queries.

### createConnection() <span id="method-createConnection"></span>

```php
protected function createConnection(yii2\extensions\ldap\Endpoint $endpoint): LdapRecord\Connection
```

Creates an unconnected LdapRecord connection using this package's service-account transport.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$endpoint` | `yii2\extensions\ldap\Endpoint` | Validated settings for one connection. |

**Returns**

`LdapRecord\Connection`: Service connection with no open LDAP handle.