迈瑞ldap用户验证

AndyYang| 阅读:1689 发表时间:2014-04-14 22:09:22 php
摘要:最近离职了,但是踩了坨狗屎,接了一个迈瑞的微信项目,赚了点钱。其中他们要求通过ldap对接自己的用户信息,微信项目这边没有用户的概念。

最近离职了,但是踩了坨狗屎,接了一个迈瑞的微信项目,赚了点钱。其中他们要求通过ldap对接自己的用户信息,微信项目这边没有用户的概念。

开发用的是php,但php默认没有开启ldap支持,需要在php.ini中开启ldap支持。
extension=php_ldap.dll //去掉前面的分号

然后重启服务器,报了一个错,没有找到Libsasl.Dll。
将php中libsasl.dll拷到system32中,再次重启,phpinfo可以看到ldap扩展了。

ldap验证代码如下:

<?php
class adLDAP {

    protected $_account_suffix = "";
    protected $_base_dn = "dc=mindray,dc=com"; 
    protected $host = array ("127.0.0.1");
    protected $port = '389';
    protected $_conn;

    function __construct($options=array()){

        if (count($options)>0){
            if (array_key_exists("base_dn",$options)){ $this->_base_dn=$options["base_dn"]; }
            if (array_key_exists("host",$options)){ $this->host=$options["host"]; }
            if (array_key_exists("port",$options)){ $this->port=$options["port"]; }
        }

        if ($this->ldap_supported() === false) {
            echo 'ldap not supported';
            return false;
        }

        return $this->connect();

    }

    function __destruct(){ 

        $this->close(); 

    }

    public function connect() {

        $host = $this->random_host();
        $port = $this->port;
        $this->_conn = ldap_connect($host,$port);

        return (true);

    }

    public function close() {

        ldap_close ($this->_conn);

    }

    /**
    * Validate a user's login credentials
    * 
    * @param string $username A user's AD username
    * @param string $password A user's AD password
    * @param bool optional $prevent_rebind
    * @return bool
    */
    public function authenticate($username, $password) {

        if (empty($username) || empty($password)) return false; 

        $r = @ldap_search( $this->_conn, $this->_base_dn, 'uid=' . $username);
        if ($r) {
            $result = @ldap_get_entries( $this->_conn, $r);
            if ($result[0]) {
                if (@ldap_bind( $this->_conn, $result[0]['dn'], $password) ) {
                    //return $result[0];
                    return true;
                 }
             }
         }

    }

    protected function random_host(){

        mt_srand(doubleval(microtime()) * 100000000); // For older PHP versions

        return ($this->host[array_rand($this->host)]);

    }

    protected function ldap_supported() {

        if (!function_exists('ldap_connect')) {
            return (false);   
        }

        return (true);

    }

}

ps:为有需要的人士分享!

本文为AndyYang原创,转载请注明出处!
如果您觉得好,可以打赏作者:
如果您觉得累了,是否想看点美女养养眼:猛戳>>朋友帮
如果您觉得皮了,是否想来点神吐槽:猛戳>>iPhone查询中

已有1条评论

昵称:
邮箱:

  • 最新评论

java屌丝2014-08-18 22:29:58
有点意思,^_^!
iPhone查询中 - bbs.ipcxz.com 朋友帮 - www.pengyb.cn iPhone查询中 - bbs.ipcxz.com
反馈
微信订阅号