2009-09-25 16:42:33 +02:00
|
|
|
/* SOGoUserManager.m - this file is part of SOGo
|
2007-05-09 21:07:35 +02:00
|
|
|
*
|
2009-02-24 02:54:59 +01:00
|
|
|
* Copyright (C) 2007-2009 Inverse inc.
|
2007-05-09 21:07:35 +02:00
|
|
|
*
|
|
|
|
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
#import <Foundation/NSArray.h>
|
|
|
|
#import <Foundation/NSDictionary.h>
|
2008-07-04 18:06:09 +02:00
|
|
|
#import <Foundation/NSEnumerator.h>
|
2009-12-02 21:53:52 +01:00
|
|
|
#import <Foundation/NSException.h>
|
2008-12-12 19:55:12 +01:00
|
|
|
#import <Foundation/NSLock.h>
|
2007-05-09 21:07:35 +02:00
|
|
|
#import <Foundation/NSString.h>
|
|
|
|
#import <Foundation/NSTimer.h>
|
|
|
|
#import <Foundation/NSValue.h>
|
2008-02-07 17:53:19 +01:00
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
2007-05-09 21:07:35 +02:00
|
|
|
|
2009-11-19 18:08:47 +01:00
|
|
|
#import "NSDictionary+BSJSONAdditions.h"
|
|
|
|
#import "NSArray+Utilities.h"
|
2009-11-29 05:19:32 +01:00
|
|
|
#import "SOGoDomainDefaults.h"
|
2009-11-19 18:08:47 +01:00
|
|
|
#import "SOGoSource.h"
|
2009-11-29 05:19:32 +01:00
|
|
|
#import "SOGoSystemDefaults.h"
|
2009-11-19 18:08:47 +01:00
|
|
|
#import "SOGoUserManager.h"
|
|
|
|
#import "SOGoCache.h"
|
|
|
|
#import "SOGoSource.h"
|
2009-09-25 16:42:33 +02:00
|
|
|
|
2009-12-02 21:53:52 +01:00
|
|
|
@implementation SOGoUserManagerRegistry
|
|
|
|
|
|
|
|
+ (id) sharedRegistry
|
|
|
|
{
|
|
|
|
static id sharedRegistry = nil;
|
|
|
|
|
|
|
|
if (!sharedRegistry)
|
|
|
|
sharedRegistry = [self new];
|
|
|
|
|
|
|
|
return sharedRegistry;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) sourceClassForType: (NSString *) type
|
|
|
|
{
|
|
|
|
NSString *sourceClass;
|
|
|
|
|
|
|
|
if (type)
|
|
|
|
{
|
|
|
|
if ([type isEqualToString: @"ldap"])
|
|
|
|
sourceClass = @"LDAPSource";
|
|
|
|
else if ([type isEqualToString: @"sql"])
|
|
|
|
sourceClass = @"SQLSource";
|
|
|
|
else
|
|
|
|
[NSException raise: @"SOGoUserManagerRegistryException"
|
|
|
|
format: @"No class known for type '%@'", type];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
sourceClass = @"LDAPSource";
|
|
|
|
|
|
|
|
return sourceClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
2007-05-09 21:07:35 +02:00
|
|
|
|
2009-09-25 16:42:33 +02:00
|
|
|
@implementation SOGoUserManager
|
2007-05-09 21:07:35 +02:00
|
|
|
|
|
|
|
+ (id) sharedUserManager
|
|
|
|
{
|
|
|
|
static id sharedUserManager = nil;
|
|
|
|
|
|
|
|
if (!sharedUserManager)
|
|
|
|
sharedUserManager = [self new];
|
|
|
|
|
|
|
|
return sharedUserManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _registerSource: (NSDictionary *) udSource
|
2009-11-29 05:19:32 +01:00
|
|
|
inDomain: (NSString *) domain
|
2007-05-09 21:07:35 +02:00
|
|
|
{
|
2009-09-25 16:42:33 +02:00
|
|
|
NSString *sourceID, *value, *type;
|
2007-05-09 21:07:35 +02:00
|
|
|
NSMutableDictionary *metadata;
|
2009-11-29 05:19:32 +01:00
|
|
|
NSObject <SOGoSource> *ldapSource;
|
2009-10-23 23:04:02 +02:00
|
|
|
BOOL isAddressBook;
|
2009-09-25 16:42:33 +02:00
|
|
|
Class c;
|
2009-11-29 05:19:32 +01:00
|
|
|
|
2007-05-09 21:07:35 +02:00
|
|
|
sourceID = [udSource objectForKey: @"id"];
|
2009-10-23 23:04:02 +02:00
|
|
|
if ([sourceID length] > 0)
|
|
|
|
{
|
2009-12-02 21:53:52 +01:00
|
|
|
type = [[udSource objectForKey: @"type"] lowercaseString];
|
|
|
|
c = NSClassFromString ([_registry sourceClassForType: type]);
|
|
|
|
ldapSource = [c sourceFromUDSource: udSource inDomain: domain];
|
2009-10-23 23:04:02 +02:00
|
|
|
if (sourceID)
|
|
|
|
[_sources setObject: ldapSource forKey: sourceID];
|
|
|
|
else
|
|
|
|
[self errorWithFormat: @"id field missing in an user source,"
|
|
|
|
@" check the SOGoUserSources defaults"];
|
|
|
|
metadata = [NSMutableDictionary dictionary];
|
2009-11-29 05:19:32 +01:00
|
|
|
if (domain)
|
|
|
|
[metadata setObject: domain forKey: @"domain"];
|
2009-10-23 23:04:02 +02:00
|
|
|
value = [udSource objectForKey: @"canAuthenticate"];
|
|
|
|
if (value)
|
|
|
|
[metadata setObject: value forKey: @"canAuthenticate"];
|
|
|
|
value = [udSource objectForKey: @"isAddressBook"];
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
[metadata setObject: value forKey: @"isAddressBook"];
|
|
|
|
isAddressBook = [value boolValue];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
isAddressBook = NO;
|
|
|
|
value = [udSource objectForKey: @"displayName"];
|
|
|
|
if (value)
|
|
|
|
[metadata setObject: value forKey: @"displayName"];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (isAddressBook)
|
|
|
|
[self errorWithFormat: @"addressbook source '%@' has"
|
|
|
|
@" no displayname", sourceID];
|
|
|
|
}
|
|
|
|
value = [udSource objectForKey: @"MailFieldNames"];
|
|
|
|
if (value)
|
|
|
|
[metadata setObject: value forKey: @"MailFieldNames"];
|
|
|
|
[_sourcesMetadata setObject: metadata forKey: sourceID];
|
|
|
|
}
|
2008-02-14 18:31:23 +01:00
|
|
|
else
|
2009-10-23 23:04:02 +02:00
|
|
|
[self errorWithFormat: @"attempted to register a contact/user source"
|
|
|
|
@" without id (skipped)"];
|
2007-05-09 21:07:35 +02:00
|
|
|
}
|
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
- (int) _registerSourcesInDomain: (NSString *) domain
|
2007-05-09 21:07:35 +02:00
|
|
|
{
|
2009-11-29 05:19:32 +01:00
|
|
|
NSArray *userSources;
|
2007-05-09 21:07:35 +02:00
|
|
|
unsigned int count, max;
|
2009-11-29 05:19:32 +01:00
|
|
|
SOGoDomainDefaults *dd;
|
2007-05-09 21:07:35 +02:00
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
if (domain)
|
|
|
|
dd = [SOGoDomainDefaults defaultsForDomain: domain];
|
|
|
|
else
|
|
|
|
dd = [SOGoSystemDefaults sharedSystemDefaults];
|
2009-09-25 16:42:33 +02:00
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
userSources = [dd userSources];
|
|
|
|
max = [userSources count];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
[self _registerSource: [userSources objectAtIndex: count]
|
|
|
|
inDomain: domain];
|
2009-09-25 16:42:33 +02:00
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
return max;
|
|
|
|
}
|
2007-05-09 21:07:35 +02:00
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
- (void) _prepareSources
|
|
|
|
{
|
|
|
|
NSArray *domains;
|
|
|
|
unsigned int count, max, total;
|
2009-09-25 16:42:33 +02:00
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
_sources = [[NSMutableDictionary alloc] init];
|
|
|
|
_sourcesMetadata = [[NSMutableDictionary alloc] init];
|
2009-09-25 16:42:33 +02:00
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
total = [self _registerSourcesInDomain: nil];
|
|
|
|
domains = [[SOGoSystemDefaults sharedSystemDefaults] domainIds];
|
|
|
|
max = [domains count];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
total += [self _registerSourcesInDomain: [domains objectAtIndex: count]];
|
|
|
|
|
|
|
|
if (!total)
|
|
|
|
[self errorWithFormat: @"No authentication sources defined - nobody will be able to login. Check your defaults."];
|
2007-05-09 21:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
2009-09-25 16:42:33 +02:00
|
|
|
_sources = nil;
|
|
|
|
_sourcesMetadata = nil;
|
2009-12-02 21:53:52 +01:00
|
|
|
_registry = [NSClassFromString ([self registryClass]) sharedRegistry];
|
2009-11-29 05:19:32 +01:00
|
|
|
[self _prepareSources];
|
2007-05-09 21:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
2009-09-25 16:42:33 +02:00
|
|
|
[_sources release];
|
|
|
|
[_sourcesMetadata release];
|
2007-05-09 21:07:35 +02:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2009-12-02 21:53:52 +01:00
|
|
|
- (NSString *) registryClass
|
|
|
|
{
|
|
|
|
return @"SOGoUserManagerRegistry";
|
|
|
|
}
|
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
- (NSArray *) sourceIDsInDomain: (NSString *) domain
|
2007-05-09 21:07:35 +02:00
|
|
|
{
|
2009-11-29 05:19:32 +01:00
|
|
|
NSMutableArray *sourceIDs;
|
|
|
|
NSArray *keys;
|
|
|
|
int count, max;
|
|
|
|
NSString *currentID, *sourceDomain;
|
|
|
|
NSObject <SOGoSource> *currentSource;
|
|
|
|
|
|
|
|
keys = [_sources allKeys];
|
|
|
|
max = [keys count];
|
|
|
|
sourceIDs = [NSMutableArray arrayWithCapacity: max];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
{
|
|
|
|
currentID = [keys objectAtIndex: count];
|
|
|
|
currentSource = [_sources objectForKey: currentID];
|
|
|
|
sourceDomain = [currentSource domain];
|
2009-11-29 22:48:00 +01:00
|
|
|
if (![sourceDomain length] || [sourceDomain isEqualToString: domain])
|
2009-11-29 05:19:32 +01:00
|
|
|
[sourceIDs addObject: currentID];
|
|
|
|
}
|
|
|
|
|
|
|
|
return sourceIDs;
|
2007-05-09 21:07:35 +02:00
|
|
|
}
|
|
|
|
|
2009-11-29 22:48:00 +01:00
|
|
|
- (NSObject <SOGoSource> *) sourceWithID: (NSString *) sourceID
|
|
|
|
{
|
|
|
|
return [_sources objectForKey: sourceID];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary *) metadataForSourceID: (NSString *) sourceID
|
|
|
|
{
|
|
|
|
return [_sourcesMetadata objectForKey: sourceID];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) authenticationSourceIDsInDomain: (NSString *) domain
|
2007-05-09 21:07:35 +02:00
|
|
|
{
|
|
|
|
NSMutableArray *sourceIDs;
|
|
|
|
NSEnumerator *allIDs;
|
2009-11-29 22:48:00 +01:00
|
|
|
NSString *currentID, *sourceDomain;
|
2009-11-29 05:19:32 +01:00
|
|
|
NSDictionary *metadata;
|
2007-05-09 21:07:35 +02:00
|
|
|
|
|
|
|
sourceIDs = [NSMutableArray array];
|
2009-09-25 16:42:33 +02:00
|
|
|
allIDs = [[_sources allKeys] objectEnumerator];
|
2009-11-29 05:19:32 +01:00
|
|
|
while ((currentID = [allIDs nextObject]))
|
2007-05-09 21:07:35 +02:00
|
|
|
{
|
2009-11-29 22:48:00 +01:00
|
|
|
sourceDomain = [[_sources objectForKey: currentID] domain];
|
|
|
|
if (![domain length] || [domain isEqualToString: sourceDomain])
|
2009-11-29 05:19:32 +01:00
|
|
|
{
|
2009-11-29 22:48:00 +01:00
|
|
|
metadata = [_sourcesMetadata objectForKey: currentID];
|
|
|
|
if ([[metadata objectForKey: @"canAuthenticate"] boolValue])
|
2009-11-29 05:19:32 +01:00
|
|
|
[sourceIDs addObject: currentID];
|
|
|
|
}
|
2007-05-09 21:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return sourceIDs;
|
|
|
|
}
|
|
|
|
|
2009-11-29 22:48:00 +01:00
|
|
|
- (NSArray *) addressBookSourceIDsInDomain: (NSString *) domain
|
2007-05-09 21:07:35 +02:00
|
|
|
{
|
2009-11-29 22:48:00 +01:00
|
|
|
NSMutableArray *sourceIDs;
|
|
|
|
NSEnumerator *allIDs;
|
|
|
|
NSString *currentID;
|
|
|
|
NSDictionary *metadata;
|
2007-05-09 21:07:35 +02:00
|
|
|
|
2009-11-29 22:48:00 +01:00
|
|
|
sourceIDs = [NSMutableArray array];
|
|
|
|
allIDs = [[self sourceIDsInDomain: domain] objectEnumerator];
|
|
|
|
while ((currentID = [allIDs nextObject]))
|
|
|
|
{
|
|
|
|
metadata = [_sourcesMetadata objectForKey: currentID];
|
|
|
|
if ([[metadata objectForKey: @"isAddressBook"] boolValue])
|
|
|
|
[sourceIDs addObject: currentID];
|
|
|
|
}
|
2007-05-09 21:07:35 +02:00
|
|
|
|
2009-11-29 22:48:00 +01:00
|
|
|
return sourceIDs;
|
2007-05-09 21:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) displayNameForSourceWithID: (NSString *) sourceID
|
|
|
|
{
|
|
|
|
NSDictionary *metadata;
|
|
|
|
|
2009-09-25 16:42:33 +02:00
|
|
|
metadata = [_sourcesMetadata objectForKey: sourceID];
|
2007-05-09 21:07:35 +02:00
|
|
|
|
|
|
|
return [metadata objectForKey: @"displayName"];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) getCNForUID: (NSString *) uid
|
|
|
|
{
|
|
|
|
NSDictionary *contactInfos;
|
|
|
|
|
2007-05-10 16:59:13 +02:00
|
|
|
// NSLog (@"getCNForUID: %@", uid);
|
2007-05-09 21:07:35 +02:00
|
|
|
contactInfos = [self contactInfosForUserWithUIDorEmail: uid];
|
|
|
|
|
|
|
|
return [contactInfos objectForKey: @"cn"];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) getEmailForUID: (NSString *) uid
|
|
|
|
{
|
|
|
|
NSDictionary *contactInfos;
|
|
|
|
|
2007-05-10 16:59:13 +02:00
|
|
|
// NSLog (@"getEmailForUID: %@", uid);
|
2007-05-09 21:07:35 +02:00
|
|
|
contactInfos = [self contactInfosForUserWithUIDorEmail: uid];
|
|
|
|
|
|
|
|
return [contactInfos objectForKey: @"c_email"];
|
|
|
|
}
|
|
|
|
|
2007-05-30 22:11:19 +02:00
|
|
|
- (NSString *) getFullEmailForUID: (NSString *) uid
|
|
|
|
{
|
|
|
|
NSDictionary *contactInfos;
|
|
|
|
|
|
|
|
contactInfos = [self contactInfosForUserWithUIDorEmail: uid];
|
|
|
|
|
|
|
|
return [NSString stringWithFormat: @"%@ <%@>",
|
|
|
|
[contactInfos objectForKey: @"cn"],
|
|
|
|
[contactInfos objectForKey: @"c_email"]];
|
|
|
|
}
|
|
|
|
|
2008-05-22 17:52:25 +02:00
|
|
|
- (NSString *) getImapLoginForUID: (NSString *) uid
|
|
|
|
{
|
2009-11-29 05:19:32 +01:00
|
|
|
NSDictionary *contactInfos;
|
|
|
|
NSString *domain;
|
|
|
|
SOGoDomainDefaults *dd;
|
|
|
|
|
|
|
|
contactInfos = [self contactInfosForUserWithUIDorEmail: uid];
|
|
|
|
domain = [contactInfos objectForKey: @"c_domain"];
|
|
|
|
if ([domain length])
|
|
|
|
dd = [SOGoDomainDefaults defaultsForDomain: domain];
|
|
|
|
else
|
|
|
|
dd = [SOGoSystemDefaults sharedSystemDefaults];
|
|
|
|
|
|
|
|
return ([dd forceIMAPLoginWithEmail] ? [self getEmailForUID: uid] : uid);
|
2008-05-22 17:52:25 +02:00
|
|
|
}
|
|
|
|
|
2007-05-09 21:07:35 +02:00
|
|
|
- (NSString *) getUIDForEmail: (NSString *) email
|
|
|
|
{
|
|
|
|
NSDictionary *contactInfos;
|
|
|
|
|
2007-05-10 16:59:13 +02:00
|
|
|
// NSLog (@"getUIDForEmail: %@", email);
|
2007-05-09 21:07:35 +02:00
|
|
|
contactInfos = [self contactInfosForUserWithUIDorEmail: email];
|
|
|
|
|
|
|
|
return [contactInfos objectForKey: @"c_uid"];
|
|
|
|
}
|
|
|
|
|
2009-09-25 16:42:33 +02:00
|
|
|
- (BOOL) _sourceCheckLogin: (NSString *) login
|
|
|
|
andPassword: (NSString *) password
|
2007-05-09 21:07:35 +02:00
|
|
|
{
|
2009-11-29 05:19:32 +01:00
|
|
|
NSObject <SOGoSource> *ldapSource;
|
2007-05-09 21:07:35 +02:00
|
|
|
NSEnumerator *authIDs;
|
|
|
|
NSString *currentID;
|
2009-09-25 16:42:33 +02:00
|
|
|
BOOL checkOK;
|
2009-11-29 22:48:00 +01:00
|
|
|
|
2007-05-09 21:07:35 +02:00
|
|
|
checkOK = NO;
|
|
|
|
|
2009-11-29 22:48:00 +01:00
|
|
|
authIDs = [[self authenticationSourceIDsInDomain: nil] objectEnumerator];
|
2008-05-22 17:52:25 +02:00
|
|
|
while (!checkOK && (currentID = [authIDs nextObject]))
|
2007-05-09 21:07:35 +02:00
|
|
|
{
|
2009-09-25 16:42:33 +02:00
|
|
|
ldapSource = [_sources objectForKey: currentID];
|
2007-05-09 21:07:35 +02:00
|
|
|
checkOK = [ldapSource checkLogin: login andPassword: password];
|
|
|
|
}
|
|
|
|
|
|
|
|
return checkOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) checkLogin: (NSString *) login
|
|
|
|
andPassword: (NSString *) password
|
|
|
|
{
|
|
|
|
NSMutableDictionary *currentUser;
|
2009-11-19 18:08:47 +01:00
|
|
|
NSString *dictPassword, *jsonUser;
|
2009-02-24 02:54:59 +01:00
|
|
|
BOOL checkOK;
|
2007-05-09 21:07:35 +02:00
|
|
|
|
2009-11-19 18:08:47 +01:00
|
|
|
jsonUser = [[SOGoCache sharedCache] userAttributesForLogin: login];
|
|
|
|
currentUser = [NSMutableDictionary dictionaryWithJSONString: jsonUser];
|
2007-05-09 21:07:35 +02:00
|
|
|
dictPassword = [currentUser objectForKey: @"password"];
|
|
|
|
if (currentUser && dictPassword)
|
|
|
|
checkOK = ([dictPassword isEqualToString: password]);
|
2009-09-25 16:42:33 +02:00
|
|
|
else if ([self _sourceCheckLogin: login andPassword: password])
|
2007-05-09 21:07:35 +02:00
|
|
|
{
|
|
|
|
checkOK = YES;
|
|
|
|
if (!currentUser)
|
|
|
|
{
|
|
|
|
currentUser = [NSMutableDictionary dictionary];
|
|
|
|
}
|
2009-08-10 18:23:21 +02:00
|
|
|
|
2009-11-19 18:08:47 +01:00
|
|
|
// It's important to cache the password here as we might have cached the
|
|
|
|
// user's entry in -contactInfosForUserWithUIDorEmail: and if we don't
|
|
|
|
// set the password and recache the entry, the password would never be
|
|
|
|
// cached for the user unless its entry expires from memcached's
|
|
|
|
// internal cache.
|
2007-05-09 21:07:35 +02:00
|
|
|
[currentUser setObject: password forKey: @"password"];
|
2009-11-19 18:08:47 +01:00
|
|
|
[[SOGoCache sharedCache]
|
|
|
|
setUserAttributes: [currentUser jsonStringValue]
|
|
|
|
forLogin: login];
|
2007-05-09 21:07:35 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
checkOK = NO;
|
|
|
|
|
|
|
|
return checkOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _fillContactMailRecords: (NSMutableDictionary *) contact
|
|
|
|
{
|
2009-11-29 05:19:32 +01:00
|
|
|
NSString *uid, *domain, *systemEmail;
|
2009-09-25 16:42:33 +02:00
|
|
|
NSMutableArray *emails;
|
2009-11-29 05:19:32 +01:00
|
|
|
SOGoDomainDefaults *dd;
|
2007-05-09 21:07:35 +02:00
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
domain = [contact objectForKey: @"c_domain"];
|
|
|
|
if ([domain length])
|
|
|
|
dd = [SOGoDomainDefaults defaultsForDomain: domain];
|
|
|
|
else
|
|
|
|
dd = [SOGoSystemDefaults sharedSystemDefaults];
|
2007-05-09 21:07:35 +02:00
|
|
|
emails = [contact objectForKey: @"emails"];
|
|
|
|
uid = [contact objectForKey: @"c_uid"];
|
2009-07-03 18:34:47 +02:00
|
|
|
if ([uid rangeOfString: @"@"].location == NSNotFound)
|
|
|
|
systemEmail
|
2009-11-29 05:19:32 +01:00
|
|
|
= [NSString stringWithFormat: @"%@@%@", uid, [dd mailDomain]];
|
2009-07-03 18:34:47 +02:00
|
|
|
else
|
|
|
|
systemEmail = uid;
|
2009-08-20 23:08:42 +02:00
|
|
|
[emails addObject: systemEmail];
|
2007-05-09 21:07:35 +02:00
|
|
|
[contact setObject: [emails objectAtIndex: 0] forKey: @"c_email"];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _fillContactInfosForUser: (NSMutableDictionary *) currentUser
|
|
|
|
withUIDorEmail: (NSString *) uid
|
|
|
|
{
|
|
|
|
NSMutableArray *emails;
|
|
|
|
NSDictionary *userEntry;
|
|
|
|
NSEnumerator *ldapSources;
|
|
|
|
LDAPSource *currentSource;
|
2009-11-29 22:48:00 +01:00
|
|
|
NSString *sourceID, *cn, *c_domain, *c_uid, *c_imaphostname;
|
2007-11-18 11:16:25 +01:00
|
|
|
NSArray *c_emails;
|
2007-11-26 21:41:59 +01:00
|
|
|
BOOL access;
|
|
|
|
|
2007-05-09 21:07:35 +02:00
|
|
|
emails = [NSMutableArray array];
|
|
|
|
cn = nil;
|
|
|
|
c_uid = nil;
|
2009-11-29 22:48:00 +01:00
|
|
|
c_domain = nil;
|
2009-05-12 21:56:39 +02:00
|
|
|
c_imaphostname = nil;
|
2007-05-09 21:07:35 +02:00
|
|
|
|
2007-11-26 21:41:59 +01:00
|
|
|
[currentUser setObject: [NSNumber numberWithBool: YES]
|
|
|
|
forKey: @"CalendarAccess"];
|
|
|
|
[currentUser setObject: [NSNumber numberWithBool: YES]
|
|
|
|
forKey: @"MailAccess"];
|
|
|
|
|
2009-11-29 22:48:00 +01:00
|
|
|
ldapSources = [[self authenticationSourceIDsInDomain: nil]
|
2009-11-29 05:19:32 +01:00
|
|
|
objectEnumerator];
|
2008-08-13 23:29:49 +02:00
|
|
|
while ((sourceID = [ldapSources nextObject]))
|
2007-05-09 21:07:35 +02:00
|
|
|
{
|
2009-09-25 16:42:33 +02:00
|
|
|
currentSource = [_sources objectForKey: sourceID];
|
2007-05-09 21:07:35 +02:00
|
|
|
userEntry = [currentSource lookupContactEntryWithUIDorEmail: uid];
|
|
|
|
if (userEntry)
|
|
|
|
{
|
|
|
|
if (!cn)
|
|
|
|
cn = [userEntry objectForKey: @"c_cn"];
|
|
|
|
if (!c_uid)
|
|
|
|
c_uid = [userEntry objectForKey: @"c_uid"];
|
2009-11-29 22:48:00 +01:00
|
|
|
if (!c_domain)
|
|
|
|
c_domain = [userEntry objectForKey: @"c_domain"];
|
2007-11-18 11:16:25 +01:00
|
|
|
c_emails = [userEntry objectForKey: @"c_emails"];
|
|
|
|
if ([c_emails count])
|
|
|
|
[emails addObjectsFromArray: c_emails];
|
2009-05-12 21:56:39 +02:00
|
|
|
if (!c_imaphostname)
|
|
|
|
c_imaphostname = [userEntry objectForKey: @"c_imaphostname"];
|
2007-11-26 21:41:59 +01:00
|
|
|
access = [[userEntry objectForKey: @"CalendarAccess"] boolValue];
|
|
|
|
if (!access)
|
|
|
|
[currentUser setObject: [NSNumber numberWithBool: NO]
|
|
|
|
forKey: @"CalendarAccess"];
|
|
|
|
access = [[userEntry objectForKey: @"MailAccess"] boolValue];
|
|
|
|
if (!access)
|
|
|
|
[currentUser setObject: [NSNumber numberWithBool: NO]
|
|
|
|
forKey: @"MailAccess"];
|
2007-05-09 21:07:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cn)
|
|
|
|
cn = @"";
|
|
|
|
if (!c_uid)
|
|
|
|
c_uid = @"";
|
2009-11-29 22:48:00 +01:00
|
|
|
if (!c_domain)
|
|
|
|
c_domain = @"";
|
2009-05-12 21:56:39 +02:00
|
|
|
|
|
|
|
if (c_imaphostname)
|
|
|
|
[currentUser setObject: c_imaphostname forKey: @"c_imaphostname"];
|
2007-05-09 21:07:35 +02:00
|
|
|
[currentUser setObject: emails forKey: @"emails"];
|
|
|
|
[currentUser setObject: cn forKey: @"cn"];
|
|
|
|
[currentUser setObject: c_uid forKey: @"c_uid"];
|
2009-11-29 22:48:00 +01:00
|
|
|
[currentUser setObject: c_domain forKey: @"c_domain"];
|
2007-10-10 20:44:26 +02:00
|
|
|
|
|
|
|
// If our LDAP queries gave us nothing, we add at least one default
|
|
|
|
// email address based on the default domain.
|
2007-11-05 20:18:45 +01:00
|
|
|
[self _fillContactMailRecords: currentUser];
|
2007-05-09 21:07:35 +02:00
|
|
|
}
|
|
|
|
|
2009-02-24 02:54:59 +01:00
|
|
|
//
|
|
|
|
// We cache here all identities, including those
|
|
|
|
// associated with email addresses.
|
|
|
|
//
|
2007-05-09 21:07:35 +02:00
|
|
|
- (void) _retainUser: (NSDictionary *) newUser
|
|
|
|
{
|
|
|
|
NSEnumerator *emails;
|
2009-02-24 02:54:59 +01:00
|
|
|
NSString *key;
|
|
|
|
|
2007-05-09 21:07:35 +02:00
|
|
|
key = [newUser objectForKey: @"c_uid"];
|
|
|
|
if (key)
|
2009-11-19 18:08:47 +01:00
|
|
|
[[SOGoCache sharedCache]
|
|
|
|
setUserAttributes: [newUser jsonStringValue]
|
|
|
|
forLogin: key];
|
2009-08-03 17:46:02 +02:00
|
|
|
|
2007-05-09 21:07:35 +02:00
|
|
|
emails = [[newUser objectForKey: @"emails"] objectEnumerator];
|
2008-09-02 04:48:18 +02:00
|
|
|
while ((key = [emails nextObject]))
|
2009-11-19 18:08:47 +01:00
|
|
|
[[SOGoCache sharedCache]
|
|
|
|
setUserAttributes: [newUser jsonStringValue]
|
|
|
|
forLogin: key];
|
2007-05-09 21:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary *) contactInfosForUserWithUIDorEmail: (NSString *) uid
|
|
|
|
{
|
|
|
|
NSMutableDictionary *currentUser, *contactInfos;
|
2009-11-19 18:08:47 +01:00
|
|
|
NSString *aUID, *jsonUser;
|
2007-05-09 21:07:35 +02:00
|
|
|
BOOL newUser;
|
|
|
|
|
2007-05-10 16:59:13 +02:00
|
|
|
if ([uid length] > 0)
|
2007-05-09 21:07:35 +02:00
|
|
|
{
|
2009-05-03 19:50:57 +02:00
|
|
|
// Remove the "@" prefix used to identified groups in the ACL tables.
|
|
|
|
aUID = [uid hasPrefix: @"@"] ? [uid substringFromIndex: 1] : uid;
|
2007-05-10 16:59:13 +02:00
|
|
|
contactInfos = [NSMutableDictionary dictionary];
|
2009-11-19 18:08:47 +01:00
|
|
|
jsonUser = [[SOGoCache sharedCache] userAttributesForLogin: aUID];
|
2009-11-26 20:31:07 +01:00
|
|
|
currentUser = [NSMutableDictionary dictionaryWithJSONString: jsonUser];
|
2007-05-10 16:59:13 +02:00
|
|
|
if (!([currentUser objectForKey: @"emails"]
|
|
|
|
&& [currentUser objectForKey: @"cn"]))
|
2007-05-09 21:07:35 +02:00
|
|
|
{
|
2009-11-29 05:19:32 +01:00
|
|
|
// We make sure that we either have no occurence of a cache entry or
|
|
|
|
// that we have an occurence with only a cached password. In the
|
|
|
|
// latter case, we update the entry with the remaining information
|
|
|
|
// and recache the value.
|
2009-08-10 18:23:21 +02:00
|
|
|
if (!currentUser || ([currentUser count] == 1 && [currentUser objectForKey: @"password"]))
|
2007-05-10 16:59:13 +02:00
|
|
|
{
|
|
|
|
newUser = YES;
|
2009-08-10 18:23:21 +02:00
|
|
|
|
|
|
|
if (!currentUser)
|
|
|
|
currentUser = [NSMutableDictionary dictionary];
|
2007-05-10 16:59:13 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
newUser = NO;
|
|
|
|
[self _fillContactInfosForUser: currentUser
|
2009-05-03 19:50:57 +02:00
|
|
|
withUIDorEmail: aUID];
|
2007-05-10 16:59:13 +02:00
|
|
|
if (newUser)
|
|
|
|
{
|
|
|
|
if ([[currentUser objectForKey: @"c_uid"] length] > 0)
|
|
|
|
[self _retainUser: currentUser];
|
|
|
|
else
|
|
|
|
currentUser = nil;
|
|
|
|
}
|
2007-05-09 21:07:35 +02:00
|
|
|
}
|
|
|
|
}
|
2007-05-10 16:59:13 +02:00
|
|
|
else
|
|
|
|
currentUser = nil;
|
2007-05-09 21:07:35 +02:00
|
|
|
|
|
|
|
return currentUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) _compactAndCompleteContacts: (NSEnumerator *) contacts
|
|
|
|
{
|
|
|
|
NSMutableDictionary *compactContacts, *returnContact;
|
|
|
|
NSDictionary *userEntry;
|
|
|
|
NSArray *newContacts;
|
|
|
|
NSMutableArray *emails;
|
2009-11-29 05:19:32 +01:00
|
|
|
NSString *uid, *email, *info;
|
2007-05-09 21:07:35 +02:00
|
|
|
|
|
|
|
compactContacts = [NSMutableDictionary dictionary];
|
2008-11-23 09:33:08 +01:00
|
|
|
while ((userEntry = [contacts nextObject]))
|
2007-05-09 21:07:35 +02:00
|
|
|
{
|
|
|
|
uid = [userEntry objectForKey: @"c_uid"];
|
2007-09-11 00:15:03 +02:00
|
|
|
if ([uid length])
|
2007-05-09 21:07:35 +02:00
|
|
|
{
|
2007-09-11 00:15:03 +02:00
|
|
|
returnContact = [compactContacts objectForKey: uid];
|
|
|
|
if (!returnContact)
|
|
|
|
{
|
|
|
|
returnContact = [NSMutableDictionary dictionary];
|
|
|
|
[returnContact setObject: uid forKey: @"c_uid"];
|
|
|
|
[compactContacts setObject: returnContact forKey: uid];
|
|
|
|
}
|
|
|
|
if (![[returnContact objectForKey: @"c_name"] length])
|
|
|
|
[returnContact setObject: [userEntry objectForKey: @"c_name"]
|
|
|
|
forKey: @"c_name"];
|
|
|
|
if (![[returnContact objectForKey: @"cn"] length])
|
|
|
|
[returnContact setObject: [userEntry objectForKey: @"c_cn"]
|
|
|
|
forKey: @"cn"];
|
|
|
|
emails = [returnContact objectForKey: @"emails"];
|
|
|
|
if (!emails)
|
|
|
|
{
|
|
|
|
emails = [NSMutableArray array];
|
|
|
|
[returnContact setObject: emails forKey: @"emails"];
|
|
|
|
}
|
|
|
|
email = [userEntry objectForKey: @"mail"];
|
|
|
|
if (email && ![emails containsObject: email])
|
|
|
|
[emails addObject: email];
|
2009-09-25 16:42:33 +02:00
|
|
|
email = [userEntry objectForKey: @"mozillasecondemail"];
|
2007-09-11 00:15:03 +02:00
|
|
|
if (email && ![emails containsObject: email])
|
|
|
|
[emails addObject: email];
|
|
|
|
email = [userEntry objectForKey: @"xmozillasecondemail"];
|
|
|
|
if (email && ![emails containsObject: email])
|
|
|
|
[emails addObject: email];
|
2009-11-29 05:19:32 +01:00
|
|
|
info = [userEntry objectForKey: @"c_info"];
|
|
|
|
if ([info length] > 0
|
|
|
|
&& ![[returnContact objectForKey: @"c_info"] length])
|
|
|
|
[returnContact setObject: info forKey: @"c_info"];
|
2008-12-12 19:55:12 +01:00
|
|
|
[self _fillContactMailRecords: returnContact];
|
2007-05-09 21:07:35 +02:00
|
|
|
}
|
|
|
|
}
|
2008-12-12 19:55:12 +01:00
|
|
|
|
2007-05-09 21:07:35 +02:00
|
|
|
newContacts = [compactContacts allValues];
|
|
|
|
|
|
|
|
return newContacts;
|
|
|
|
}
|
|
|
|
|
2008-08-07 18:07:43 +02:00
|
|
|
- (NSArray *) _fetchEntriesInSources: (NSArray *) sourcesList
|
|
|
|
matching: (NSString *) filter
|
2008-07-28 22:29:24 +02:00
|
|
|
{
|
|
|
|
NSMutableArray *contacts;
|
2009-09-25 16:42:33 +02:00
|
|
|
NSEnumerator *sources;
|
2008-07-28 22:29:24 +02:00
|
|
|
NSString *sourceID;
|
2009-09-25 16:42:33 +02:00
|
|
|
id currentSource;
|
2008-07-28 22:29:24 +02:00
|
|
|
|
|
|
|
contacts = [NSMutableArray array];
|
2009-09-25 16:42:33 +02:00
|
|
|
sources = [sourcesList objectEnumerator];
|
|
|
|
while ((sourceID = [sources nextObject]))
|
2008-07-28 22:29:24 +02:00
|
|
|
{
|
2009-09-25 16:42:33 +02:00
|
|
|
currentSource = [_sources objectForKey: sourceID];
|
2008-07-28 22:29:24 +02:00
|
|
|
[contacts addObjectsFromArray:
|
|
|
|
[currentSource fetchContactsMatching: filter]];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [self _compactAndCompleteContacts: [contacts objectEnumerator]];
|
|
|
|
}
|
|
|
|
|
2008-08-07 18:07:43 +02:00
|
|
|
- (NSArray *) fetchContactsMatching: (NSString *) filter
|
2009-11-29 05:19:32 +01:00
|
|
|
inDomain: (NSString *) domain
|
2008-08-07 18:07:43 +02:00
|
|
|
{
|
2009-11-29 05:19:32 +01:00
|
|
|
return [self
|
|
|
|
_fetchEntriesInSources: [self addressBookSourceIDsInDomain: domain]
|
|
|
|
matching: filter];
|
2008-08-07 18:07:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) fetchUsersMatching: (NSString *) filter
|
2009-11-29 22:48:00 +01:00
|
|
|
inDomain: (NSString *) domain
|
2008-08-07 18:07:43 +02:00
|
|
|
{
|
2009-11-29 22:48:00 +01:00
|
|
|
return [self _fetchEntriesInSources:
|
|
|
|
[self authenticationSourceIDsInDomain: domain]
|
2009-11-29 05:19:32 +01:00
|
|
|
matching: filter];
|
2008-08-07 18:07:43 +02:00
|
|
|
}
|
|
|
|
|
2009-05-03 19:50:57 +02:00
|
|
|
- (NSString *) getLoginForDN: (NSString *) theDN
|
|
|
|
{
|
2009-12-02 21:53:52 +01:00
|
|
|
NSEnumerator *sources;
|
2009-05-03 19:50:57 +02:00
|
|
|
NSString *login;
|
2009-12-02 21:53:52 +01:00
|
|
|
NSObject <SOGoDNSource> *currentSource;
|
2009-05-03 19:50:57 +02:00
|
|
|
|
|
|
|
login = nil;
|
2009-12-02 21:53:52 +01:00
|
|
|
|
|
|
|
sources = [[_sources allValues] objectEnumerator];
|
|
|
|
while (!login && (currentSource = [sources nextObject]))
|
|
|
|
if ([currentSource conformsToProtocol: @protocol (SOGoDNSource)]
|
|
|
|
&& [theDN hasSuffix: [currentSource baseDN]])
|
|
|
|
login = [currentSource lookupLoginByDN: theDN];
|
|
|
|
|
2009-05-03 19:50:57 +02:00
|
|
|
return login;
|
|
|
|
}
|
|
|
|
|
2007-05-09 21:07:35 +02:00
|
|
|
@end
|