]> err.no Git - scalable-opengroupware.org/blob - SoObjects/SOGo/LDAPUserManager.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1101 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / SoObjects / SOGo / LDAPUserManager.m
1 /* LDAPUserManager.m - this file is part of SOGo
2  *
3  * Copyright (C) 2007 Inverse groupe conseil
4  *
5  * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
6  *
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)
10  * any later version.
11  *
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.
16  *
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.
21  */
22
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>
29
30 #import "LDAPSource.h"
31 #import "LDAPUserManager.h"
32
33 static NSString *defaultMailDomain = nil;
34
35 @implementation LDAPUserManager
36
37 + (void) initialize
38 {
39   NSUserDefaults *ud;
40
41   ud = [NSUserDefaults standardUserDefaults];
42   if (!defaultMailDomain)
43     {
44       defaultMailDomain = [ud stringForKey: @"SOGoDefaultMailDomain"];
45       [defaultMailDomain retain];
46     }
47 }
48
49 + (id) sharedUserManager
50 {
51   static id sharedUserManager = nil;
52
53   if (!sharedUserManager)
54     sharedUserManager = [self new];
55
56   return sharedUserManager;
57 }
58
59 - (void) _registerSource: (NSDictionary *) udSource
60 {
61   NSMutableDictionary *metadata;
62   LDAPSource *ldapSource;
63   NSString *sourceID, *value;
64   
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"];
70   if (value)
71     [metadata setObject: value forKey: @"canAuthenticate"];
72   value = [udSource objectForKey: @"isAddressBook"];
73   if (value)
74     [metadata setObject: value forKey: @"isAddressBook"];
75   value = [udSource objectForKey: @"displayName"];
76   if (value)
77     [metadata setObject: value forKey: @"displayName"];
78   [sourcesMetadata setObject: metadata forKey: sourceID];
79 }
80
81 - (void) _prepareLDAPSourcesWithDefaults: (NSUserDefaults *) ud
82 {
83   NSArray *udSources;
84   unsigned int count, max;
85
86   sources = [NSMutableDictionary new];
87   sourcesMetadata = [NSMutableDictionary new];
88
89   udSources = [ud arrayForKey: @"SOGoLDAPSources"];
90   max = [udSources count];
91   for (count = 0; count < max; count++)
92     [self _registerSource: [udSources objectAtIndex: count]];
93 }
94
95 - (id) init
96 {
97   NSUserDefaults *ud;
98
99   if ((self = [super init]))
100     {
101       ud = [NSUserDefaults standardUserDefaults];
102
103       sources = nil;
104       sourcesMetadata = nil;
105       users = [NSMutableDictionary new];
106       cleanupInterval
107         = [ud integerForKey: @"SOGOLDAPUserManagerCleanupInterval"];
108       if (cleanupInterval)
109         cleanupTimer = [NSTimer timerWithTimeInterval: cleanupInterval
110                                 target: self
111                                 selector: @selector (cleanupUsers)
112                                 userInfo: nil
113                                 repeats: YES];
114       [self _prepareLDAPSourcesWithDefaults: ud];
115     }
116
117   return self;
118 }
119
120 - (void) dealloc
121 {
122   [sources release];
123   [users release];
124   [super dealloc];
125 }
126
127 - (NSArray *) sourceIDs
128 {
129   return [sources allKeys];
130 }
131
132 - (NSArray *) _sourcesOfType: (NSString *) sourceType
133 {
134   NSMutableArray *sourceIDs;
135   NSEnumerator *allIDs;
136   NSString *currentID;
137   NSNumber *canAuthenticate;
138
139   sourceIDs = [NSMutableArray array];
140   allIDs = [[sources allKeys] objectEnumerator];
141   currentID = [allIDs nextObject];
142   while (currentID)
143     {
144       canAuthenticate = [[sourcesMetadata objectForKey: currentID]
145                           objectForKey: sourceType];
146       if ([canAuthenticate boolValue])
147         [sourceIDs addObject: currentID];
148       currentID = [allIDs nextObject];
149     }
150
151   return sourceIDs;
152 }
153
154 - (NSArray *) authenticationSourceIDs
155 {
156   return [self _sourcesOfType: @"canAuthenticate"];
157 }
158
159 - (NSArray *) addressBookSourceIDs
160 {
161   return [self _sourcesOfType: @"isAddressBook"];
162 }
163
164 - (LDAPSource *) sourceWithID: (NSString *) sourceID
165 {
166   return [sources objectForKey: sourceID];
167 }
168
169 - (NSString *) displayNameForSourceWithID: (NSString *) sourceID
170 {
171   NSDictionary *metadata;
172
173   metadata = [sourcesMetadata objectForKey: sourceID];
174
175   return [metadata objectForKey: @"displayName"];
176 }
177
178 - (NSString *) getCNForUID: (NSString *) uid
179 {
180   NSDictionary *contactInfos;
181
182 //   NSLog (@"getCNForUID: %@", uid);
183   contactInfos = [self contactInfosForUserWithUIDorEmail: uid];
184
185   return [contactInfos objectForKey: @"cn"];
186 }
187
188 - (NSString *) getEmailForUID: (NSString *) uid
189 {
190   NSDictionary *contactInfos;
191
192 //   NSLog (@"getEmailForUID: %@", uid);
193   contactInfos = [self contactInfosForUserWithUIDorEmail: uid];
194
195   return [contactInfos objectForKey: @"c_email"];
196 }
197
198 - (NSString *) getFullEmailForUID: (NSString *) uid
199 {
200   NSDictionary *contactInfos;
201
202   contactInfos = [self contactInfosForUserWithUIDorEmail: uid];
203
204   return [NSString stringWithFormat: @"%@ <%@>",
205                    [contactInfos objectForKey: @"cn"],
206                    [contactInfos objectForKey: @"c_email"]];
207 }
208
209 - (NSString *) getUIDForEmail: (NSString *) email
210 {
211   NSDictionary *contactInfos;
212
213 //   NSLog (@"getUIDForEmail: %@", email);
214   contactInfos = [self contactInfosForUserWithUIDorEmail: email];
215
216   return [contactInfos objectForKey: @"c_uid"];
217 }
218
219 - (BOOL) _ldapCheckLogin: (NSString *) login
220              andPassword: (NSString *) password
221
222   BOOL checkOK;
223   LDAPSource *ldapSource;
224   NSEnumerator *authIDs;
225   NSString *currentID;
226
227   checkOK = NO;
228
229   authIDs = [[self authenticationSourceIDs] objectEnumerator];
230   currentID = [authIDs nextObject];
231   while (currentID && !checkOK)
232     {
233       ldapSource = [sources objectForKey: currentID];
234       checkOK = [ldapSource checkLogin: login andPassword: password];
235       if (!checkOK)
236         currentID = [authIDs nextObject];
237     }
238
239   return checkOK;
240 }
241
242 - (BOOL) checkLogin: (NSString *) login
243         andPassword: (NSString *) password
244 {
245   BOOL checkOK;
246   NSDate *cleanupDate;
247   NSMutableDictionary *currentUser;
248   NSString *dictPassword;
249
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])
255     {
256       checkOK = YES;
257       if (!currentUser)
258         {
259           currentUser = [NSMutableDictionary dictionary];
260           [users setObject: currentUser forKey: login];
261         }
262       [currentUser setObject: password forKey: @"password"];
263     }
264   else
265     checkOK = NO;
266
267   if (cleanupInterval)
268     {
269       cleanupDate = [[NSDate date] addTimeInterval: cleanupInterval];
270       [currentUser setObject: cleanupDate forKey: @"cleanupDate"];
271     }
272
273   return checkOK;
274 }
275
276 - (void) _fillContactMailRecords: (NSMutableDictionary *) contact
277 {
278   NSMutableArray *emails;
279   NSString *uid, *systemEmail;
280
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"];
288 }
289
290 - (void) _fillContactInfosForUser: (NSMutableDictionary *) currentUser
291                    withUIDorEmail: (NSString *) uid
292 {
293   NSMutableArray *emails;
294   NSDictionary *userEntry;
295   NSEnumerator *ldapSources;
296   LDAPSource *currentSource;
297   NSString *cn, *email, *c_uid;
298
299   emails = [NSMutableArray array];
300   cn = nil;
301   c_uid = nil;
302
303   ldapSources = [sources objectEnumerator];
304   currentSource = [ldapSources nextObject];
305   while (currentSource)
306     {
307       userEntry = [currentSource lookupContactEntryWithUIDorEmail: uid];
308       if (userEntry)
309         {
310           if (!cn)
311             cn = [userEntry objectForKey: @"c_cn"];
312           if (!c_uid)
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];
323         }
324       currentSource = [ldapSources nextObject];
325     }
326
327   if (!cn)
328     cn = @"";
329   if (!c_uid)
330     c_uid = @"";
331
332   [currentUser setObject: emails forKey: @"emails"];
333   [currentUser setObject: cn forKey: @"cn"];
334   [currentUser setObject: c_uid forKey: @"c_uid"];
335   [self _fillContactMailRecords: currentUser];
336 }
337
338 - (void) _retainUser: (NSDictionary *) newUser
339 {
340   NSString *key;
341   NSEnumerator *emails;
342
343   key = [newUser objectForKey: @"c_uid"];
344   if (key)
345     [users setObject: newUser forKey: key];
346   emails = [[newUser objectForKey: @"emails"] objectEnumerator];
347   key = [emails nextObject];
348   while (key)
349     {
350       [users setObject: newUser forKey: key];
351       key = [emails nextObject];
352     }
353 }
354
355 - (NSDictionary *) contactInfosForUserWithUIDorEmail: (NSString *) uid
356 {
357   NSMutableDictionary *currentUser, *contactInfos;
358   NSDate *cleanupDate;
359   BOOL newUser;
360
361   if ([uid length] > 0)
362     {
363       contactInfos = [NSMutableDictionary dictionary];
364       currentUser = [users objectForKey: uid];
365       if (!([currentUser objectForKey: @"emails"]
366             && [currentUser objectForKey: @"cn"]))
367         {
368           if (!currentUser)
369             {
370               newUser = YES;
371               currentUser = [NSMutableDictionary dictionary];
372             }
373           else
374             newUser = NO;
375           [self _fillContactInfosForUser: currentUser
376                 withUIDorEmail: uid];
377           if (newUser)
378             {
379               if ([[currentUser objectForKey: @"c_uid"] length] > 0)
380                 [self _retainUser: currentUser];
381               else
382                 currentUser = nil;
383             }
384         }
385
386       if (cleanupInterval && currentUser)
387         {
388           cleanupDate = [[NSDate date] addTimeInterval: cleanupInterval];
389           [currentUser setObject: cleanupDate forKey: @"cleanupDate"];
390         }
391     }
392   else
393     currentUser = nil;
394
395   return currentUser;
396 }
397
398 - (void) _fillContactsMailRecords: (NSEnumerator *) contacts
399 {
400   NSMutableDictionary *currentContact;
401
402   currentContact = [contacts nextObject];
403   while (currentContact)
404     {
405       [self _fillContactMailRecords: currentContact];
406       currentContact = [contacts nextObject];
407     }
408 }
409
410 - (NSArray *) _compactAndCompleteContacts: (NSEnumerator *) contacts
411 {
412   NSMutableDictionary *compactContacts, *returnContact;
413   NSDictionary *userEntry;
414   NSArray *newContacts;
415   NSMutableArray *emails;
416   NSString *uid, *email;
417
418   compactContacts = [NSMutableDictionary dictionary];
419   userEntry = [contacts nextObject];
420   while (userEntry)
421     {
422       uid = [userEntry objectForKey: @"c_uid"];
423       returnContact = [compactContacts objectForKey: uid];
424       if (!returnContact)
425         {
426           returnContact = [NSMutableDictionary dictionary];
427           [returnContact setObject: uid forKey: @"c_uid"];
428           [compactContacts setObject: returnContact forKey: uid];
429         }
430       if (![[returnContact objectForKey: @"c_name"] length])
431         [returnContact setObject: [userEntry objectForKey: @"c_name"]
432                        forKey: @"c_name"];
433       if (![[returnContact objectForKey: @"cn"] length])
434         [returnContact setObject: [userEntry objectForKey: @"c_cn"]
435                        forKey: @"cn"];
436       emails = [returnContact objectForKey: @"emails"];
437       if (!emails)
438         {
439           emails = [NSMutableArray array];
440           [returnContact setObject: emails forKey: @"emails"];
441         }
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];
451
452       userEntry = [contacts nextObject];
453     }
454   newContacts = [compactContacts allValues];
455   [self _fillContactsMailRecords: [newContacts objectEnumerator]];
456
457   return newContacts;
458 }
459
460 - (NSArray *) fetchContactsMatching: (NSString *) filter
461 {
462   NSMutableArray *contacts;
463   NSEnumerator *ldapSources;
464   LDAPSource *currentSource;
465
466   contacts = [NSMutableArray array];
467   ldapSources = [sources objectEnumerator];
468   currentSource = [ldapSources nextObject];
469   while (currentSource)
470     {
471       [contacts addObjectsFromArray:
472                   [currentSource fetchContactsMatching: filter]];
473       currentSource = [ldapSources nextObject];
474     }
475
476   return [self _compactAndCompleteContacts: [contacts objectEnumerator]];
477 }
478
479 - (void) cleanupSources
480 {
481   NSEnumerator *userIDs;
482   NSString *currentID;
483   NSDictionary *currentUser;
484   NSDate *now;
485
486   now = [NSDate date];
487   userIDs = [[users allKeys] objectEnumerator];
488   currentID = [userIDs nextObject];
489   while (currentID)
490     {
491       currentUser = [users objectForKey: currentID];
492       if ([now earlierDate:
493                  [currentUser objectForKey: @"cleanupDate"]] == now)
494         [users removeObjectForKey: currentID];
495       currentID = [userIDs nextObject];
496     }
497 }
498
499 @end