Reading a user's registered domain on OS X
You can create CBUserIdentity
for a user from your username:
CBUserIdentity* identity = [CBUserIdentity identityWithName:NSUserName() authority:[CBIdentityAuthority defaultIdentityAuthority]];
Then you can get the authority user id :
CBIdentityAuthority* authority = identity.authority;
Then you can see if this is a local organ (the alternative is a managed organ):
if ([authority isEqual:[CBIdentityAuthority localIdentityAuthority])
{
// user is local
}
else
{
// user is managed
}
Authority has a property localizedName
, but that hardly includes the domain name, I don't think. I don't know how to get it.
Update:
This is the approach using the open directory API:
ODSession* session = [ODSession defaultSession];
ODNode* node = [ODNode nodeWithSession:session type:kODNodeTypeAuthentication error:NULL];
ODQuery* query = [ODQuery queryWithNode:node forRecordTypes:kODRecordTypeUsers attribute:kODAttributeTypeRecordName matchType:kODMatchEqualTo queryValues:NSUserName() returnAttributes:kODAttributeTypeStandardOnly maximumResults:0 error:NULL];
NSArray* results = [query resultsAllowingPartial:NO error:NULL];
ODRecord* record = results.firstObject;
At this point, you can query the entry for some of its attributes. It may be interesting kODAttributeTypeMetaNodeLocation
:
NSArray* attributes = [record valuesForAttribute:kODAttributeTypeMetaNodeLocation error:NULL];
NSString* attribute = attributes.firstObject;
For a local account, the meta node location should be "/ Local / Default". I checked with an LDAP account and gave "/LDAPv3/my.ldap.server.example.com". I don't have an Active Directory account to test.
Alternatively, you can try kODAttributeTypeMetaRecordName
. For a local account, this returns nil
. He gave the fully distinguished name for the LDAP account: "uid = ken, ou = People, dc = example, dc = com". Again, I don't know what it would do for the Active Directory account.
You can record the entry to see other available attributes. This will show the keys of the attribute as string values. You can look here to try and find the symbolic constant for the object of interest, or check / System / Library / Frameworks / OpenDirectory. framework / Frameworks / CFOpenDirectory.framework / Headers / CFOpenDirectoryConstants.h for some that are not documented.
Once you find what you're really interested in, you can simplify your query by only querying those, not kODAttributeTypeStandardOnly
. Also, you should consider the request asynchronously and not synchronously as in my sample code.
source to share