kadotafig/yii2-ldaprecord

Yii2 扩展,用于 LdapRecord 库

安装: 11

依赖者: 0

推荐者: 0

安全: 0

星标: 0

关注者: 0

分支: 1

类型:yii2-extension

v0.2.5 2021-08-29 23:59 UTC

This package is auto-updated.

Last update: 2024-09-29 06:14:31 UTC


README

LdapRecord 的文档

ZimbraLDAP 的文档

安装

安装此扩展的首选方式是通过 composer

php composer.phar require --prefer-dist kadotafig/yii2-ldaprecord "*"

或者添加

"kadotafig/yii2-ldaprecord": "*"

到你的 composer.json 文件的 require 部分。

配置

'components' => [
...
'ldap' => [
            'class' => kadotafig\ldaprecord\LdapRecord::class,
            'providers' => [
                'ad' => [
                    'class' => kadotafig\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
                    ],
                ],
            ],
            'zimbra' => [
                    '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');