tags:

views:

172

answers:

1

Anyone know how to get a user's short user name, eg. "johnsmith", given their full name, eg. "John Smith"?

Note I'm interested in any user, not the current user, so functions like NSUserName are irrelevant.

Why? I am authenticating a username and password using Authorization Services. This allows people to enter either their short name or their full name, which is nice, but I then need to know who they've actually logged in as (ie. short user name and/or user id).

Nasty hacks like [NSHomeDirectoryForUser(username) lastPathComponent] don't work consistently.

+5  A: 

Hi Adrian,

You need to use the Collaboration Framework :). Link this framework to your project, and then you just need to do the following:

CBIdentity* identity = [CBIdentity identityWithName:@"John Smith" authority:[CBIdentityAuthority localIdentityAuthority]];
NSLog(@"Posix name: %@", [identity posixName]);

And voilà!

EDIT: If you need to find only users that are bound on the network, you need to use +managedIdentityAuthority instead of +localIdentityAuthority. And if you need to find both local users AND network users, use +defaultIdentityAuthority.

naixn
+1 I've never heard of that framework before!
Dave DeLong
Perfect, thanks. I'm sure Apple's documentation could make it easier to find these things.After I asked the question I found that getpwnam() would do it, but glad to have this sanctioned method. I bet getpwnam doesn't do network users.
Adrian
getpwnam() does network users through DirectoryServices.however, the good thing about CBIdentity is that you can specify both, or either (aka, you might want to check only local users, or only network users).Plus, CBIdentity allows you to search for an identity via Full name, short name, email address, or alias, which is quite convenient :).
naixn