迈瑞ldap用户验证

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

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

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

ldap验证代码如下:

 
 
  1. <?php
  2. class adLDAP {
  3.  
  4. protected $_account_suffix = "";
  5. protected $_base_dn = "dc=mindray,dc=com";
  6. protected $host = array ("127.0.0.1");
  7. protected $port = '389';
  8. protected $_conn;
  9.  
  10. function __construct($options=array()){
  11.  
  12. if (count($options)>0){
  13. if (array_key_exists("base_dn",$options)){ $this->_base_dn=$options["base_dn"]; }
  14. if (array_key_exists("host",$options)){ $this->host=$options["host"]; }
  15. if (array_key_exists("port",$options)){ $this->port=$options["port"]; }
  16. }
  17.  
  18. if ($this->ldap_supported() === false) {
  19. echo 'ldap not supported';
  20. return false;
  21. }
  22.  
  23. return $this->connect();
  24.  
  25. }
  26.  
  27. function __destruct(){
  28.  
  29. $this->close();
  30.  
  31. }
  32.  
  33. public function connect() {
  34.  
  35. $host = $this->random_host();
  36. $port = $this->port;
  37. $this->_conn = ldap_connect($host,$port);
  38.  
  39. return (true);
  40.  
  41. }
  42.  
  43. public function close() {
  44.  
  45. ldap_close ($this->_conn);
  46.  
  47. }
  48.  
  49. /**
  50. * Validate a user's login credentials
  51. *
  52. * @param string $username A user's AD username
  53. * @param string $password A user's AD password
  54. * @param bool optional $prevent_rebind
  55. * @return bool
  56. */
  57. public function authenticate($username, $password) {
  58.  
  59. if (empty($username) || empty($password)) return false;
  60.  
  61. $r = @ldap_search( $this->_conn, $this->_base_dn, 'uid=' . $username);
  62. if ($r) {
  63. $result = @ldap_get_entries( $this->_conn, $r);
  64. if ($result[0]) {
  65. if (@ldap_bind( $this->_conn, $result[0]['dn'], $password) ) {
  66. //return $result[0];
  67. return true;
  68. }
  69. }
  70. }
  71.  
  72. }
  73.  
  74. protected function random_host(){
  75.  
  76. mt_srand(doubleval(microtime()) * 100000000); // For older PHP versions
  77.  
  78. return ($this->host[array_rand($this->host)]);
  79.  
  80. }
  81.  
  82. protected function ldap_supported() {
  83.  
  84. if (!function_exists('ldap_connect')) {
  85. return (false);
  86. }
  87.  
  88. return (true);
  89.  
  90. }
  91.  
  92. }

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

更多请关注:http://www.webyang.net/Html/web/article_107.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值