Get all users on OS X

So, I want to implement Parental Controls for each user in my application, but I need a way to get all users and add them to NSTableView. These users must be the same, displayed in the login window, with the exception of the other ... one and system users.

Any ideas on how to do this? In addition, I want to be able to get a selection in this table view and, of course, change the displayed settings in accordance with this.

+1
source share
3 answers

Here is how I do it:

#import <CoreServices/CoreServices.h>
#import <Collaboration/Collaboration.h>

CSIdentityAuthorityRef defaultAuthority = CSGetLocalIdentityAuthority();
CSIdentityClass identityClass = kCSIdentityClassUser;

CSIdentityQueryRef query = CSIdentityQueryCreate(NULL, identityClass, defaultAuthority);

CFErrorRef error = NULL;
CSIdentityQueryExecute(query, 0, &error);

CFArrayRef results = CSIdentityQueryCopyResults(query);

int numResults = CFArrayGetCount(results);

NSMutableArray * users = [NSMutableArray array];
for (int i = 0; i < numResults; ++i) {
    CSIdentityRef identity = (CSIdentityRef)CFArrayGetValueAtIndex(results, i);

    CBIdentity * identityObject = [CBIdentity identityWithCSIdentity:identity];
    [users addObject:identityObject];
}

CFRelease(results);
CFRelease(query);

//users contains a list of known Aqua-style users.

CBIdentity , CSIdentityRef, .

+9

dscl localhost -list /Local/Default/Users

, , . , cocoa, - , .

Apple, , , . , - :

http://www.martinkahr.com/2006/10/15/cocoa-directory-services-wrapper/index.html

+1

Here is the Swift version, note that in Xcode 7.2.1, "init (CSIdentity :)" is not available in Swift: CSIdentity is not available in Swift. "

Instead, we can use:

CBIdentity (uniqueIdentifier uuid: NSUUID, authority: CBIdentityAuthority)

func getSystemUsers()->[CBIdentity]{
        let defaultAuthority    = CSGetLocalIdentityAuthority().takeUnretainedValue()
        let identityClass       = kCSIdentityClassUser

        let query   = CSIdentityQueryCreate(nil, identityClass, defaultAuthority).takeRetainedValue()

        var error : Unmanaged<CFErrorRef>? = nil

        CSIdentityQueryExecute(query, 0, &error)

        let results = CSIdentityQueryCopyResults(query).takeRetainedValue()

        let resultsCount = CFArrayGetCount(results)

        var allUsersArray = [CBIdentity]()

        for idx in 0...resultsCount-1 {

            let identity    = unsafeBitCast(CFArrayGetValueAtIndex(results,idx),CSIdentityRef.self)
            let uuidString  = CFUUIDCreateString(nil, CSIdentityGetUUID(identity).takeUnretainedValue())

            if let uuidNS = NSUUID(UUIDString: uuidString as String), let identityObject =  CBIdentity(uniqueIdentifier: uuidNS, authority: CBIdentityAuthority.defaultIdentityAuthority()){
                allUsersArray.append(identityObject)
            }
        }

        return allUsersArray
    }
0
source

All Articles