2014-02-11 180 views
5

首先appologies這應該是對服務器故障的,但它是做與PHP一樣,所以我認爲這爲它的最佳地點。PHP,Active Directory中,用戶帳戶控制

我正在創建一些方法來將我們的Intranet與Active Directory集成。其中一種方法會自動在我們的數據庫中搜索新用戶,並在AD中創建用戶帳戶(如果發現新用戶)。

同樣地,如果在數據庫中留下了用戶被標記,它會自動禁用在活動目錄的帳戶。

我一直在尋找從Active Directory中傳遞的屬性,特別是用戶帳戶控制領域。

microsoft website它指出這下它的屬性列表:

The following table lists possible flags that you can assign. You cannot set some 
of the values on a user or computer object because these values can be set or 
reset only by the directory service. The flags are cumulative. To disable a 
user's account, set the UserAccountControl attribute to 0x0202 (0x002 + 0x0200). In 
decimal, this is 514 (2 + 512). 

問題 我的問題是,如果我們用上面的例子中,標記記錄作爲用戶(512)和殘疾人(2),這最終使得公元514作爲

在PHP中返回的字段值,我怎麼能提取標誌已標記的記錄呢?例如,如果給出514,我如何使用PHP來確定它是一個普通的用戶帳戶,並禁用了(2和512)?

例如分成以下:

Flag | Splits into  | Flag Meaning 
--------+------------------+--------------------------------------------------------- 
514  | 512 + 2   | Normal User Account + Disabled 
522  | 512 + 2 + 8  | Normal User Account + Disabled + Home Directory Required 
8389120 | 8388608 + 512 | Password Expired + Normal User Account 

我希望你能理解我的問題,但隨時請求確認以上信息。

非常感謝

+0

什麼PHP工具是您使用訪問活動目錄?我想知道我有一個類似的內聯網要求。 –

回答

1

今天在相同的形勢下應運而生,它是更簡潔有:

$flag_to_find = 530; 
$flags = array(); 
for ($i=0; $i<=26; $i++){ 
    if ($flag_to_find & (1 << $i)){ 
    array_push($flags, 1 << $i); 
    } 
} 
print_r($flags); 
+0

我喜歡這種方法比我自己更多!感謝您的額外(更好)答案:) –

+0

這真棒,到處尋找這個,謝謝! –

1

好了,大量的試驗和錯誤之後,我摸索出如何做到這一點!

$bits = array(
    '27' => 1, '26' => 2, '25' => 4, '24' => 8, '23' => 16, '22' => 32, 
    '21' => 64, '20' => 128, '19' => 256, '18' => 512, '17' => 1024, 
    '16' => 2048, '15' => 4096, '14' => 8192, '13' => 16328, 
    '12' => 32768, '11' => 65536, '10' => 131072, '9' => 262144, 
    '8' => 524288, '7' => 1048576, '6' => 2097152, '5' => 4194304, 
    '4' => 8388608, '3' => 16777216, '2' => 33554432, '1' => 67108864 
); 

$flag_to_find = 530; // Indicates a normal user account, which is disabled, and locked out 

$binary = str_pad(decbin($flag_to_find), count($bits), 0, STR_PAD_LEFT); 

$flags = array(); 

for($i=0; $i<strlen($binary); $i++) 
{ 
    if($binary[$i]==1) 
{ 
     $flags[] = $bits[$i+1]; 
    } 
} 

print_r($flags); 

// Outputs: 512: Normal User Account 
      16: Locked Out Account 
      2: Disabled Accout 

它通過獲取UAC中所有可能標誌的十進制值來工作。有27個 - 我已將所有值分配給$bits陣列。

然後指定一個標誌來查找。

然後將該標誌的十進制值轉換爲二進制,並用0將其填充。

循環遍歷二進制字符串中的每個值。如果迭代值爲1,則從$bits陣列中獲取其關聯的標誌十進制值。

然後將所有可能的標誌存儲在$flags數組中。

希望這可以幫助任何人在將來遇到類似的問題!

2

添加到詹姆斯·斯隆的回答,這裏是標誌列表:

public function findFlags($flag) { 

    $flags = array(); 
    $flaglist = array(
       1 => 'SCRIPT', 
       2 => 'ACCOUNTDISABLE', 
       8 => 'HOMEDIR_REQUIRED', 
       16 => 'LOCKOUT', 
       32 => 'PASSWD_NOTREQD', 
       64 => 'PASSWD_CANT_CHANGE', 
      128 => 'ENCRYPTED_TEXT_PWD_ALLOWED', 
      256 => 'TEMP_DUPLICATE_ACCOUNT', 
      512 => 'NORMAL_ACCOUNT', 
      2048 => 'INTERDOMAIN_TRUST_ACCOUNT', 
      4096 => 'WORKSTATION_TRUST_ACCOUNT', 
      8192 => 'SERVER_TRUST_ACCOUNT', 
      65536 => 'DONT_EXPIRE_PASSWORD', 
      131072 => 'MNS_LOGON_ACCOUNT', 
      262144 => 'SMARTCARD_REQUIRED', 
      524288 => 'TRUSTED_FOR_DELEGATION', 
     1048576 => 'NOT_DELEGATED', 
     2097152 => 'USE_DES_KEY_ONLY', 
     4194304 => 'DONT_REQ_PREAUTH', 
     8388608 => 'PASSWORD_EXPIRED', 
     16777216 => 'TRUSTED_TO_AUTH_FOR_DELEGATION', 
     67108864 => 'PARTIAL_SECRETS_ACCOUNT' 
    ); 
    for ($i=0; $i<=26; $i++){ 
     if ($flag & (1 << $i)){ 
      array_push($flags, 1 << $i); 
     } 
    } 
    foreach($flags as $k=>&$v) { 
     $v = $v . ' ' . $flaglist[$v]; 
    } 
    return $flags; 
} 
+0

像魅力一樣工作!謝謝 – Meloman

相關問題