1. ทำการติดตั้ง ldap server และ ldap mod สำหรับ php และ config ใน file php.ini ดังนี้
2. config file ชื่อ ldap.php ซึ่งใช้สำหรับ config ค่าต่างๆ ที่จำเป็น
3. Library LDAP ชื่อ Service_ldap.php
4. การใช้งาน
5. ลองทดสอบการใช้งานผ่าน Unit Testing ของ CodeIgniter
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();