1. Go to this page and download the library: Download stmswitcher/yii2-ldap-auth library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
stmswitcher / yii2-ldap-auth example snippets
'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',
],
...
]
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;
}