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 [sourcesMetadata setObject: metadata forKey: sourceID];
81 - (void) _prepareLDAPSourcesWithDefaults: (NSUserDefaults *) ud
84 unsigned int count, max;
86 sources = [NSMutableDictionary new];
87 sourcesMetadata = [NSMutableDictionary new];
89 udSources = [ud arrayForKey: @"SOGoLDAPSources"];
90 max = [udSources count];
91 for (count = 0; count < max; count++)
92 [self _registerSource: [udSources objectAtIndex: count]];
99 if ((self = [super init]))
101 ud = [NSUserDefaults standardUserDefaults];
104 sourcesMetadata = nil;
105 users = [NSMutableDictionary new];
107 = [ud integerForKey: @"SOGOLDAPUserManagerCleanupInterval"];
109 cleanupTimer = [NSTimer timerWithTimeInterval: cleanupInterval
111 selector: @selector (cleanupUsers)
114 [self _prepareLDAPSourcesWithDefaults: ud];
127 - (NSArray *) sourceIDs
129 return [sources allKeys];
132 - (NSArray *) _sourcesOfType: (NSString *) sourceType
134 NSMutableArray *sourceIDs;
135 NSEnumerator *allIDs;
137 NSNumber *canAuthenticate;
139 sourceIDs = [NSMutableArray array];
140 allIDs = [[sources allKeys] objectEnumerator];
141 currentID = [allIDs nextObject];
144 canAuthenticate = [[sourcesMetadata objectForKey: currentID]
145 objectForKey: sourceType];
146 if ([canAuthenticate boolValue])
147 [sourceIDs addObject: currentID];
148 currentID = [allIDs nextObject];
154 - (NSArray *) authenticationSourceIDs
156 return [self _sourcesOfType: @"canAuthenticate"];
159 - (NSArray *) addressBookSourceIDs
161 return [self _sourcesOfType: @"isAddressBook"];
164 - (LDAPSource *) sourceWithID: (NSString *) sourceID
166 return [sources objectForKey: sourceID];
169 - (NSString *) displayNameForSourceWithID: (NSString *) sourceID
171 NSDictionary *metadata;
173 metadata = [sourcesMetadata objectForKey: sourceID];
175 return [metadata objectForKey: @"displayName"];
178 - (NSString *) getCNForUID: (NSString *) uid
180 NSDictionary *contactInfos;
182 // NSLog (@"getCNForUID: %@", uid);
183 contactInfos = [self contactInfosForUserWithUIDorEmail: uid];
185 return [contactInfos objectForKey: @"cn"];
188 - (NSString *) getEmailForUID: (NSString *) uid
190 NSDictionary *contactInfos;
192 // NSLog (@"getEmailForUID: %@", uid);
193 contactInfos = [self contactInfosForUserWithUIDorEmail: uid];
195 return [contactInfos objectForKey: @"c_email"];
198 - (NSString *) getFullEmailForUID: (NSString *) uid
200 NSDictionary *contactInfos;
202 contactInfos = [self contactInfosForUserWithUIDorEmail: uid];
204 return [NSString stringWithFormat: @"%@ <%@>",
205 [contactInfos objectForKey: @"cn"],
206 [contactInfos objectForKey: @"c_email"]];
209 - (NSString *) getUIDForEmail: (NSString *) email
211 NSDictionary *contactInfos;
213 // NSLog (@"getUIDForEmail: %@", email);
214 contactInfos = [self contactInfosForUserWithUIDorEmail: email];
216 return [contactInfos objectForKey: @"c_uid"];
219 - (BOOL) _ldapCheckLogin: (NSString *) login
220 andPassword: (NSString *) password
223 LDAPSource *ldapSource;
224 NSEnumerator *authIDs;
229 authIDs = [[self authenticationSourceIDs] objectEnumerator];
230 currentID = [authIDs nextObject];
231 while (currentID && !checkOK)
233 ldapSource = [sources objectForKey: currentID];
234 checkOK = [ldapSource checkLogin: login andPassword: password];
236 currentID = [authIDs nextObject];
242 - (BOOL) checkLogin: (NSString *) login
243 andPassword: (NSString *) password
247 NSMutableDictionary *currentUser;
248 NSString *dictPassword;
250 currentUser = [users objectForKey: login];
251 dictPassword = [currentUser objectForKey: @"password"];
252 if (currentUser && dictPassword)
253 checkOK = ([dictPassword isEqualToString: password]);
254 else if ([self _ldapCheckLogin: login andPassword: password])
259 currentUser = [NSMutableDictionary dictionary];
260 [users setObject: currentUser forKey: login];
262 [currentUser setObject: password forKey: @"password"];
269 cleanupDate = [[NSDate date] addTimeInterval: cleanupInterval];
270 [currentUser setObject: cleanupDate forKey: @"cleanupDate"];
276 - (void) _fillContactMailRecords: (NSMutableDictionary *) contact
278 NSMutableArray *emails;
279 NSString *uid, *systemEmail;
281 emails = [contact objectForKey: @"emails"];
282 uid = [contact objectForKey: @"c_uid"];
283 systemEmail = [NSString stringWithFormat: @"%@@%@", uid, defaultMailDomain];
284 if ([emails containsObject: systemEmail])
285 [emails removeObject: systemEmail];
286 [emails addObject: systemEmail];
287 [contact setObject: [emails objectAtIndex: 0] forKey: @"c_email"];
290 - (void) _fillContactInfosForUser: (NSMutableDictionary *) currentUser
291 withUIDorEmail: (NSString *) uid
293 NSMutableArray *emails;
294 NSDictionary *userEntry;
295 NSEnumerator *ldapSources;
296 LDAPSource *currentSource;
297 NSString *cn, *email, *c_uid;
299 emails = [NSMutableArray array];
303 ldapSources = [sources objectEnumerator];
304 currentSource = [ldapSources nextObject];
305 while (currentSource)
307 userEntry = [currentSource lookupContactEntryWithUIDorEmail: uid];
311 cn = [userEntry objectForKey: @"c_cn"];
313 c_uid = [userEntry objectForKey: @"c_uid"];
314 email = [userEntry objectForKey: @"mail"];
315 if (email && ![emails containsObject: email])
316 [emails addObject: email];
317 email = [userEntry objectForKey: @"mozillaSecondEmail"];
318 if (email && ![emails containsObject: email])
319 [emails addObject: email];
320 email = [userEntry objectForKey: @"xmozillasecondemail"];
321 if (email && ![emails containsObject: email])
322 [emails addObject: email];
324 currentSource = [ldapSources nextObject];
332 [currentUser setObject: emails forKey: @"emails"];
333 [currentUser setObject: cn forKey: @"cn"];
334 [currentUser setObject: c_uid forKey: @"c_uid"];
335 [self _fillContactMailRecords: currentUser];
338 - (void) _retainUser: (NSDictionary *) newUser
341 NSEnumerator *emails;
343 key = [newUser objectForKey: @"c_uid"];
345 [users setObject: newUser forKey: key];
346 emails = [[newUser objectForKey: @"emails"] objectEnumerator];
347 key = [emails nextObject];
350 [users setObject: newUser forKey: key];
351 key = [emails nextObject];
355 - (NSDictionary *) contactInfosForUserWithUIDorEmail: (NSString *) uid
357 NSMutableDictionary *currentUser, *contactInfos;
361 if ([uid length] > 0)
363 contactInfos = [NSMutableDictionary dictionary];
364 currentUser = [users objectForKey: uid];
365 if (!([currentUser objectForKey: @"emails"]
366 && [currentUser objectForKey: @"cn"]))
371 currentUser = [NSMutableDictionary dictionary];
375 [self _fillContactInfosForUser: currentUser
376 withUIDorEmail: uid];
379 if ([[currentUser objectForKey: @"c_uid"] length] > 0)
380 [self _retainUser: currentUser];
386 if (cleanupInterval && currentUser)
388 cleanupDate = [[NSDate date] addTimeInterval: cleanupInterval];
389 [currentUser setObject: cleanupDate forKey: @"cleanupDate"];
398 - (void) _fillContactsMailRecords: (NSEnumerator *) contacts
400 NSMutableDictionary *currentContact;
402 currentContact = [contacts nextObject];
403 while (currentContact)
405 [self _fillContactMailRecords: currentContact];
406 currentContact = [contacts nextObject];
410 - (NSArray *) _compactAndCompleteContacts: (NSEnumerator *) contacts
412 NSMutableDictionary *compactContacts, *returnContact;
413 NSDictionary *userEntry;
414 NSArray *newContacts;
415 NSMutableArray *emails;
416 NSString *uid, *email;
418 compactContacts = [NSMutableDictionary dictionary];
419 userEntry = [contacts nextObject];
422 uid = [userEntry objectForKey: @"c_uid"];
423 returnContact = [compactContacts objectForKey: uid];
426 returnContact = [NSMutableDictionary dictionary];
427 [returnContact setObject: uid forKey: @"c_uid"];
428 [compactContacts setObject: returnContact forKey: uid];
430 if (![[returnContact objectForKey: @"c_name"] length])
431 [returnContact setObject: [userEntry objectForKey: @"c_name"]
433 if (![[returnContact objectForKey: @"cn"] length])
434 [returnContact setObject: [userEntry objectForKey: @"c_cn"]
436 emails = [returnContact objectForKey: @"emails"];
439 emails = [NSMutableArray array];
440 [returnContact setObject: emails forKey: @"emails"];
442 email = [userEntry objectForKey: @"mail"];
443 if (email && ![emails containsObject: email])
444 [emails addObject: email];
445 email = [userEntry objectForKey: @"mozillaSecondEmail"];
446 if (email && ![emails containsObject: email])
447 [emails addObject: email];
448 email = [userEntry objectForKey: @"xmozillasecondemail"];
449 if (email && ![emails containsObject: email])
450 [emails addObject: email];
452 userEntry = [contacts nextObject];
454 newContacts = [compactContacts allValues];
455 [self _fillContactsMailRecords: [newContacts objectEnumerator]];
460 - (NSArray *) fetchContactsMatching: (NSString *) filter
462 NSMutableArray *contacts;
463 NSEnumerator *ldapSources;
464 LDAPSource *currentSource;
466 contacts = [NSMutableArray array];
467 ldapSources = [sources objectEnumerator];
468 currentSource = [ldapSources nextObject];
469 while (currentSource)
471 [contacts addObjectsFromArray:
472 [currentSource fetchContactsMatching: filter]];
473 currentSource = [ldapSources nextObject];
476 return [self _compactAndCompleteContacts: [contacts objectEnumerator]];
479 - (void) cleanupSources
481 NSEnumerator *userIDs;
483 NSDictionary *currentUser;
487 userIDs = [[users allKeys] objectEnumerator];
488 currentID = [userIDs nextObject];
491 currentUser = [users objectForKey: currentID];
492 if ([now earlierDate:
493 [currentUser objectForKey: @"cleanupDate"]] == now)
494 [users removeObjectForKey: currentID];
495 currentID = [userIDs nextObject];