From 2ef47d4cf32e43674dff8b817c293d6fe95bdffc Mon Sep 17 00:00:00 2001 From: znek Date: Tue, 19 Oct 2004 17:53:46 +0000 Subject: [PATCH] some LDAP stuff git-svn-id: http://svn.opengroupware.org/SOGo/trunk@411 d1b88da0-ebda-0310-925b-ed51d893ca5b --- SOGo/SoObjects/Mailer/ChangeLog | 5 + SOGo/SoObjects/Mailer/SOGoMailAccounts.m | 18 +++- SOGo/SoObjects/Mailer/Version | 2 +- SOGoLogic/AgenorUserManager.h | 2 + SOGoLogic/AgenorUserManager.m | 119 ++++++++++++++++++++--- SOGoLogic/ChangeLog | 6 ++ SOGoLogic/Version | 2 +- 7 files changed, 135 insertions(+), 19 deletions(-) diff --git a/SOGo/SoObjects/Mailer/ChangeLog b/SOGo/SoObjects/Mailer/ChangeLog index 2198a063..e9d301e3 100644 --- a/SOGo/SoObjects/Mailer/ChangeLog +++ b/SOGo/SoObjects/Mailer/ChangeLog @@ -1,3 +1,8 @@ +2004-10-19 Marcus Mueller + + * SOGoMailAccounts.m: values for toManyRelationshipKeys are constructed + from information retrieved by the AgenorUserManager now. (v0.9.34) + 2004-10-11 Helge Hess * SOGoDraftsFolder.m: added methods to deal with new draft objects diff --git a/SOGo/SoObjects/Mailer/SOGoMailAccounts.m b/SOGo/SoObjects/Mailer/SOGoMailAccounts.m index 0682a40a..078269c1 100644 --- a/SOGo/SoObjects/Mailer/SOGoMailAccounts.m +++ b/SOGo/SoObjects/Mailer/SOGoMailAccounts.m @@ -23,16 +23,26 @@ #include "SOGoMailAccounts.h" #include "common.h" #include +#include @implementation SOGoMailAccounts /* listing the available mailboxes */ - (NSArray *)toManyRelationshipKeys { - // TODO: hardcoded, need to implement the Agenor Anais LDAP query? - return [NSArray arrayWithObjects: - @"agenortest@mail.opengroupware.org", - nil]; + static AgenorUserManager *um = nil; + NSString *uid, *server, *account; + + if(!um) + um = [AgenorUserManager sharedUserManager]; + uid = [[self container] davDisplayName]; + server = [um getIMAPServerForUID:uid]; + if(!server) + return nil; + account = [NSString stringWithFormat:@"%@@%@", + uid, + server]; + return [NSArray arrayWithObject:account]; } /* name lookup */ diff --git a/SOGo/SoObjects/Mailer/Version b/SOGo/SoObjects/Mailer/Version index a416fb2d..8d1e9bd6 100644 --- a/SOGo/SoObjects/Mailer/Version +++ b/SOGo/SoObjects/Mailer/Version @@ -1,3 +1,3 @@ # $Id$ -SUBMINOR_VERSION:=33 +SUBMINOR_VERSION:=34 diff --git a/SOGoLogic/AgenorUserManager.h b/SOGoLogic/AgenorUserManager.h index 2d9177e2..910ded38 100644 --- a/SOGoLogic/AgenorUserManager.h +++ b/SOGoLogic/AgenorUserManager.h @@ -40,6 +40,8 @@ - (NSString *)getCNForUID:(NSString *)_uid; +- (NSString *)getIMAPServerForUID:(NSString *)_uid; + @end #endif /* __AgenorUserManager_H_ */ diff --git a/SOGoLogic/AgenorUserManager.m b/SOGoLogic/AgenorUserManager.m index 60842b44..062aa262 100644 --- a/SOGoLogic/AgenorUserManager.m +++ b/SOGoLogic/AgenorUserManager.m @@ -21,11 +21,38 @@ // $Id$ -#import "AgenorUserManager.h" +#include "AgenorUserManager.h" +#include + + +@interface AgenorUserManager (PrivateAPI) +- (NGLdapConnection *)ldapConnection; +@end @implementation AgenorUserManager +static BOOL debugOn = NO; +static BOOL useLDAP = NO; +static NSString *ldapHost = nil; +static NSString *ldapBaseDN = nil; + ++ (void)initialize { + static BOOL didInit = NO; + NSUserDefaults *ud; + + if(didInit) + return; + didInit = YES; + ud = [NSUserDefaults standardUserDefaults]; + debugOn = [ud boolForKey:@"SOGoUserManagerDebugEnabled"]; + useLDAP = [ud boolForKey:@"SOGoUserManagerUsesLDAP"]; + if(useLDAP) { + ldapHost = [[ud stringForKey:@"SOGoLDAPHost"] retain]; + ldapBaseDN = [[ud stringForKey:@"SOGoLDAPBaseDN"] retain]; + } +} + + (id)sharedUserManager { static id mgr = nil; if(mgr == nil) { @@ -45,6 +72,16 @@ [super dealloc]; } +- (NGLdapConnection *)ldapConnection { + static NGLdapConnection *ldapConnection = nil; + if(!ldapConnection) { + ldapConnection = [[NGLdapConnection alloc] initWithHostName:ldapHost]; +#if 0 + [ldapConnection setUseCache:YES]; +#endif + } + return ldapConnection; +} - (NSString *)getUIDForEmail:(NSString *)_email { NSRange r; @@ -72,20 +109,76 @@ } - (NSString *)getCNForUID:(NSString *)_uid { -#warning !! IMPLEMENT ME!! - NSString *s; - NSRange r; - - s = _uid; - if ([s length] < 10) + if(useLDAP) { + static NSArray *cnAttrs = nil; + NGLdapConnection *conn; + EOQualifier *q; + NSEnumerator *resultEnum; + NGLdapEntry *entry; + NGLdapAttribute *cnAttr; + + if(!cnAttrs) { + cnAttrs = [[NSArray alloc] initWithObjects:@"cn", nil]; + } + q = [EOQualifier qualifierWithQualifierFormat:@"uid = %@", _uid]; + + conn = [self ldapConnection]; + resultEnum = [conn deepSearchAtBaseDN:ldapBaseDN + qualifier:q + attributes:cnAttrs]; + entry = [resultEnum nextObject]; + if(!entry) { + if(debugOn) { + NSLog(@"Didn't find LDAP entry for uid '%@'!", _uid); + } + return nil; + } + cnAttr = [entry attributeWithName:@"cn"]; + if(!cnAttr && debugOn) { + NSLog(@"LDAP entry for uid '%@' has no common name?", _uid); + } + return [cnAttr stringValueAtIndex:0]; + } + else { + NSString *s; + NSRange r; + + s = _uid; + if ([s length] < 10) return s; - - // TODO: algorithm might be inappropriate, depends on the actual UID - r = [s rangeOfString:@"."]; - if (r.length == 0) + + // TODO: algorithm might be inappropriate, depends on the actual UID + r = [s rangeOfString:@"."]; + if (r.length == 0) return s; - - return [s substringToIndex:r.location]; + + return [s substringToIndex:r.location]; + } +} + +- (NSString *)getIMAPServerForUID:(NSString *)_uid { + /* + First of all : for a particular user IMAP and SMTP are served on the same + host. + + The name of the machine is determined by applying a regex on every values of + the mineqMelRoutage LDAP attribute. + The regex is : .*%.*@(.*\.melanie2\.i2$) + It extracts the substring that follows '@', ends with 'melanie2', on + adresses which have a '%' before the '@' + + Example: helge.hesse%opengroupware.org@servername1.melanie2.i2 + -> servername1.melanie2.i2 + + If only one server name is found by applying the regex on every value of the + attribute, then this name is the IMAP/SMTP server for that user. + Note that this is the case when we got a unique (well formed) value for the + attribute. + If the regex finds more than one servername when applied to the differents + values, then the IMAP/SMTP server name is to be found in the + mineqMelServeurPrincipal attribute of the user. + */ + return @"mail.opengroupware.org"; } @end diff --git a/SOGoLogic/ChangeLog b/SOGoLogic/ChangeLog index 5e2ddc31..7d9cc6eb 100644 --- a/SOGoLogic/ChangeLog +++ b/SOGoLogic/ChangeLog @@ -1,3 +1,9 @@ +2004-10-19 Marcus Mueller + + * AgenorUserManager.[hm]: added an interface to retrieve the IMAP + server for a particular UID. Also, AgenorUserManager uses LDAP + now *if* certain defaults are set. (v0.9.22) + 2004-10-19 Helge Hess * v0.9.21 diff --git a/SOGoLogic/Version b/SOGoLogic/Version index c331e5f9..79ebcd59 100644 --- a/SOGoLogic/Version +++ b/SOGoLogic/Version @@ -1,6 +1,6 @@ # $Id$ -SUBMINOR_VERSION:=20 +SUBMINOR_VERSION:=22 # v0.9.18 requires NGExtensions v4.3.123 # v0.9.13 requires libFoundation v1.0.62 -- 2.39.5