[HELP] LDAP Authentication with database

Hi. I would like to ask for your help. The search result for LDAP is very limited and I have already tried different methods in fixing the login page for my task. The AD credential should be authenticated using LDAP and I have compiled PHP and enabled LDAP in our server. Please check the codes below or see the attached file. Any suggestions on how to make this work would be of great help.

I have included the ldap function (ldapAuth) in User.php.

I have also created a database table named admin’ with fields id (PK, incremental), username and role.

Thanks in advance.

User.php

<?php

namespace app\models;

class User extends \yii\base\Object implements \yii\web\IdentityInterface

{

public &#036;id;


public &#036;username;


public &#036;password;


public &#036;authKey;


public &#036;accessToken;





private static &#036;users = [


    '100' =&gt; [


        'id' =&gt; '100',


        'username' =&gt; 'admin',


        'password' =&gt; 'adm&#33;np@ssword',


        'authKey' =&gt; 'test100key',


        'accessToken' =&gt; '100-token',


    ],                                                                                      


];





public static function findIdentity(&#036;id)


{


    return isset(self::&#036;users[&#036;id]) ? new static(self::&#036;users[&#036;id]) : null;


}





public static function findIdentityByAccessToken(&#036;token, &#036;type = null)


{


    foreach (self::&#036;users as &#036;user) {


        if (&#036;user['accessToken'] === &#036;token) {


            return new static(&#036;user);


        }


    }


    return null;


}





public static function findByUsername(&#036;username)


{


  


    return new static(array ('username' =&gt; &#036;username));


  


    foreach (self::&#036;users as &#036;user) {


        if (strcasecmp(&#036;user['username'], &#036;username) === 0) {


            return new static(&#036;user);


        }


    }


    return null;


}





public function getId()


{


    return &#036;this-&gt;id;


}





public function getAuthKey()


{


    return &#036;this-&gt;authKey;


}





public function validateAuthKey(&#036;authKey)


{


    return &#036;this-&gt;authKey === &#036;authKey;


}





[b]public static function ldapAuth(&#036;username, &#036;password)[/b] {





    &#036;adServer = &quot;ldap://wsmv001.company.com ldap://wsmv002.company.com &quot;;





    try {


        &#036;ldap = ldap_connect(&#036;adServer);


        if (&#33;&#036;ldap) {


            throw new Exception(&quot;Unable to connect to ldap server&quot;);


            echo &quot;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&quot;;


            echo &quot;Not connected&#33;&quot;;


        }


        else{


            echo &quot;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&quot;;


            echo &quot;Connected&#33;&quot;;


        }


       


        if (strpos(&#036;username, '&#092;&#092;') &gt; 0) {


            &#036;parts = explode(&quot;&#092;&#092;&quot;, &#036;username);


            &#036;region = &#036;parts[0];


            &#036;username = &#036;parts[1];


        }


        else {


            &#036;region = &quot;na&quot;;


            &#036;username = &#036;username;


        }





        &#036;ldaprdn = strtoupper(&#036;region) . &quot;&#092;&#092;&quot; . &#036;username;


        &#036;ldap_dn = &quot;dc={&#036;region},dc=vishayint,dc=com&quot;;





        ldap_set_option(&#036;ldap, LDAP_OPT_PROTOCOL_VERSION, 3);


        ldap_set_option(&#036;ldap, LDAP_OPT_REFERRALS, 0);





        &#036;bind = ldap_bind(&#036;ldap, &#036;ldaprdn, &#036;password);


        if (&#33;&#036;bind) {


            throw new Exception(&quot;unable to authenticate&quot;);


        }


        return true;





        // below may be used if/when we use AD groups to define iClock privileges


        echo &quot;Authenticated&#092;r&#092;n&quot;;


        &#036;results = ldap_search(&#036;ldap,&#036;ldap_dn,&quot;(samaccountname={&#036;username})&quot;, array(&quot;memberof&quot;));


        //&#036;results = ldap_search(&#036;ldap,&#036;ldap_dn);


        &#036;entries = ldap_get_entries(&#036;ldap, &#036;results);





        print_r(&#036;entries);


        // No information found, bad user


        if(&#036;entries['count'] == 0) {


            echo &quot;No entries found&#092;r&#092;n&quot;;


        }





        // Get groups and primary group token


        &#036;output = &#036;entries[0]['memberof'];


        &#036;token = &#036;entries[0]['primarygroupid'][0];





        // Remove extraneous first entry


        array_shift(&#036;output);





        // We need to look up the primary group, get list of all groups


        &#036;results2 = ldap_search(&#036;ldap,&#036;ldap_dn,&quot;(objectcategory=group)&quot;,array(&quot;distinguishedname&quot;,&quot;primarygrouptoken&quot;));


        &#036;entries2 = ldap_get_entries(&#036;ldap, &#036;results2);





        // Remove extraneous first entry


        array_shift(&#036;entries2);





        // Loop through and find group with a matching primary group token


        foreach(&#036;entries2 as &#036;e) {


            if(&#036;e['primarygrouptoken'][0] == &#036;token) {


                // Primary group found, add it to output array


                &#036;output[] = &#036;e['distinguishedname'][0];


                // Break loop


                break;


            }


        }





        return &#036;output;


    }


    finally {


        @ldap_close(&#036;ldap);


    }


}

}


LoginForm.php

<?php

namespace app\models;

use Yii;

use yii\base\Model;

class LoginForm extends Model

{

public &#036;username;


public &#036;password;


public &#036;rememberMe = true;





private &#036;_user = false;





public function rules()


{


    return [


        // username and password are both required


        [['username', 'password'], 'required'],


        // rememberMe must be a boolean value


        ['rememberMe', 'boolean'],


        // password is validated by validatePassword()


        ['password', 'validatePassword'],


    ];


}





public function validatePassword(&#036;attribute, &#036;params)


{


    if (&#33;&#036;this-&gt;hasErrors()) {


        &#036;user = &#036;this-&gt;getUser();





        &#036;this-&gt;_user = User::ldapAuth(&#036;user, &#036;password);


    }


}





public function login()


{


    if (&#036;this-&gt;validate()) {


        return Yii::&#036;app-&gt;user-&gt;login(&#036;this-&gt;getUser(), &#036;this-&gt;rememberMe ? 3600*24*30 : 0);


    }


    return false;


}





public function getUser()


{


    if (&#036;this-&gt;_user === false) {


        &#036;this-&gt;_user = User::findByUsername(&#036;this-&gt;username);


    }





    return &#036;this-&gt;_user;


}

}