LDAP
Bu içerik henüz dilinizde mevcut değil.
LDAP directories for Yii2 through native LdapRecord models
Named connections, user synchronization, group membership, and optional password verification
Features
Section titled “Features”Quick start
Section titled “Quick start”Installation
Section titled “Installation”The package needs the PHP LDAP extension in the runtime that serves the application, not only in the CLI. Install it with Composer:
composer require yii2-extensions/ldap:^0.1Applications on Yii 2.0 need a Bower asset source such as Asset Packagist. The Installation Guide covers it, together with Yii 22 and local checkouts.
One directory, two service accounts. people binds a read-only account for queries and synchronization; memberships
binds a separate account whose LDAP ACL allows it to modify group members. The password verifier is optional and shares
nothing with either of them.
use yii2\extensions\ldap\Directory;use yii2\extensions\ldap\PasswordVerifier;
return [ // Registers the named connections with LdapRecord at bootstrap. No socket opens until the first query. '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'), // Opts this name into setMembership(). Synchronization refuses it. 'writable' => true, ], ], ], // Only when the application itself checks LDAP passwords. Skip it behind Keycloak or another SSO. 'ldapLogin' => [ 'class' => PasswordVerifier::class, 'host' => 'directory.example.org', ], ],];Every connection upgrades to StartTLS and validates the server certificate before it sends a credential. There is no plaintext fallback and referrals are not followed.
use LdapRecord\Models\OpenLDAP\{Group, User};use yii2\extensions\ldap\{Directory, DirectoryUnavailableException, PasswordVerifier, UserSynchronizer};
/** @var Directory $directory */$directory = Yii::$app->get('directory');
// Native LdapRecord models. The connection name selects the directory and the account that reads it.$alex = User::on('people') ->whereEquals('uid', 'alex') ->firstOrFail();
$groups = $alex->groups()->get();
// Copy two attributes into a local table, page by page, on a connection isolated from other queries.// Each row is keyed by the connection name and the entryUUID (objectGUID on Active Directory), never by// username or DN. A page that fails stops the run instead of passing for a complete directory.$count = (new UserSynchronizer($directory))->sync( User::on('people'), ['display_name' => 'cn', 'email' => 'mail'], static function (string $source, string $guid, array $attributes): void { // Values arrive as LdapRecord returns them: a list per attribute, or null when the entry has none. $profile = [ 'display_name' => $attributes['display_name'][0] ?? null, 'email' => $attributes['email'][0] ?? null, ];
Yii::$app->db->createCommand()->upsert( '{{%directory_user}}', ['ldap_source' => $source, 'ldap_guid' => $guid] + $profile, $profile, )->execute(); },);
// One direct membership, one LDAP modify. Both models must be loaded on the writable connection:// a group or user loaded on `people` is refused with InvalidConfigException before anything is sent.$operators = Group::on('memberships')->whereEquals('cn', 'operators')->firstOrFail();$member = User::on('memberships')->whereEquals('uid', 'alex')->firstOrFail();
$directory->setMembership($operators, $member, true);$directory->setMembership($operators, $member, false);
// Optional password check for an identity the application already resolved. One call, three outcomes:// true, false for a rejected password, or an exception when the directory cannot give an answer./** @var PasswordVerifier $verifier */$verifier = Yii::$app->get('ldapLogin');
try { $valid = $verifier->verify($identity->ldapPrincipal, $password);} catch (DirectoryUnavailableException) { $valid = false;}$identity->ldapPrincipal is the bind name the application stores for that user; never take it from the request. The
Usage Examples add queries across two directories
and the full login flow. The
Configuration Guide details certificate
trust, least-privilege accounts and persistent workers.
Documentation
Section titled “Documentation”Practical guides for connecting directories, querying native models, and running the test suite.
The guides and the API reference generated from the
PHPDoc in src/ are published at https://yii2-framework.github.io/docs/extensions/ldap/.
AI assistance
Section titled “AI assistance”AI agents: read the documentation index and fetch the Markdown version
of a page by appending index.md to its URL. The
AI agents guide covers skill installation, client setup and
verification.
- 🤖
yii2-ldapskill: named LDAP connections, user synchronization, direct group membership, and optional password verification. - 🧭
yii2-frameworkskill: Yii 22.0 application conventions.
The hosted documentation covers Yii 22.0. If it does not match the installed revision or is unavailable, use the repository guides, the skill source, the source PHPDoc and the changelog.
Our social networks
Section titled “Our social networks”Follow on X Follow on Facebook Join our Subreddit Join on Telegram
License
Section titled “License”Package
Section titled “Package”- Source code and issues: yii2-extensions/ldap. Star the repository if the package is useful to you.
- Composer package: yii2-extensions/ldap.
- Changelog: CHANGELOG.md.