2013-02-15 55 views

回答

3

我一直在尋找如何在用戶的本地帳戶或任何網絡目錄帳戶(ActiveDir或執行opendir)檢測。因此,我已經使用開放式目錄框架做的是這樣的:

  1. 獲取缺省ODSession會議
  2. 獲得本地節點 - kODNodeTypeLocalNodes(我不想查詢發送到服務器的所有時間)
  3. 查詢與如發現查詢值設置爲當前用戶的主目錄
  4. 節點kODAttributeTypeNFSHomeDirectory則意味着用戶是本地的,如果不是(bacause我們查詢本地節點ONY) - 用戶網絡帳戶

所以是這樣的:

static BOOL isLocalUser = NO; 
static BOOL shouldKeepRunning = YES;  // global 

-(BOOL)isLocalUser 
{ 
    isLocalUser = NO; // set default to NO here 
    NSError* err; 
    ODSession *mySession = [ODSession defaultSession]; 
    ODNode *myNode = [ODNode nodeWithSession:mySession type:kODNodeTypeLocalNodes error:&err]; 
    ODQuery *myQuery = [ODQuery queryWithNode: myNode 
           forRecordTypes: kODRecordTypeUsers 
            attribute: kODAttributeTypeNFSHomeDirectory 
            matchType: kODMatchEqualTo 
            queryValues: NSHomeDirectory() 
           returnAttributes: kODAttributeTypeStandardOnly 
           maximumResults: 0 
             error: &err]; 

    [myQuery retain]; 
    [myQuery setDelegate: self]; 
    [myQuery scheduleInRunLoop: [NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 

    NSRunLoop *theRunLoop = [NSRunLoop currentRunLoop]; 
    while (shouldKeepRunning && [theRunLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]); 

    return isLocalUser; 
} 

- (void)query:(ODQuery *)inSearch foundResults:(NSArray *)inResults error:(NSError *)inError 
{ 

    if (!inResults && !inError) 
    { 
     [inSearch removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
     [inSearch release]; 
     shouldKeepRunning = NO; // end of search 
    } 

    ... 
    // check what you found here 
    // if found any nodes, user is local so 
    isLocalUser = YES; 
} 

另一個想法是使用身份服務:

  1. 獲取當前用戶的身份(CSIdentityQueryCreateForCurrentUser)
  2. 從(CSIdentityGetAuthority)獲得權威
  3. 看如果是地方當局(CSGetLocalIdentityAuthority)

希望這可以幫助。

相關問題