stmswitcher/yii2-ldap-auth

一个简单的库,用于处理 Yii 2 应用程序中的 LDAP 身份验证。

安装数: 2,863

依赖者: 0

建议者: 0

安全性: 0

星星: 2

关注者: 1

分支: 4

开放性问题: 3

类型:yii2-extension

0.2 2024-05-16 15:37 UTC

This package is auto-updated.

Last update: 2024-09-16 16:24:28 UTC


README

这是一个简单的扩展,用于处理 Yii 2 应用程序中的 LDAP 身份验证。

此扩展旨在用于仅依赖 LDAP 验证的应用程序,且不支持访问令牌。

安装

composer require "stmswitcher/yii2-ldap-auth"

配置示例和用例

考虑 yii2-app-basic

在配置文件中配置组件并更改用户身份类

'components' => [
    ...
    'ldapAuth' => [
        'class' => '\stmswitcher\Yii2LdapAuth\LdapAuth',
        'host' => 'your-ldap-hostname',
        'baseDn' => 'dc=work,dc=group',
        'searchUserName' => '<username for a search user>',
        'searchUserPassword' => '<password for a search user>',

        // optional parameters and their default values
        'ldapVersion' => 3,             // LDAP version
        'protocol' => 'ldaps://',       // Protocol to use           
        'followReferrals' => false,     // If connector should follow referrals
        'port' => 636,                  // Port to connect to
        'loginAttribute' => 'uid',      // Identifying user attribute to look up for
        'ldapObjectClass' => 'person',  // Class of user objects to look up for
        'timeout' => 10,                // Operation timeout, seconds
        'connectTimeout' => 5,          // Connect timeout, seconds
    ],
    ...
    
    'user' => [
        'identityClass' => '\stmswitcher\Yii2LdapAuth\Model\LdapUser',
    ],
    ...
]

更新 LoginForm 类中的方法

use stmswitcher\Yii2LdapAuth\Model\LdapUser;

...

public function validatePassword($attribute, $params)
{
    if (!$this->hasErrors()) {
        $user = LdapUser::findIdentity($this->username);

        if (!$user || !Yii::$app->ldapAuth->authenticate($user->getDn(), $this->password)) {
            $this->addError($attribute, 'Incorrect username or password.');
        }
    }
}

...

public function login()
{
    if ($this->validate()) {
        return Yii::$app->user->login(
            LdapUser::findIdentity($this->username),
            $this->rememberMe
                ? 3600*24*30 : 0
        );
    }
    return false;
}

验证用户是否属于 LDAP 群组

如果您还需要检查用户是否是某个 LDAP 群组的成员,请在 authenticate 函数中使用另一个参数

Yii::$app->ldapAuth->authenticate($user->getDn(), $this->password, 'cn=auth-user-group')

现在您可以使用 LDAP 凭据登录到您的应用程序。