2016-07-14 11 views
3

ADアクセスをチェックし、特定のグループ内でもメンバーシップをチェックする以下のスクリプトがあります。ユーザーが再帰的にメンバになっているすべてのActive Directoryグループを表示

ユーザーがアクセス権を持つすべての単一のグループを表示するにはどうすればよいですか?たとえば、user-userA、group-groupA、groupA in groupBのように、groupBを表示するグループAと同じ

私は何が欠けていますか?あなたのinGroup方法で

<?php 
     //ini_set('display_errors', 1); 
     session_start(); 
     //ini_set('display_startup_errors', 1); 
     //error_reporting(E_ALL); 
//  require_once('assets/config.php'); 

     $ldap_server = "ldap*************************"; 


if(isset($_SESSION['itssd_user'])) { 
    $submittedusername = $_SESSION['itssd_user']; 
} 
if(isset($_SESSION['itssd_pw'])) { 
    $submittedpassword = $_SESSION['itssd_pw']; 
} 


     //$ro_access_group='CN=****,OU=Service Desk,OU=Customer Services,OU=ITS,OU=Groups,DC=registry,DC=otago,DC=ac,DC=nz'; 
     $ro_access_group='DC=registry,DC=otago,DC=ac,DC=nz'; 
     // Connect to the LDAP server 
     $ldap = ldap_connect($ldap_server); 

function inGroup($ldapConnection, $userDN, $groupToFind) { 
    $filter = "(memberof:1.2.840.113556.1.4.1941:=".$groupToFind.")"; 
    $search = ldap_search($ldapConnection, $userDN, $filter, array("dn"), 1); 

    $items = ldap_get_entries($ldapConnection, $search); 
    echo "<pre>"; 
     echo var_dump($items)."<br>"; 
    echo "</pre>"; 
    if(!isset($items["count"])) { 
     return false; 
    } 
    return (bool)$items["count"]; 
} 
     if ($ldap) { 
       // Connect to the database for querying. 
//    $dbConn = connectDB(); 
       //$dn = "cn=" . $submittedusername . ",ou=Users,ou=Otago,dc=registry,dc=otago,dc=ac,dc=nz"; 

       $dn = "registry\\".$submittedusername; 

       $basedn = "dc=registry,dc=otago,dc=ac,dc=nz"; 
       if (($submittedpassword == "") OR ($submittedpassword == NULL)) { 
         $loginResult = 'INVALIDUSER'; 
       } else { 
         // Now attempt to bind 
         if (ldap_bind($ldap, $dn, $submittedpassword)) { 
           $search_user=ldap_search($ldap, $basedn, "(sAMAccountName=".$submittedusername.")"); 
           if($search_user){ 
           echo 'Authenticated'; 
           } 

           $user_details=ldap_get_entries($ldap, $search_user); 
           //$ro_name=$user_details[0]["displayname"][0]; 
           if(!$user_details){ 
             $loginResult = "INVALIDUSER"; 
             echo 'null user details<br>'.sizeof($user_details); 
           } 
           else{ 
             //echo "<pre>"; 
              //echo var_dump($user_details[0])."<br>"; 
             //echo "</pre>"; 

             if(isset($user_details[0]["memberof"][0])) { 
             $groupCount = $user_details[0]["memberof"]["count"] - 1; 
             for ($i = 0; $i <= $groupCount; $i++) { 
              echo $user_details[0]["memberof"][$i]; 
              echo "<br>"; 
             } 
            } 
             $_SESSION['itssd_email_messages_count'] = 0; 
             $_SESSION['itssd_notifications_count'] = 0; 
             $_SESSION['itssd_username'] = $submittedusername; 
             $_SESSION['itssd_date_view'] = date('Y-m-d', time()); 
             $_SESSION['itssd_prod'] = FALSE; 
             if (inGroup($ldap, $user_details[0]["dn"], $ro_access_group)) { 
               $loginResult = "Authorised"; 
             } else { 
               //echo "<br/><br/><br/>".$submittedusername." not in group ".$ro_access_group; 
               //echo "<br/>".$user_details[0]["memberof"]; 
               //echo var_dump($user_details[0]["memberof"]); 
               //echo "<br/><br/>"; 
               echo inGroup($ldap, $user_details[0]["dn"], $ro_access_group); 
               $loginResult = "INVALIDUSER"; 
             } 
           } 
         } else { 
           $loginResult = 'INVALIDUSER'; 
         } 
         $submittedpassword = NULL; 
       } 
       echo $loginResult; 

     } 
?> 

答えて

1

代わりに、フィルタのためにこれを使用します。

$filter = "(&(distinguishedName=$groupToFind)(member:1.2.840.113556.1.4.1941:=$userDN))";

をザ・は、そのDNによって再帰的にメンバーが含まれているかどうかを確認まずDNでグループを選択します。

編集

あなたは検索がすべてのグループを返すようにしたい場合、ユーザはこのフィルタの使用のメンバーである:よう

$filter = "(&(objectClass=group)(member:1.2.840.113556.1.4.1941:=$userDN))";

$filter = "(&(objectClass=group)(member:1.2.840.113556.1.4.1941:=$userDN))"; 
$search = ldap_search($ldapConnection, 'DC=registry,DC=otago,DC=ac,DC=nz', $filter, array("cn")); 

$allGroups = ldap_get_entries($ldapConnection, $search); 

$allGroups上記のように、ユーザーが直接または間接的に(例えば、異なるグループのメンバーであるグループなど)。しかし、DC=registry,DC=otago,DC=ac,DC=nzは本当にあなたのドメインの "ベース"レベルですか?それはldap_searchの第2引数です。

+0

ありがとうございました。変更された$ filter変数を使用すると、お互いに関連付けられたすべてのグループをそれぞれどのように表示できますか? –

+0

私は 'ldap_search'と一緒に使用したときにユーザーが属しているすべてのグループを返すフィルターを含めるように更新しました。 – ChadSikorra

+0

乾杯 - OK iは、特定のユーザ '場合のmembersOf性showeするためのコードを変更(ISSET($ user_details [0] [ "のmemberOfを"] [0])){ \t \t \t \t \t \t \t \t \t \t \t $ groupCount = $ user_details [0] ["memberof"] ["count"] - 1; (; $ I <= $ groupCount; $ i = 0 iは++ $)ため \t \t \t \t \t \t \t \t \t \t \t { \t \t \t \t \t \t \t \t \t \t \t \tが[0] $ user_detailsエコー"memberof"] [$ i]; \t \t \t \t \t \t \t \t \t \t \t \tエコー "
"。 \t \t \t \t \t \t \t \t \t \t \t} \t \t \t \t \t \t \t \t \t \t} 'どのように私はrecoursively、各いずれかを使用することができたこれらのいるグループを確認しますか? –

関連する問題