From cddfac82dd125294995684bf395562088b0254e0 Mon Sep 17 00:00:00 2001 From: Francis Lachapelle Date: Tue, 9 Jun 2020 15:15:06 -0400 Subject: [PATCH] Revert "Revert "Revert "fix(core): added back instance caching for LDAP members""" This reverts commit 056004bca11fe0843f06ad4c42496a8fa54cad1e. --- SoObjects/SOGo/LDAPSource.h | 1 - SoObjects/SOGo/LDAPSource.m | 193 ++++++++++++++++-------------------- 2 files changed, 85 insertions(+), 109 deletions(-) diff --git a/SoObjects/SOGo/LDAPSource.h b/SoObjects/SOGo/LDAPSource.h index 53312b7e0..d2446dd96 100644 --- a/SoObjects/SOGo/LDAPSource.h +++ b/SoObjects/SOGo/LDAPSource.h @@ -72,7 +72,6 @@ NSDictionary *_contactMapping; NSArray *_contactObjectClasses; NSArray *_groupObjectClasses; - NSMutableArray *_members; NSDictionary *_modulesConstraints; NSArray *_lookupFields; diff --git a/SoObjects/SOGo/LDAPSource.m b/SoObjects/SOGo/LDAPSource.m index 5f8ea98c3..9e1c38f4c 100644 --- a/SoObjects/SOGo/LDAPSource.m +++ b/SoObjects/SOGo/LDAPSource.m @@ -98,7 +98,6 @@ static Class NSStringK; // "name" expands to sn, displayname and cn _searchFields = [[NSArray arrayWithObjects: @"name", @"mail", @"telephonenumber", nil] retain]; _groupObjectClasses = [[NSArray arrayWithObjects: @"group", @"groupofnames", @"groupofuniquenames", @"posixgroup", nil] retain]; - _members = nil; _IMAPHostField = nil; _IMAPLoginField = nil; _SieveHostField = nil; @@ -145,7 +144,6 @@ static Class NSStringK; [_mailFields release]; [_searchFields release]; [_groupObjectClasses release]; - [_members release]; [_IMAPHostField release]; [_IMAPLoginField release]; [_SieveHostField release]; @@ -2039,92 +2037,90 @@ _makeLDAPChanges (NGLdapConnection *ldapConnection, NSAutoreleasePool *pool; int i, c; NGLdapEntry *entry; + NSMutableArray *members = nil; - if (!_members) + if ([uid hasPrefix: @"@"]) + uid = [uid substringFromIndex: 1]; + + entry = [self lookupGroupEntryByUID: uid inDomain: nil]; + + if (entry) { - if ([uid hasPrefix: @"@"]) - uid = [uid substringFromIndex: 1]; + members = [NSMutableArray new]; + uids = [NSMutableArray array]; + dns = [NSMutableArray array]; + logins = [NSMutableArray array]; - entry = [self lookupGroupEntryByUID: uid inDomain: nil]; + // We check if it's a static group + // Fetch "members" - we get DNs + d = [entry asDictionary]; + o = [d objectForKey: @"member"]; + CHECK_CLASS(o); + if (o) [dns addObjectsFromArray: o]; - if (entry) + // Fetch "uniqueMembers" - we get DNs + o = [d objectForKey: @"uniquemember"]; + CHECK_CLASS(o); + if (o) [dns addObjectsFromArray: o]; + + // Fetch "memberUid" - we get UID (like login names) + o = [d objectForKey: @"memberuid"]; + CHECK_CLASS(o); + if (o) [uids addObjectsFromArray: o]; + + c = [dns count] + [uids count]; + + // We deal with a static group, let's add the members + if (c) { - _members = [[NSMutableArray alloc] init]; - uids = [NSMutableArray array]; - dns = [NSMutableArray array]; - logins = [NSMutableArray array]; + um = [SOGoUserManager sharedUserManager]; - // We check if it's a static group - // Fetch "members" - we get DNs - d = [entry asDictionary]; - o = [d objectForKey: @"member"]; - CHECK_CLASS(o); - if (o) [dns addObjectsFromArray: o]; - - // Fetch "uniqueMembers" - we get DNs - o = [d objectForKey: @"uniquemember"]; - CHECK_CLASS(o); - if (o) [dns addObjectsFromArray: o]; - - // Fetch "memberUid" - we get UID (like login names) - o = [d objectForKey: @"memberuid"]; - CHECK_CLASS(o); - if (o) [uids addObjectsFromArray: o]; - - c = [dns count] + [uids count]; - - // We deal with a static group, let's add the members - if (c) + // We add members for whom we have their associated DN + for (i = 0; i < [dns count]; i++) { - um = [SOGoUserManager sharedUserManager]; - - // We add members for whom we have their associated DN - for (i = 0; i < [dns count]; i++) + pool = [NSAutoreleasePool new]; + dn = [dns objectAtIndex: i]; + login = [um getLoginForDN: [dn lowercaseString]]; + user = [SOGoUser userWithLogin: login roles: nil]; + if (user) { - pool = [NSAutoreleasePool new]; - dn = [dns objectAtIndex: i]; - login = [um getLoginForDN: [dn lowercaseString]]; - user = [SOGoUser userWithLogin: login roles: nil]; - if (user) - { - [logins addObject: login]; - [_members addObject: [NSDictionary dictionaryWithObject: login - forKey: @"c_uid"]]; - } - [pool release]; + [logins addObject: login]; + [members addObject: [NSDictionary dictionaryWithObject: login + forKey: @"c_uid"]]; } - - // We add members for whom we have their associated login name - for (i = 0; i < [uids count]; i++) - { - pool = [NSAutoreleasePool new]; - login = [uids objectAtIndex: i]; - user = [SOGoUser userWithLogin: login roles: nil]; - if (user) - { - [logins addObject: login]; - [_members addObject: [NSDictionary dictionaryWithObject: login - forKey: @"c_uid"]]; - } - [pool release]; - } - - - // We are done fetching members, let's cache the members of the group - // (ie., their UIDs) in memcached to speed up -hasMemberWithUID. - [[SOGoCache sharedCache] setValue: [logins componentsJoinedByString: @","] - forKey: [NSString stringWithFormat: @"%@+%@", uid, _domain]]; + [pool release]; } - else + + // We add members for whom we have their associated login name + for (i = 0; i < [uids count]; i++) { - // We deal with a dynamic group, let's search all users for whom - // memberOf is equal to our group's DN. - // We also need to look for labelelURI? + pool = [NSAutoreleasePool new]; + login = [uids objectAtIndex: i]; + user = [SOGoUser userWithLogin: login roles: nil]; + if (user) + { + [logins addObject: login]; + [members addObject: [NSDictionary dictionaryWithObject: login + forKey: @"c_uid"]]; + } + [pool release]; } + + + // We are done fetching members, let's cache the members of the group + // (ie., their UIDs) in memcached to speed up -hasMemberWithUID. + [[SOGoCache sharedCache] setValue: [logins componentsJoinedByString: @","] + forKey: [NSString stringWithFormat: @"%@+%@", uid, _domain]]; + } + else + { + // We deal with a dynamic group, let's search all users for whom + // memberOf is equal to our group's DN. + // We also need to look for labelelURI? } } - return _members; + return members; } // @@ -2133,49 +2129,30 @@ _makeLDAPChanges (NGLdapConnection *ldapConnection, - (BOOL) groupWithUIDHasMemberWithUID: (NSString *) uid memberUid: (NSString *) memberUid { + BOOL rc; + NSString *key, *value;; + NSArray *a; rc = NO; - // If _members is initialized, we use it as it's very accurate. - // Otherwise, we fallback on memcached in order to avoid - // decomposing the group all the time just to see if a user - // is a member of it. - if (_members) + if ([uid hasPrefix: @"@"]) + uid = [uid substringFromIndex: 1]; + + key = [NSString stringWithFormat: @"%@+%@", uid, _domain]; + value = [[SOGoCache sharedCache] valueForKey: key]; + + // If the value isn't in memcached, that probably means -members was never called. + // We call it only once here. + if (!value) { - NSString *currentUID; - int count, max; - - max = [_members count]; - for (count = 0; !rc && count < max; count++) - { - currentUID = [[_members objectAtIndex: count] objectForKey: @"c_uid"]; - rc = [memberUid isEqualToString: currentUID]; - } - } - else - { - NSString *key, *value; - NSArray *a; - - if ([uid hasPrefix: @"@"]) - uid = [uid substringFromIndex: 1]; - - key = [NSString stringWithFormat: @"%@+%@", uid, _domain]; + [self membersForGroupWithUID: uid]; value = [[SOGoCache sharedCache] valueForKey: key]; - - // If the value isn't in memcached, that probably means -members was never called. - // We call it only once here. - if (!value) - { - [self membersForGroupWithUID: uid]; - value = [[SOGoCache sharedCache] valueForKey: key]; - } - - a = [value componentsSeparatedByString: @","]; - rc = [a containsObject: memberUid]; } + a = [value componentsSeparatedByString: @","]; + rc = [a containsObject: memberUid]; + return rc; }