duancaozen6066 2015-09-06 15:38
浏览 155

如何在php中配置OpenLDAP dn?

Just learned the OpenLDAP by today and I want to integrate with the PHP. What I did is installing the OpenLDAP this is my structure looks like:

LDAP Structure Screenshot

Than, I want to integrate this LDAP within the PHP, but I don't know how to get the information for this configuration (tried with this with no luck):

protected $baseDn = "dc=maxcrc,dc=com";
protected $dn = "cn=Manager,ou=group,o=accounts,dc=maxcrc,dc=com";
protected $groupOU = "ou=group";
protected $peopleOU = "ou=People";
protected $peopleOU = "ou=People";

Can somebody expertise in this help me? this is my full code for ldapconnection

 <?php
include_once('settings.php');

class LDAP_Connector {
    protected $baseDn = "dc=maxcrc,dc=com";

    // Distinguished name for the admin.
    protected $dn = "cn=Manager,ou=group,o=accounts,dc=maxcrc,dc=com";

    // The ou of groups.
    protected $groupOU = "ou=group";

    // The ou of people.
    protected $peopleOU = "ou=People";

    protected $ldapconn;

    public function __construct() {
        global $ldapHost, $ldapPort, $ldapUser, $ldapPassword;

        message("Creating LDAP connector");
        $this->ldapconn = ldap_connect($ldapHost, $ldapPort);
        if ($this->ldapconn) {
            message("LDAP Connected - ".$this->ldapconn);
        }
        else {
            message("LDAP failed to connect");
        }
    }

    public function authenticate($username, $password) {
        $r = false;
        global $ldapHost, $ldapPort, $ldapUser, $ldapPassword;

        if ($this->ldapconn) {
            $bind = ldap_bind($this->ldapconn, $this->dn, $ldapPassword);
            if ($bind) {
                //$uid = $ldapUser;
                $uid = $username;

                // Filter on the uid.
                $filter = "(mail=". $uid . ")";

                // Return the userPassword.
                $attr = array("userpassword","rpDisabledState");

                // Get the record for the user.
                $result = ldap_search($this->ldapconn, $this->peopleOU . "," . $this->baseDn, $filter, $attr);

                //message( "result = ".print_r($result, true) );

                $entries = ldap_get_entries($this->ldapconn, $result);
                //message( print_r($entries, true));

                $ldapEncodedPW = "{SHA}".base64_encode(pack("H*", $password));

                $userDisabled = $entries[0]["rpdisabledstate"][0];
                message("User ".$username." state is ".$userDisabled);

                if ($userDisabled) {
                    message("username ".$username." is disabled ");
                }

                if ($entries[0]["userpassword"][0]==$ldapEncodedPW && !$userDisabled) {
                    $r = true;
                }
                else {
                    message("Passwords do not match or the account has been disabled.");
                    message("Password (provided) = ".$ldapEncodedPW);
                    message("Password (ldap)     = ".$entries[0]["userpassword"][0]);

                }
            }
        }
        return $r;
    }
}
?>

Hope someone can help me with this, I stacked a week for this stuf :((

Regards, T

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 腾讯企业邮箱邮件可以恢复么
    • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
    • ¥15 错误 LNK2001 无法解析的外部符号
    • ¥50 安装pyaudiokits失败
    • ¥15 计组这些题应该咋做呀
    • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
    • ¥15 让node服务器有自动加载文件的功能
    • ¥15 jmeter脚本回放有的是对的有的是错的
    • ¥15 r语言蛋白组学相关问题
    • ¥15 Python时间序列如何拟合疏系数模型