CodeIgniter :: LDAP Library

1. ทำการติดตั้ง ldap server และ ldap mod สำหรับ php และ config ใน file php.ini ดังนี้

Quote

extension=php_ldap.dll


2. config file ชื่อ ldap.php ซึ่งใช้สำหรับ config ค่าต่างๆ ที่จำเป็น
$config['ldap_host'] = "ldap.example.com";
$config['ldap_port'] = "389";
$config['account_prefix'] = "CORP\\";
$config['base_dn'] = "OU=example,DC=com";       


3. Library LDAP ชื่อ Service_ldap.php
class Service_ldap {
        private $CI;            /*!< CodeIgniter instance */
        protected $connection = NULL;

        private $host;
        private $port;
        private $account_prefix;

        function __construct() {
                $this->CI = & get_instance();

                $configs = $this->CI->config->load('ldap', true);
                if( array_key_exists( 'ldap_host', $configs ) ) {
                        $this->host = $configs['ldap_host'];
                }
                if( array_key_exists( 'ldap_port', $configs ) ) {
                        $this->port = $configs['ldap_port'];
                }
                if( array_key_exists( 'account_prefix', $configs ) ) {
                        $this->account_prefix = $configs['account_prefix'];
                }
                return $this->connect();
        }

        function __destruct() {
                $this->close();
        }

        /**
         * Connect to LDAP server
         */
        public function connect() {
                try {
                        $this->connection = @ldap_connect( $this->host, $this->ldap_port );
                        if( !$this->connection ) {
                                return false;
                        }
                }catch (Exception $e) {
                        return false;
                }
                return true;
        }

        /**
         * Authenticate to LDAP with username and password
         * @param $username
         * @param $password
         */
        public function authenticate( $username, $password ) {
                try {
                        if( $this->connection ) {
                                $bind = @ldap_bind($this->connection, $this->account_prefix.$username, $password);
                                if( $bind ) {
                                        /** TODO **/
                                } else {
                                        return false;
                                }
                        } else {
                                return false;
                        }
                } catch( Exception $e ) {
                        return false;
                }
                return true;
        }

        /**
         * Close LDAP connection
         */
        public function close() {
                try {
                        if( $this->connection ) {
                                @ldap_close($this->connection);
                        } else {
                                return false;
                        }
                } catch( Exception $e ) {
                        return false;
                }
                return true;
        }
}


4. การใช้งาน
$this->load->library('service_ldap');
$this->service_ldap->authenticate('username', 'password')
$this->service_ldap->close()


5. ลองทดสอบการใช้งานผ่าน Unit Testing ของ CodeIgniter
$this->load->library('unit_test');
$this->load->library('service_ldap');

$this->unit->run( $this->service_ldap->connect(), true, 'Test conect to LDAP Server' );
$this->unit->run( $this->service_ldap->authenticate('fghfg', '555'), true, 'Test authenticate to LDAP Server' );
$this->unit->run( $this->service_ldap->close(), true, 'Test close LDAP connection' );
echo $this->unit->report();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值