# Configuration guide

> Register yii2\extensions\ldap\Directory for named service-account connections, and yii2\extensions\ldap\PasswordVerifier only when the application itself...

Register `yii2\extensions\ldap\Directory` for named service-account connections, and
`yii2\extensions\ldap\PasswordVerifier` only when the application itself checks LDAP passwords. The two components share
no connection or credentials.

## Named directory connections

Register the component in `bootstrap` in each web or console application that uses it. Use a read-only LDAP account for
queries and sync. A writer needs enough read access to look up the intended group and user, plus permission to modify
only the relevant membership attributes.

```php
use yii2\extensions\ldap\Directory;

return [
    'bootstrap' => ['directory'],
    'components' => [
        'directory' => [
            'class' => Directory::class,
            'connections' => [
                'people' => [
                    'host' => 'directory.example.org',
                    'baseDn' => 'dc=example,dc=org',
                    'username' => 'cn=reader,ou=services,dc=example,dc=org',
                    'password' => getenv('APP_LDAP_READ_PASSWORD'),
                ],
                'memberships' => [
                    'host' => 'directory.example.org',
                    'baseDn' => 'dc=example,dc=org',
                    'username' => 'cn=writer,ou=services,dc=example,dc=org',
                    'password' => getenv('APP_LDAP_WRITE_PASSWORD'),
                    'writable' => true,
                ],
            ],
        ],
    ],
];
```

Set both secrets before starting the application. Each entry accepts optional `port` (389) and `timeout` (5 seconds).
`timeout` bounds the network connection and the wait for each directory reply, so raise it for a directory whose
searches take longer. Add another named entry for a different directory. Both read and write connections use verified
StartTLS; use the same client trust configuration as for password verification. Never put service-account credentials in
source control.

Set `port` and `timeout` as integers. `writable` defaults to `false` and must be a boolean. It controls the extension's
membership and sync methods; it does not revoke native LdapRecord write methods. Enforce least privilege with directory
ACLs and authorize each change in the application. Treat LDAP connection names, model classes, query attributes and
scopes as application configuration.

## Persistent workers

Yii registers connections in LdapRecord's process-wide registry when bootstrapping. Choose unique, stable names and
select one explicitly with `Model::on('people')`. Existing registrations are not replaced. Do not switch aliases,
container instances, endpoints or credentials per request in a persistent worker. When needed, call
`$directory->disconnect()` between jobs to release connections; subsequent operations reconnect as the configured
service account. A failed bind closes the handle without running the operation.

Keep this component for the lifetime of a persistent worker, or call `$directory->unregister()` in teardown before
replacing the application. Unregister only after its LDAP work has finished; do not retain its model or query objects
across teardown. Restart the worker when changing endpoints or secrets. Do not log full connection objects, exceptions
or directory records where credentials or personal data could be exposed.

## Password verification

Configure one trusted LDAP DNS hostname, not a URL or a value from the login form. The verifier opens a new connection
for every attempt. It requests LDAP v3, disables referrals, requires a trusted server certificate and upgrades the
connection with StartTLS **before** sending credentials. There is no plaintext fallback. Implicit TLS (LDAPS) is not
supported in this version.

```php
use yii2\extensions\ldap\PasswordVerifier;

return [
    'components' => [
        'ldapLogin' => [
            'class' => PasswordVerifier::class,
            'host' => 'directory.example.org',
            'port' => 389,
            'timeout' => 5,
        ],
    ],
];
```

Install your directory's issuing CA in the LDAP client's trust store and configure it to require certificate
verification. The server certificate must match the configured hostname. Never weaken the host's TLS policy to make
development credentials work. The LDAP client must support setting a required certificate policy on the connection. A
client lacking that capability fails closed.

`timeout` bounds, in seconds, the network connection and the wait for each directory reply; it is not an end-to-end
login deadline. Do not attach passwords to request traces or log the login form. The verifier does not use global
LdapRecord authentication listeners or configure a shared service-account connection.

[Back to documentation](/docs/extensions/ldap/index.md#documentation)