1. Go to this page and download the library: Download commifreak/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/ */
public function init()
{
parent::init(); // TODO: Change the autogenerated stub
$this->location = 0; // Set it to the first entry, which is now always autodetected
}
public function login()
{
if (!$this->validate()) {
return false;
}
// At this point we filled the model - now we ask the LDAP if the entered data is correct
/** @var LdapAuth $ldap */
$ldap = Yii::$app->ldap;
if ($ldap->login($this->username, $this->password, $this->location)) { // or if ($ldap->login($this->username, $this->password, $this->location, true)) if you want to use `$fetchUserDN`-feature!
$userData = $ldap->fetchUserData();
if ($userData) {
$user = User::find()->where(['sid' => $userData['sid']])->one();
if (!$user) {
$user = new User();
$user->sid = $userData['sid'];
}
$user->email = $userData['mail'];
$user->firstname = $userData['givenname'];
$user->lastname = $userData['sn'];
$user->phone = $userData['telephonenumber'];
$user->validate();
$user->save();
return Yii::$app->user->login($user, $this->rememberMe ? 3600 * 24 * 30 : 0);
}
} else {
Yii::$app->session->setFlash('error', 'Login gescheitert!');
}
}
/** @var LdapAuth $ldap */
$ldap = \Yii::$app->ldap;
if (!$ldap->login(\Yii::$app->user->identity->sid, $this->userPassword, false, true)) {
return false;
}
$updateAttrs = [];
if (!empty($this->phone)) {
$updateAttrs['telephonenumber'] = $this->phone;
}
if (!empty($this->room)) {
$updateAttrs['physicaldeliveryofficename'] = $this->room;
}
if (!$ldap->updateAttributes($updateAttrs)) {
return false;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.