Yii2 yii2-adldap-module


Can anybody give me an example of how to implement ldap or yii2-adldap-module in the loginform.php/user module?


yii2-adldap-module is wrapping this module, so you can use it example:


Thanks but I was wondering if anybody could give me any examples of how to amend the user model and login model to work with this extension

you can add validationPassword function to user model, see below:

public function validatePassword($password)


        $ldap_correct = false;

        $connection = ldap_connect('your address', 389);

        ldap_set_option($connection, LDAP_OPT_PROTOCOL_VERSION, 3);

        ldap_set_option($connection, LDAP_OPT_REFERRALS, 0);

        $username = substr($this->email, 0, strpos($this->email, '@'));

        if ($connection) {

            try {

                @$bind = ldap_bind($connection, 'hz' . "\\" . $username, $password);

            } catch (Exception $e) {

                echo $e->getMessage();


            if (!$bind) {

                $ldap_correct = false;

            } else $ldap_correct = true;


        if ($ldap_correct) {

            $this->password = Yii::$app->getSecurity()->generatePasswordHash($password);


            return true;

        } else {

            if ($this->password == '') {


                return false;

            } else {

                if (Yii::$app->getSecurity()->validatePassword($password, $this->password)) {

                    return true;

                } else return false;




Hope it help you


For one of my projects I’m also working on LDAP.

Currently I would suggest to everybody who needs LDAP and reads this:

For now (10/2015) - don’t use:

yii2-ldap-module: https://github.com/edvler/yii2-adldap-module

adldap/adldap: https://github.com/adldap/adLDAP/

Looks like nobody is active working on both of them.

Both are currently not working or buggy and need a lot of fixing.

I ended up directly using:

strebl/adldap = https://github.com/strebl/adLDAP

Since this seems to be the only stable and "composer friendly" solution around.

If anyone has better suggestions on the LDAP topic, please let us know.

Best Regards

Thanks MetaCrawler


Now it’s possible to use a full integration in yii2 with a user model.

See the readme of the yii2-adldap-module.

Search on Github for yii2-adldap-module and look into the readme.md.