Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get short user name from full name

Tags:

macos

cocoa

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.

like image 548
Adrian Avatar asked Dec 02 '25 19:12

Adrian


1 Answers

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.

like image 174
Thibault Martin-Lagardette Avatar answered Dec 04 '25 10:12

Thibault Martin-Lagardette



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!