2014-04-24 5 views
0

AD 서버에 바인딩 할 수 있지만 특정 그룹의 멤버 자격을 검증하는 방법을 이해하는 데 어려움이 있습니다. 내가하고 싶은 일은 사용자가 그룹 "DOMAIN \ IT"의 일부인지 확인하고, 그렇다면 나중에 사용할 수있는 세션 변수를 할당하는 것입니다.PHP 및 LDAP로 AD 그룹 멤버쉽을 확인하십시오.

if (isset($_POST["submit"])){ 

    $ldaprdn = "DOMAIN\\" . $_POST["username"];  // ldap rdn or dn 
    $ldappass = $_POST["password"]; // associated password 
    } else { 
    $ldaprdn = "noUserName";  // ldap rdn or dn 
    $ldappass = "noPassWord"; // associated password 
    } 

    //check login form post submission and blank values 
    if (isset($_POST["submit"])){ 
     if ($_SESSION["blanklogin"] !== "1"){ 
      // connect to ldap server 
      $ldapconn = ldap_connect("DC01.ROOT.DOMAIN.ORG") 
      or die("Could not connect to LDAP server."); 
      if ($ldapconn) { 

      // binding to ldap server 
      $ldapbind = ldap_bind($ldapconn, $ldaprdn, $ldappass); 

      // verify binding 
      if ($ldapbind) { 
       $_SESSION["login"] = "1"; 

    TODO: CHECK GROUP MEMBERSHIP - IF IN GROUP DOMAIN\IT then set session variable. 
       session_regenerate_id(true); 
       echo "LDAP Bind For "; echo $ldaprdn; echo " successful..."; 
         echo "Login Successful"; 
         header("Location: index.php"); 
        } else { 
        echo "LDAP bind for "; echo $ldaprdn; echo " Failed...<br />"; 
        $_SESSION["login"] = "0"; 
        } 
       $_SESSION["blanklogin"] = "0"; 
       ldap_unbind($ldapconn); 
       } 
       } else { 
       echo "Username & Password Required<br />"; 
      } 
      } 

답변

0

다음 코드는 내 프로젝트 중 하나에서 가져온 사용자가 재귀 포함의 구성원 인 그룹 이름의 목록을 반환 : 여기에 지금까지 가지고있는 것입니다. 원하는 것을 확인하기 위해이를 사용할 수 있어야합니다.

$ldapConnection = ldap_connect($ldapServerAddress, $ldapServerPort); 
ldap_set_option($ldapConnection, LDAP_OPT_PROTOCOL_VERSION, 3); 
ldap_set_option($ldapConnection, LDAP_OPT_REFERRALS, 0); 

// Do something to handle connection failure here, this is just what I did. 
if ($ldapConnection === false) throw new ActiveDirectoryConnectionException(); 

$ldapBind = ldap_bind($ldapConnection, $ldapUsername, $dapPassword); 

// Do something to handle binding failure here, this is just what I did. 
if ($ldapBind === false) throw new ActiveDirectoryAuthenticationException(); 

$result = ldap_search($ldapConnection, $ldapSearchRoot, "(member:1.2.840.113556.1.4.1941:=" . $userDN . ")", array("sAMAccountName", "dn")); 

// Do something to handle query failure here, this is just what I did. 
if ($result === false) throw new ActiveDirectorySearchException(ldap_error($ldapConnection), ldap_errno($ldapConnection)); 

$groups = ldap_get_entries($ldapConnection, $result); 

$groupNames = array(); 

for ($i = 0; $i < $groups['count']; $i++) 
{ 
    $groupNames[] = $groups[$i]['samaccountname'][0]; 
} 

return $groupNames; 
+0

Wonderful, 대단히 감사합니다. @Ashigore! –

관련 문제