ldap.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * Created by IntelliJ IDEA.
  4. * User: iaun
  5. * Date: 2019/11/7
  6. * Time: 8:50
  7. */
  8. ini_set("error_reporting", "E_ALL & ~E_NOTICE");
  9. function check_user($user, $password, $groupName)
  10. {
  11. $ldapInfo = new stdClass();
  12. $ldapInfo->host = "192.168.144.144";
  13. $ldapInfo->port = 389;
  14. $ldapInfo->domain = "sk39.org";
  15. $conn = ldap_connect($ldapInfo->host, $ldapInfo->port);
  16. if ($conn) {
  17. $loginUser = $user . '@' . $ldapInfo->domain;
  18. ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3);
  19. ldap_set_option($conn, LDAP_OPT_REFERRALS, 0);
  20. $bind = ldap_bind($conn, $loginUser, $password);
  21. if ($bind) {
  22. $ldap_dn = "CN=Users,DC=sk39,DC=org";
  23. $filter = "(samaccountname=$user)";
  24. $attr = array("memberof");
  25. $results = ldap_search($conn, $ldap_dn, $filter, $attr);
  26. $entries = ldap_get_entries($conn, $results);
  27. if ($entries["count"] == 1) {
  28. $split = explode(",", $entries[0]["memberof"][0]);
  29. $isGroupMember = false;
  30. for ($i = 0, $len = count($split); $i < $len; $i++) {
  31. list($key, $value) = explode("=", $split[$i]);
  32. if ($key == "CN" && $value == $groupName) {
  33. $isGroupMember = true;
  34. break;
  35. }
  36. }
  37. if ($isGroupMember) {
  38. // 成功
  39. return 1;
  40. } else {
  41. // 不在组
  42. return -3;
  43. }
  44. }
  45. } else {
  46. // 密码错误或连接失败
  47. return -2;
  48. }
  49. }
  50. // 其他错误
  51. return -1;
  52. }
  53. //echo check_user("user1", "Skills39.", "PrivilegedGrp");