views:

49

answers:

2

So I want to implement Parental Controls per user in my app, but I need a way of getting all Users and add them to an NSTableView. These users should be the same displayed by the Login Window, excluding the Other... one and system users.

Any ideas on how to do this? Also, I want to be able to get the selection on that table view and of course change the settings displayed according to that.

+1  A: 

From the command line, you can run

dscl localhost -list /Local/Default/Users

There are a lot of users that start with the underscore character that can be ignored by you app. This command can of course be run from within cocoa and the output read, but it can also be accessed more directly.

You can also use an Apple framework, but it is probably easier to use a wrapper. I can't find a very recent one right now, but search for something like this:

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

Peter DeWeese
The criteria for if a user should be displayed or not Only users with an UID higher than 500, and whose shell is not set to /bin/false, are displayed in the login window. You can use this as the criteria (instead of "if the user name doesn't start with an underscore").
zneak
+2  A: 

Here's 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.

The CBIdentity objects are much more convenient to use than the CSIdentityRef objects, but they do require importing the Collaboration framework.

Dave DeLong