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>
|
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/NSUserDefaults.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"
|
|
|
|
#import "SOGoSource.h"
|
|
|
|
#import "SOGoUserManager.h"
|
|
|
|
#import "SOGoCache.h"
|
|
|
|
#import "SOGoSource.h"
|
2009-09-25 16:42:33 +02:00
|
|
|
|
2009-11-19 18:08:47 +01:00
|
|
|
#import "LDAPSource.h"
|
|
|
|
#import "SQLSource.h"
|
2007-05-09 21:07:35 +02:00
|
|
|
|
|
|
|
static NSString *defaultMailDomain = nil;
|
2008-10-04 01:12:11 +02:00
|
|
|
static NSString *LDAPContactInfoAttribute = nil;
|
2008-02-07 17:53:19 +01:00
|
|
|
static BOOL defaultMailDomainIsConfigured = NO;
|
2008-05-22 20:01:52 +02:00
|
|
|
static BOOL forceImapLoginWithEmail = NO;
|
2007-05-09 21:07:35 +02:00
|
|
|
|
2008-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
2008-12-17 21:19:58 +01:00
|
|
|
static NSLock *lock = nil;
|
2008-12-12 19:55:12 +01:00
|
|
|
#endif
|
|
|
|
|
2009-09-25 16:42:33 +02:00
|
|
|
@implementation SOGoUserManager
|
2007-05-09 21:07:35 +02:00
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
NSUserDefaults *ud;
|
|
|
|
|
|
|
|
ud = [NSUserDefaults standardUserDefaults];
|
|
|
|
if (!defaultMailDomain)
|
|
|
|
{
|
|
|
|
defaultMailDomain = [ud stringForKey: @"SOGoDefaultMailDomain"];
|
|
|
|
[defaultMailDomain retain];
|
2008-02-07 17:53:19 +01:00
|
|
|
defaultMailDomainIsConfigured = YES;
|
2008-10-04 01:12:11 +02:00
|
|
|
|
|
|
|
if (!defaultMailDomain)
|
|
|
|
{
|
|
|
|
[self warnWithFormat:
|
|
|
|
@"no domain specified for SOGoDefaultMailDomain,"
|
|
|
|
@" value set to 'localhost'"];
|
|
|
|
defaultMailDomain = @"localhost";
|
|
|
|
}
|
|
|
|
|
2009-10-27 19:46:17 +01:00
|
|
|
LDAPContactInfoAttribute = [[ud stringForKey: @"SOGoLDAPContactInfoAttribute"] lowercaseString];
|
2008-11-23 09:33:08 +01:00
|
|
|
[LDAPContactInfoAttribute retain];
|
2008-02-07 17:53:19 +01:00
|
|
|
}
|
2008-05-22 17:52:25 +02:00
|
|
|
if (!forceImapLoginWithEmail)
|
|
|
|
forceImapLoginWithEmail = [ud boolForKey: @"SOGoForceIMAPLoginWithEmail"];
|
2008-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
lock = [NSLock new];
|
|
|
|
#endif
|
2008-02-07 17:53:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (BOOL) defaultMailDomainIsConfigured
|
|
|
|
{
|
|
|
|
return defaultMailDomainIsConfigured;
|
2007-05-09 21:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) sharedUserManager
|
|
|
|
{
|
|
|
|
static id sharedUserManager = nil;
|
|
|
|
|
2008-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock lock];
|
|
|
|
#endif
|
2007-05-09 21:07:35 +02:00
|
|
|
if (!sharedUserManager)
|
|
|
|
sharedUserManager = [self new];
|
2008-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock unlock];
|
|
|
|
#endif
|
2007-05-09 21:07:35 +02:00
|
|
|
|
|
|
|
return sharedUserManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _registerSource: (NSDictionary *) udSource
|
|
|
|
{
|
2009-09-25 16:42:33 +02:00
|
|
|
NSString *sourceID, *value, *type;
|
2007-05-09 21:07:35 +02:00
|
|
|
NSMutableDictionary *metadata;
|
2009-09-25 16:42:33 +02:00
|
|
|
id<SOGoSource> ldapSource;
|
2009-10-23 23:04:02 +02:00
|
|
|
BOOL isAddressBook;
|
2009-09-25 16:42:33 +02:00
|
|
|
Class c;
|
2007-05-09 21:07:35 +02:00
|
|
|
|
|
|
|
sourceID = [udSource objectForKey: @"id"];
|
2009-10-23 23:04:02 +02:00
|
|
|
if ([sourceID length] > 0)
|
|
|
|
{
|
|
|
|
type = [udSource objectForKey: @"type"];
|
|
|
|
|
|
|
|
if (!type || [type caseInsensitiveCompare: @"ldap"] == NSOrderedSame)
|
|
|
|
c = [LDAPSource class];
|
|
|
|
else
|
|
|
|
c = [SQLSource class];
|
|
|
|
|
|
|
|
ldapSource = [c sourceFromUDSource: udSource];
|
|
|
|
if (sourceID)
|
|
|
|
[_sources setObject: ldapSource forKey: sourceID];
|
|
|
|
else
|
|
|
|
[self errorWithFormat: @"id field missing in an user source,"
|
|
|
|
@" check the SOGoUserSources defaults"];
|
|
|
|
metadata = [NSMutableDictionary dictionary];
|
|
|
|
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-09-25 16:42:33 +02:00
|
|
|
- (void) _prepareSourcesWithDefaults: (NSUserDefaults *) ud
|
2007-05-09 21:07:35 +02:00
|
|
|
{
|
2009-09-25 16:42:33 +02:00
|
|
|
id o, sources;
|
2007-05-09 21:07:35 +02:00
|
|
|
unsigned int count, max;
|
|
|
|
|
2009-09-25 16:42:33 +02:00
|
|
|
_sources = [[NSMutableDictionary alloc] init];
|
|
|
|
_sourcesMetadata = [[NSMutableDictionary alloc] init];
|
|
|
|
|
|
|
|
sources = [NSMutableArray array];
|
|
|
|
o = [ud arrayForKey: @"SOGoLDAPSources"];
|
|
|
|
|
|
|
|
if (o)
|
|
|
|
{
|
|
|
|
[self errorWithFormat: @"Using depecrated SOGoLDAPSources default. You should now use SOGoUserSources."];
|
2007-05-09 21:07:35 +02:00
|
|
|
|
2009-09-25 16:42:33 +02:00
|
|
|
if ([o isKindOfClass: [NSArray class]])
|
|
|
|
[sources addObjectsFromArray: o];
|
|
|
|
else
|
|
|
|
[self errorWithFormat: @"SOGoLDAPSources is NOT an array. Check your defaults. You should now use SOGoUserSources nonetheless."];
|
|
|
|
}
|
2008-10-22 22:50:27 +02:00
|
|
|
|
2009-09-25 16:42:33 +02:00
|
|
|
o = [ud arrayForKey: @"SOGoUserSources"];
|
|
|
|
|
|
|
|
if (o)
|
|
|
|
{
|
|
|
|
if ([o isKindOfClass: [NSArray class]])
|
|
|
|
[sources addObjectsFromArray: o];
|
|
|
|
else
|
|
|
|
[self errorWithFormat: @"SOGoUserSources is NOT an array. Check your defaults."];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([sources count])
|
2008-12-17 21:19:58 +01:00
|
|
|
{
|
2009-09-25 16:42:33 +02:00
|
|
|
max = [sources count];
|
2008-12-17 21:19:58 +01:00
|
|
|
for (count = 0; count < max; count++)
|
2009-09-25 16:42:33 +02:00
|
|
|
[self _registerSource: [sources objectAtIndex: count]];
|
|
|
|
}
|
2009-10-22 19:15:08 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
[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
|
|
|
|
{
|
|
|
|
NSUserDefaults *ud;
|
|
|
|
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
|
|
|
ud = [NSUserDefaults standardUserDefaults];
|
|
|
|
|
2009-09-25 16:42:33 +02:00
|
|
|
_sources = nil;
|
|
|
|
_sourcesMetadata = nil;
|
|
|
|
[self _prepareSourcesWithDefaults: ud];
|
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];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) sourceIDs
|
|
|
|
{
|
2009-09-25 16:42:33 +02:00
|
|
|
return [_sources allKeys];
|
2007-05-09 21:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) _sourcesOfType: (NSString *) sourceType
|
|
|
|
{
|
|
|
|
NSMutableArray *sourceIDs;
|
|
|
|
NSEnumerator *allIDs;
|
|
|
|
NSString *currentID;
|
|
|
|
NSNumber *canAuthenticate;
|
|
|
|
|
|
|
|
sourceIDs = [NSMutableArray array];
|
2009-09-25 16:42:33 +02:00
|
|
|
allIDs = [[_sources allKeys] objectEnumerator];
|
2008-07-28 22:29:24 +02:00
|
|
|
while ((currentID = [allIDs nextObject]))
|
2007-05-09 21:07:35 +02:00
|
|
|
{
|
2009-09-25 16:42:33 +02:00
|
|
|
canAuthenticate = [[_sourcesMetadata objectForKey: currentID]
|
2007-05-09 21:07:35 +02:00
|
|
|
objectForKey: sourceType];
|
|
|
|
if ([canAuthenticate boolValue])
|
|
|
|
[sourceIDs addObject: currentID];
|
|
|
|
}
|
|
|
|
|
|
|
|
return sourceIDs;
|
|
|
|
}
|
|
|
|
|
2007-10-10 20:44:26 +02:00
|
|
|
- (NSDictionary *) metadataForSourceID: (NSString *) sourceID
|
|
|
|
{
|
2009-09-25 16:42:33 +02:00
|
|
|
return [_sourcesMetadata objectForKey: sourceID];
|
2007-10-10 20:44:26 +02:00
|
|
|
}
|
|
|
|
|
2007-05-09 21:07:35 +02:00
|
|
|
- (NSArray *) authenticationSourceIDs
|
|
|
|
{
|
|
|
|
return [self _sourcesOfType: @"canAuthenticate"];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) addressBookSourceIDs
|
|
|
|
{
|
|
|
|
return [self _sourcesOfType: @"isAddressBook"];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (LDAPSource *) sourceWithID: (NSString *) sourceID
|
|
|
|
{
|
2009-09-25 16:42:33 +02:00
|
|
|
return [_sources objectForKey: sourceID];
|
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
|
|
|
|
{
|
|
|
|
return ((forceImapLoginWithEmail) ? [self getEmailForUID: uid] : uid);
|
|
|
|
}
|
|
|
|
|
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-09-25 16:42:33 +02:00
|
|
|
id<SOGoSource> ldapSource;
|
2007-05-09 21:07:35 +02:00
|
|
|
NSEnumerator *authIDs;
|
|
|
|
NSString *currentID;
|
2009-09-25 16:42:33 +02:00
|
|
|
BOOL checkOK;
|
|
|
|
|
2007-05-09 21:07:35 +02:00
|
|
|
checkOK = NO;
|
|
|
|
|
|
|
|
authIDs = [[self authenticationSourceIDs] 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
|
|
|
|
2008-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock lock];
|
|
|
|
#endif
|
|
|
|
|
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;
|
|
|
|
|
2008-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock unlock];
|
|
|
|
#endif
|
|
|
|
|
2007-05-09 21:07:35 +02:00
|
|
|
return checkOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _fillContactMailRecords: (NSMutableDictionary *) contact
|
|
|
|
{
|
2007-05-10 18:05:18 +02:00
|
|
|
NSString *uid, *systemEmail;
|
2009-09-25 16:42:33 +02:00
|
|
|
NSMutableArray *emails;
|
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
|
|
|
|
= [NSString stringWithFormat: @"%@@%@", uid, defaultMailDomain];
|
|
|
|
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-05-12 21:56:39 +02:00
|
|
|
NSString *sourceID, *cn, *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-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"];
|
|
|
|
|
2008-08-13 23:29:49 +02:00
|
|
|
ldapSources = [[self authenticationSourceIDs] objectEnumerator];
|
|
|
|
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"];
|
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-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"];
|
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];
|
2008-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock lock];
|
|
|
|
#endif
|
2007-05-10 16:59:13 +02:00
|
|
|
if (!([currentUser objectForKey: @"emails"]
|
|
|
|
&& [currentUser objectForKey: @"cn"]))
|
2007-05-09 21:07:35 +02:00
|
|
|
{
|
2009-08-10 18:23:21 +02: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.
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock unlock];
|
|
|
|
#endif
|
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;
|
2008-11-23 09:33:08 +01:00
|
|
|
NSString *uid, *email, *infoAttribute;
|
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];
|
2008-11-23 09:33:08 +01:00
|
|
|
if ([LDAPContactInfoAttribute length]
|
|
|
|
&& ![[returnContact
|
|
|
|
objectForKey: LDAPContactInfoAttribute] length])
|
|
|
|
{
|
|
|
|
infoAttribute
|
|
|
|
= [userEntry objectForKey: LDAPContactInfoAttribute];
|
|
|
|
if ([infoAttribute length])
|
|
|
|
[returnContact setObject: infoAttribute
|
2008-10-04 01:12:11 +02:00
|
|
|
forKey: LDAPContactInfoAttribute];
|
2008-11-23 09:33:08 +01:00
|
|
|
}
|
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
|
|
|
|
{
|
|
|
|
return [self _fetchEntriesInSources: [self addressBookSourceIDs]
|
|
|
|
matching: filter];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) fetchUsersMatching: (NSString *) filter
|
|
|
|
{
|
|
|
|
return [self _fetchEntriesInSources: [self authenticationSourceIDs]
|
|
|
|
matching: filter];
|
|
|
|
}
|
|
|
|
|
2009-05-03 19:50:57 +02:00
|
|
|
- (NSString *) getLoginForDN: (NSString *) theDN
|
|
|
|
{
|
|
|
|
NSEnumerator *ldapSources;
|
|
|
|
NSString *login;
|
|
|
|
LDAPSource *currentSource;
|
|
|
|
|
|
|
|
login = nil;
|
2009-09-25 16:42:33 +02:00
|
|
|
ldapSources = [[_sources allValues] objectEnumerator];
|
2009-05-03 19:50:57 +02:00
|
|
|
while ((currentSource = [ldapSources nextObject]))
|
|
|
|
{
|
|
|
|
if ([theDN hasSuffix: [currentSource baseDN]])
|
|
|
|
{
|
|
|
|
login = [currentSource lookupLoginByDN: theDN];
|
|
|
|
if (login)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return login;
|
|
|
|
}
|
|
|
|
|
2007-05-09 21:07:35 +02:00
|
|
|
@end
|