Revert "Revert "fix(core): added back instance caching for LDAP members""

This reverts commit ca5df1a153.
pull/280/head
Ludovic Marcotte 2020-06-08 09:53:44 -04:00
parent dbf5179e69
commit 056004bca1
2 changed files with 112 additions and 88 deletions

View File

@ -72,6 +72,7 @@
NSDictionary *_contactMapping; NSDictionary *_contactMapping;
NSArray *_contactObjectClasses; NSArray *_contactObjectClasses;
NSArray *_groupObjectClasses; NSArray *_groupObjectClasses;
NSMutableArray *_members;
NSDictionary *_modulesConstraints; NSDictionary *_modulesConstraints;

View File

@ -98,6 +98,7 @@ static Class NSStringK;
// "name" expands to sn, displayname and cn // "name" expands to sn, displayname and cn
_searchFields = [[NSArray arrayWithObjects: @"name", @"mail", @"telephonenumber", nil] retain]; _searchFields = [[NSArray arrayWithObjects: @"name", @"mail", @"telephonenumber", nil] retain];
_groupObjectClasses = [[NSArray arrayWithObjects: @"group", @"groupofnames", @"groupofuniquenames", @"posixgroup", nil] retain]; _groupObjectClasses = [[NSArray arrayWithObjects: @"group", @"groupofnames", @"groupofuniquenames", @"posixgroup", nil] retain];
_members = nil;
_IMAPHostField = nil; _IMAPHostField = nil;
_IMAPLoginField = nil; _IMAPLoginField = nil;
_SieveHostField = nil; _SieveHostField = nil;
@ -142,6 +143,7 @@ static Class NSStringK;
[_mailFields release]; [_mailFields release];
[_searchFields release]; [_searchFields release];
[_groupObjectClasses release]; [_groupObjectClasses release];
[_members release];
[_IMAPHostField release]; [_IMAPHostField release];
[_IMAPLoginField release]; [_IMAPLoginField release];
[_SieveHostField release]; [_SieveHostField release];
@ -2033,90 +2035,92 @@ _makeLDAPChanges (NGLdapConnection *ldapConnection,
NSAutoreleasePool *pool; NSAutoreleasePool *pool;
int i, c; int i, c;
NGLdapEntry *entry; NGLdapEntry *entry;
NSMutableArray *members = nil;
if ([uid hasPrefix: @"@"]) if (!_members)
uid = [uid substringFromIndex: 1];
entry = [self lookupGroupEntryByUID: uid inDomain: nil];
if (entry)
{ {
members = [NSMutableArray new]; if ([uid hasPrefix: @"@"])
uids = [NSMutableArray array]; uid = [uid substringFromIndex: 1];
dns = [NSMutableArray array];
logins = [NSMutableArray array];
// We check if it's a static group entry = [self lookupGroupEntryByUID: uid inDomain: nil];
// 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 if (entry)
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)
{ {
um = [SOGoUserManager sharedUserManager]; _members = [[NSMutableArray alloc] init];
uids = [NSMutableArray array];
dns = [NSMutableArray array];
logins = [NSMutableArray array];
// We add members for whom we have their associated DN // We check if it's a static group
for (i = 0; i < [dns count]; i++) // 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)
{ {
pool = [NSAutoreleasePool new]; um = [SOGoUserManager sharedUserManager];
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];
}
// We add members for whom we have their associated login name // We add members for whom we have their associated DN
for (i = 0; i < [uids count]; i++) 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)
{
[logins addObject: login];
[_members addObject: [NSDictionary dictionaryWithObject: login
forKey: @"c_uid"]];
}
[pool release];
}
// 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]];
}
else
{ {
pool = [NSAutoreleasePool new]; // We deal with a dynamic group, let's search all users for whom
login = [uids objectAtIndex: i]; // memberOf is equal to our group's DN.
user = [SOGoUser userWithLogin: login roles: nil]; // We also need to look for labelelURI?
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;
} }
// //
@ -2125,29 +2129,48 @@ _makeLDAPChanges (NGLdapConnection *ldapConnection,
- (BOOL) groupWithUIDHasMemberWithUID: (NSString *) uid - (BOOL) groupWithUIDHasMemberWithUID: (NSString *) uid
memberUid: (NSString *) memberUid memberUid: (NSString *) memberUid
{ {
BOOL rc; BOOL rc;
NSString *key, *value;;
NSArray *a;
rc = NO; rc = NO;
if ([uid hasPrefix: @"@"]) // If _members is initialized, we use it as it's very accurate.
uid = [uid substringFromIndex: 1]; // Otherwise, we fallback on memcached in order to avoid
// decomposing the group all the time just to see if a user
key = [NSString stringWithFormat: @"%@+%@", uid, _domain]; // is a member of it.
value = [[SOGoCache sharedCache] valueForKey: key]; if (_members)
// 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]; NSString *currentUID;
value = [[SOGoCache sharedCache] valueForKey: key]; int count, max;
}
a = [value componentsSeparatedByString: @","]; max = [_members count];
rc = [a containsObject: memberUid]; 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];
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];
}
return rc; return rc;
} }