1 /* LDAPUserManager.m - this file is part of SOGo
3 * Copyright (C) 2007 Inverse groupe conseil
5 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
7 * This file is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This file is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #import <Foundation/NSArray.h>
24 #import <Foundation/NSDictionary.h>
25 #import <Foundation/NSString.h>
26 #import <Foundation/NSTimer.h>
27 #import <Foundation/NSUserDefaults.h>
28 #import <Foundation/NSValue.h>
30 #import "LDAPSource.h"
31 #import "LDAPUserManager.h"
33 static NSString *defaultMailDomain = nil;
35 @implementation LDAPUserManager
41 ud = [NSUserDefaults standardUserDefaults];
42 if (!defaultMailDomain)
44 defaultMailDomain = [ud stringForKey: @"SOGoDefaultMailDomain"];
45 [defaultMailDomain retain];
49 + (id) sharedUserManager
51 static id sharedUserManager = nil;
53 if (!sharedUserManager)
54 sharedUserManager = [self new];
56 return sharedUserManager;
59 - (void) _registerSource: (NSDictionary *) udSource
61 NSMutableDictionary *metadata;
62 LDAPSource *ldapSource;
63 NSString *sourceID, *value;
65 sourceID = [udSource objectForKey: @"id"];
66 ldapSource = [LDAPSource sourceFromUDSource: udSource];
67 [sources setObject: ldapSource forKey: sourceID];
68 metadata = [NSMutableDictionary dictionary];
69 value = [udSource objectForKey: @"canAuthenticate"];
71 [metadata setObject: value forKey: @"canAuthenticate"];
72 value = [udSource objectForKey: @"isAddressBook"];
74 [metadata setObject: value forKey: @"isAddressBook"];
75 value = [udSource objectForKey: @"displayName"];
77 [metadata setObject: value forKey: @"displayName"];
78 value = [udSource objectForKey: @"MailFieldNames"];
80 [metadata setObject: value forKey: @"MailFieldNames"];
81 [sourcesMetadata setObject: metadata forKey: sourceID];
84 - (void) _prepareLDAPSourcesWithDefaults: (NSUserDefaults *) ud
87 unsigned int count, max;
89 sources = [NSMutableDictionary new];
90 sourcesMetadata = [NSMutableDictionary new];
92 udSources = [ud arrayForKey: @"SOGoLDAPSources"];
93 max = [udSources count];
94 for (count = 0; count < max; count++)
95 [self _registerSource: [udSources objectAtIndex: count]];
102 if ((self = [super init]))
104 ud = [NSUserDefaults standardUserDefaults];
107 sourcesMetadata = nil;
108 users = [NSMutableDictionary new];
110 = [ud integerForKey: @"SOGOLDAPUserManagerCleanupInterval"];
112 cleanupTimer = [NSTimer timerWithTimeInterval: cleanupInterval
114 selector: @selector (cleanupUsers)
117 [self _prepareLDAPSourcesWithDefaults: ud];
130 - (NSArray *) sourceIDs
132 return [sources allKeys];
135 - (NSArray *) _sourcesOfType: (NSString *) sourceType
137 NSMutableArray *sourceIDs;
138 NSEnumerator *allIDs;
140 NSNumber *canAuthenticate;
142 sourceIDs = [NSMutableArray array];
143 allIDs = [[sources allKeys] objectEnumerator];
144 currentID = [allIDs nextObject];
147 canAuthenticate = [[sourcesMetadata objectForKey: currentID]
148 objectForKey: sourceType];
149 if ([canAuthenticate boolValue])
150 [sourceIDs addObject: currentID];
151 currentID = [allIDs nextObject];
157 - (NSDictionary *) metadataForSourceID: (NSString *) sourceID
159 return [sourcesMetadata objectForKey: sourceID];
162 - (NSArray *) authenticationSourceIDs
164 return [self _sourcesOfType: @"canAuthenticate"];
167 - (NSArray *) addressBookSourceIDs
169 return [self _sourcesOfType: @"isAddressBook"];
172 - (LDAPSource *) sourceWithID: (NSString *) sourceID
174 return [sources objectForKey: sourceID];
177 - (NSString *) displayNameForSourceWithID: (NSString *) sourceID
179 NSDictionary *metadata;
181 metadata = [sourcesMetadata objectForKey: sourceID];
183 return [metadata objectForKey: @"displayName"];
186 - (NSString *) getCNForUID: (NSString *) uid
188 NSDictionary *contactInfos;
190 // NSLog (@"getCNForUID: %@", uid);
191 contactInfos = [self contactInfosForUserWithUIDorEmail: uid];
193 return [contactInfos objectForKey: @"cn"];
196 - (NSString *) getEmailForUID: (NSString *) uid
198 NSDictionary *contactInfos;
200 // NSLog (@"getEmailForUID: %@", uid);
201 contactInfos = [self contactInfosForUserWithUIDorEmail: uid];
203 return [contactInfos objectForKey: @"c_email"];
206 - (NSString *) getFullEmailForUID: (NSString *) uid
208 NSDictionary *contactInfos;
210 contactInfos = [self contactInfosForUserWithUIDorEmail: uid];
212 return [NSString stringWithFormat: @"%@ <%@>",
213 [contactInfos objectForKey: @"cn"],
214 [contactInfos objectForKey: @"c_email"]];
217 - (NSString *) getUIDForEmail: (NSString *) email
219 NSDictionary *contactInfos;
221 // NSLog (@"getUIDForEmail: %@", email);
222 contactInfos = [self contactInfosForUserWithUIDorEmail: email];
224 return [contactInfos objectForKey: @"c_uid"];
227 - (BOOL) _ldapCheckLogin: (NSString *) login
228 andPassword: (NSString *) password
231 LDAPSource *ldapSource;
232 NSEnumerator *authIDs;
237 authIDs = [[self authenticationSourceIDs] objectEnumerator];
238 currentID = [authIDs nextObject];
239 while (currentID && !checkOK)
241 ldapSource = [sources objectForKey: currentID];
242 checkOK = [ldapSource checkLogin: login andPassword: password];
244 currentID = [authIDs nextObject];
250 - (BOOL) checkLogin: (NSString *) login
251 andPassword: (NSString *) password
255 NSMutableDictionary *currentUser;
256 NSString *dictPassword;
258 currentUser = [users objectForKey: login];
259 dictPassword = [currentUser objectForKey: @"password"];
260 if (currentUser && dictPassword)
261 checkOK = ([dictPassword isEqualToString: password]);
262 else if ([self _ldapCheckLogin: login andPassword: password])
267 currentUser = [NSMutableDictionary dictionary];
268 [users setObject: currentUser forKey: login];
270 [currentUser setObject: password forKey: @"password"];
277 cleanupDate = [[NSDate date] addTimeInterval: cleanupInterval];
278 [currentUser setObject: cleanupDate forKey: @"cleanupDate"];
284 - (void) _fillContactMailRecords: (NSMutableDictionary *) contact
286 NSMutableArray *emails;
287 NSString *uid, *systemEmail;
289 emails = [contact objectForKey: @"emails"];
290 uid = [contact objectForKey: @"c_uid"];
291 systemEmail = [NSString stringWithFormat: @"%@@%@", uid, defaultMailDomain];
292 if ([emails containsObject: systemEmail])
293 [emails removeObject: systemEmail];
294 [emails addObject: systemEmail];
295 [contact setObject: [emails objectAtIndex: 0] forKey: @"c_email"];
298 - (void) _fillContactInfosForUser: (NSMutableDictionary *) currentUser
299 withUIDorEmail: (NSString *) uid
301 NSMutableArray *emails;
302 NSDictionary *userEntry;
303 NSEnumerator *ldapSources;
304 LDAPSource *currentSource;
305 NSString *cn, *email, *c_uid;
308 emails = [NSMutableArray array];
312 ldapSources = [sources objectEnumerator];
313 currentSource = [ldapSources nextObject];
314 while (currentSource)
316 userEntry = [currentSource lookupContactEntryWithUIDorEmail: uid];
320 cn = [userEntry objectForKey: @"c_cn"];
322 c_uid = [userEntry objectForKey: @"c_uid"];
324 if ((attrs = [[sourcesMetadata objectForKey: [currentSource sourceID]] objectForKey: @"MailFieldNames"]))
328 for (i = 0; i < [attrs count]; i++)
330 email = [userEntry objectForKey: [attrs objectAtIndex: i]];
331 if (email && ![emails containsObject: email])
332 [emails addObject: email];
336 currentSource = [ldapSources nextObject];
344 [currentUser setObject: emails forKey: @"emails"];
345 [currentUser setObject: cn forKey: @"cn"];
346 [currentUser setObject: c_uid forKey: @"c_uid"];
348 // If our LDAP queries gave us nothing, we add at least one default
349 // email address based on the default domain.
350 [self _fillContactMailRecords: currentUser];
353 - (void) _retainUser: (NSDictionary *) newUser
356 NSEnumerator *emails;
358 key = [newUser objectForKey: @"c_uid"];
360 [users setObject: newUser forKey: key];
361 emails = [[newUser objectForKey: @"emails"] objectEnumerator];
362 key = [emails nextObject];
365 [users setObject: newUser forKey: key];
366 key = [emails nextObject];
370 - (NSDictionary *) contactInfosForUserWithUIDorEmail: (NSString *) uid
372 NSMutableDictionary *currentUser, *contactInfos;
376 if ([uid length] > 0)
378 contactInfos = [NSMutableDictionary dictionary];
379 currentUser = [users objectForKey: uid];
380 if (!([currentUser objectForKey: @"emails"]
381 && [currentUser objectForKey: @"cn"]))
386 currentUser = [NSMutableDictionary dictionary];
390 [self _fillContactInfosForUser: currentUser
391 withUIDorEmail: uid];
394 if ([[currentUser objectForKey: @"c_uid"] length] > 0)
395 [self _retainUser: currentUser];
401 if (cleanupInterval && currentUser)
403 cleanupDate = [[NSDate date] addTimeInterval: cleanupInterval];
404 [currentUser setObject: cleanupDate forKey: @"cleanupDate"];
413 - (void) _fillContactsMailRecords: (NSEnumerator *) contacts
415 NSMutableDictionary *currentContact;
417 currentContact = [contacts nextObject];
418 while (currentContact)
420 [self _fillContactMailRecords: currentContact];
421 currentContact = [contacts nextObject];
425 - (NSArray *) _compactAndCompleteContacts: (NSEnumerator *) contacts
427 NSMutableDictionary *compactContacts, *returnContact;
428 NSDictionary *userEntry;
429 NSArray *newContacts;
430 NSMutableArray *emails;
431 NSString *uid, *email;
433 compactContacts = [NSMutableDictionary dictionary];
434 userEntry = [contacts nextObject];
437 uid = [userEntry objectForKey: @"c_uid"];
440 returnContact = [compactContacts objectForKey: uid];
443 returnContact = [NSMutableDictionary dictionary];
444 [returnContact setObject: uid forKey: @"c_uid"];
445 [compactContacts setObject: returnContact forKey: uid];
447 if (![[returnContact objectForKey: @"c_name"] length])
448 [returnContact setObject: [userEntry objectForKey: @"c_name"]
450 if (![[returnContact objectForKey: @"cn"] length])
451 [returnContact setObject: [userEntry objectForKey: @"c_cn"]
453 emails = [returnContact objectForKey: @"emails"];
456 emails = [NSMutableArray array];
457 [returnContact setObject: emails forKey: @"emails"];
459 email = [userEntry objectForKey: @"mail"];
460 if (email && ![emails containsObject: email])
461 [emails addObject: email];
462 email = [userEntry objectForKey: @"mozillaSecondEmail"];
463 if (email && ![emails containsObject: email])
464 [emails addObject: email];
465 email = [userEntry objectForKey: @"xmozillasecondemail"];
466 if (email && ![emails containsObject: email])
467 [emails addObject: email];
470 userEntry = [contacts nextObject];
472 newContacts = [compactContacts allValues];
473 [self _fillContactsMailRecords: [newContacts objectEnumerator]];
478 - (NSArray *) fetchContactsMatching: (NSString *) filter
480 NSMutableArray *contacts;
481 NSEnumerator *ldapSources;
482 LDAPSource *currentSource;
484 contacts = [NSMutableArray array];
485 ldapSources = [sources objectEnumerator];
486 currentSource = [ldapSources nextObject];
487 while (currentSource)
489 [contacts addObjectsFromArray:
490 [currentSource fetchContactsMatching: filter]];
491 currentSource = [ldapSources nextObject];
494 return [self _compactAndCompleteContacts: [contacts objectEnumerator]];
497 - (void) cleanupSources
499 NSEnumerator *userIDs;
501 NSDictionary *currentUser;
505 userIDs = [[users allKeys] objectEnumerator];
506 currentID = [userIDs nextObject];
509 currentUser = [users objectForKey: currentID];
510 if ([now earlierDate:
511 [currentUser objectForKey: @"cleanupDate"]] == now)
512 [users removeObjectForKey: currentID];
513 currentID = [userIDs nextObject];