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 if ([emails count] == 0)
352 [self _fillContactMailRecords: currentUser];
356 - (void) _retainUser: (NSDictionary *) newUser
359 NSEnumerator *emails;
361 key = [newUser objectForKey: @"c_uid"];
363 [users setObject: newUser forKey: key];
364 emails = [[newUser objectForKey: @"emails"] objectEnumerator];
365 key = [emails nextObject];
368 [users setObject: newUser forKey: key];
369 key = [emails nextObject];
373 - (NSDictionary *) contactInfosForUserWithUIDorEmail: (NSString *) uid
375 NSMutableDictionary *currentUser, *contactInfos;
379 if ([uid length] > 0)
381 contactInfos = [NSMutableDictionary dictionary];
382 currentUser = [users objectForKey: uid];
383 if (!([currentUser objectForKey: @"emails"]
384 && [currentUser objectForKey: @"cn"]))
389 currentUser = [NSMutableDictionary dictionary];
393 [self _fillContactInfosForUser: currentUser
394 withUIDorEmail: uid];
397 if ([[currentUser objectForKey: @"c_uid"] length] > 0)
398 [self _retainUser: currentUser];
404 if (cleanupInterval && currentUser)
406 cleanupDate = [[NSDate date] addTimeInterval: cleanupInterval];
407 [currentUser setObject: cleanupDate forKey: @"cleanupDate"];
416 - (void) _fillContactsMailRecords: (NSEnumerator *) contacts
418 NSMutableDictionary *currentContact;
420 currentContact = [contacts nextObject];
421 while (currentContact)
423 [self _fillContactMailRecords: currentContact];
424 currentContact = [contacts nextObject];
428 - (NSArray *) _compactAndCompleteContacts: (NSEnumerator *) contacts
430 NSMutableDictionary *compactContacts, *returnContact;
431 NSDictionary *userEntry;
432 NSArray *newContacts;
433 NSMutableArray *emails;
434 NSString *uid, *email;
436 compactContacts = [NSMutableDictionary dictionary];
437 userEntry = [contacts nextObject];
440 uid = [userEntry objectForKey: @"c_uid"];
443 returnContact = [compactContacts objectForKey: uid];
446 returnContact = [NSMutableDictionary dictionary];
447 [returnContact setObject: uid forKey: @"c_uid"];
448 [compactContacts setObject: returnContact forKey: uid];
450 if (![[returnContact objectForKey: @"c_name"] length])
451 [returnContact setObject: [userEntry objectForKey: @"c_name"]
453 if (![[returnContact objectForKey: @"cn"] length])
454 [returnContact setObject: [userEntry objectForKey: @"c_cn"]
456 emails = [returnContact objectForKey: @"emails"];
459 emails = [NSMutableArray array];
460 [returnContact setObject: emails forKey: @"emails"];
462 email = [userEntry objectForKey: @"mail"];
463 if (email && ![emails containsObject: email])
464 [emails addObject: email];
465 email = [userEntry objectForKey: @"mozillaSecondEmail"];
466 if (email && ![emails containsObject: email])
467 [emails addObject: email];
468 email = [userEntry objectForKey: @"xmozillasecondemail"];
469 if (email && ![emails containsObject: email])
470 [emails addObject: email];
473 userEntry = [contacts nextObject];
475 newContacts = [compactContacts allValues];
476 [self _fillContactsMailRecords: [newContacts objectEnumerator]];
481 - (NSArray *) fetchContactsMatching: (NSString *) filter
483 NSMutableArray *contacts;
484 NSEnumerator *ldapSources;
485 LDAPSource *currentSource;
487 contacts = [NSMutableArray array];
488 ldapSources = [sources objectEnumerator];
489 currentSource = [ldapSources nextObject];
490 while (currentSource)
492 [contacts addObjectsFromArray:
493 [currentSource fetchContactsMatching: filter]];
494 currentSource = [ldapSources nextObject];
497 return [self _compactAndCompleteContacts: [contacts objectEnumerator]];
500 - (void) cleanupSources
502 NSEnumerator *userIDs;
504 NSDictionary *currentUser;
508 userIDs = [[users allKeys] objectEnumerator];
509 currentID = [userIDs nextObject];
512 currentUser = [users objectForKey: currentID];
513 if ([now earlierDate:
514 [currentUser objectForKey: @"cleanupDate"]] == now)
515 [users removeObjectForKey: currentID];
516 currentID = [userIDs nextObject];