nohnaimer / yii2-ldaprecord
Yii2 扩展库 LdapRecord
0.3.1
2023-07-11 07:36 UTC
Requires
- php: >=8.1
- directorytree/ldaprecord: 3.1.*
- yiisoft/yii2: 2.0.*
Requires (Dev)
- roave/security-advisories: dev-latest
README
安装
建议通过 composer 安装此扩展。
php composer.phar require --prefer-dist nohnaimer/yii2-ldaprecord "*"
或添加
"nohnaimer/yii2-ldaprecord": "*"
到你的 composer.json
文件的 require 部分。
配置
'components' => [ ... 'ldap' => [ 'class' => nohnaimer\ldaprecord\LdapRecord::class, 'providers' => [ 'ad' => [ 'class' => nohnaimer\ldaprecord\LdapRecord::class, // Mandatory Configuration Options 'hosts' => ['192.168.1.1'], 'base_dn' => 'dc=local,dc=com', 'username' => 'admin@local.com', 'password' => 'password', // Optional Configuration Options 'port' => 389, 'follow_referrals' => false, 'use_ssl' => false, 'use_tls' => false, 'version' => 3, 'timeout' => 5, // Custom LDAP Options 'options' => [ // See: https://php.ac.cn/ldap_set_option LDAP_OPT_X_TLS_REQUIRE_CERT => LDAP_OPT_X_TLS_HARD ], ], 'ldap' => [ 'hosts' => ['192.168.1.2'], 'base_dn' => 'dc=local,dc=com', 'username' => 'cn=admin,dc=mts,dc=by', 'password' => 'password', // Optional Configuration Options 'port' => 389, 'version' => 3, // Custom LDAP Options 'options' => [ // See: https://php.ac.cn/ldap_set_option LDAP_OPT_X_TLS_REQUIRE_CERT => LDAP_OPT_X_TLS_HARD ], ], ], ], ... ],
用法
不带用户模型的简单用法
查询
Yii::$app->ldap->initProvider('ad')->query()->where('cn', '=', 'John Doe')->get();
认证
Yii::$app->ldap->initProvider('ad')->auth()->attempt('username', 'password', true);
带模型的简单用法
模型
class User extends \LdapRecord\Models\ActiveDirectory\User { /** * The "booting" method of the model. * @throws \LdapRecord\Auth\BindException * @throws \LdapRecord\ConnectionException */ protected static function boot() { Yii::$app->ldap->initProvider('ad'); } protected $connection = 'ad'; }
$user = User::findByGuid('guid');