2009-04-30 23:17:55 +02:00
|
|
|
/* SOGoGroup.m - this file is part of SOGo
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 Inverse inc.
|
|
|
|
*
|
|
|
|
* Author: Ludovic Marcotte <lmarcotte@inverse.ca>
|
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Here are some group samples:
|
|
|
|
|
|
|
|
|
|
|
|
[ POSIX group ]
|
|
|
|
|
|
|
|
dn: cn=it-staff,ou=Group,dc=zzz,dc=xxx,dc=yyy
|
|
|
|
objectClass: posixGroup
|
|
|
|
objectClass: top
|
|
|
|
cn: it-staff
|
|
|
|
userPassword: {crypt}x
|
|
|
|
gidNumber: 8000
|
|
|
|
memberUid: lsa
|
|
|
|
memberUid: mrm
|
|
|
|
memberUid: ij
|
|
|
|
memberUid: no
|
|
|
|
memberUid: ld
|
|
|
|
memberUid: db
|
|
|
|
memberUid: rgl
|
|
|
|
memberUid: ja
|
|
|
|
memberUid: hbt
|
|
|
|
memberUid: hossein
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SOGoGroup.h"
|
|
|
|
|
|
|
|
#import <Foundation/NSArray.h>
|
|
|
|
#import <Foundation/NSString.h>
|
|
|
|
|
|
|
|
#include "LDAPSource.h"
|
|
|
|
#include "LDAPUserManager.h"
|
|
|
|
#include "SOGoUser.h"
|
|
|
|
|
|
|
|
#import <NGLdap/NGLdapConnection.h>
|
|
|
|
#import <NGLdap/NGLdapAttribute.h>
|
|
|
|
#import <NGLdap/NGLdapEntry.h>
|
|
|
|
|
|
|
|
@implementation SOGoGroup
|
|
|
|
|
|
|
|
- (id) initWithIdentifier: (NSString *) theID
|
|
|
|
source: (LDAPSource *) theSource
|
|
|
|
entry: (NGLdapEntry *) theEntry
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
|
|
|
|
if (self)
|
|
|
|
{
|
|
|
|
ASSIGN(_identifier, theID);
|
|
|
|
ASSIGN(_source, theSource);
|
|
|
|
ASSIGN(_entry, theEntry);
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
RELEASE(_identifier);
|
|
|
|
RELEASE(_source);
|
|
|
|
RELEASE(_entry);
|
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2009-05-03 19:50:57 +02:00
|
|
|
+ (id) groupWithIdentifier: (NSString *) theID
|
|
|
|
{
|
|
|
|
NSString *uid;
|
|
|
|
|
|
|
|
uid = [theID hasPrefix: @"@"] ? [theID substringFromIndex: 1] : theID;
|
|
|
|
return [SOGoGroup groupWithValue: uid andSourceSelector: @selector (lookupGroupEntryByUID:)];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) groupWithEmail: (NSString *) theEmail
|
|
|
|
{
|
|
|
|
return [SOGoGroup groupWithValue: theEmail andSourceSelector: @selector (lookupGroupEntryByEmail:)];
|
|
|
|
}
|
|
|
|
|
2009-04-30 23:17:55 +02:00
|
|
|
//
|
2009-05-03 19:50:57 +02:00
|
|
|
// Returns nil if theValue doesn't match to a group
|
|
|
|
// (so its objectClass isn't a group)
|
2009-04-30 23:17:55 +02:00
|
|
|
//
|
2009-05-03 19:50:57 +02:00
|
|
|
+ (id) groupWithValue: (NSString *) theValue
|
|
|
|
andSourceSelector: (SEL) theSelector
|
2009-04-30 23:17:55 +02:00
|
|
|
{
|
|
|
|
NSArray *allSources;
|
|
|
|
NGLdapEntry *entry;
|
|
|
|
LDAPSource *source;
|
|
|
|
id o;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
// Don't bother looking in all sources if the
|
2009-05-03 19:50:57 +02:00
|
|
|
// supplied value is nil.
|
|
|
|
if (!theValue)
|
2009-04-30 23:17:55 +02:00
|
|
|
return nil;
|
|
|
|
|
|
|
|
allSources = [[LDAPUserManager sharedUserManager] sourceIDs];
|
|
|
|
o = nil;
|
|
|
|
|
|
|
|
for (i = 0; i < [allSources count]; i++)
|
|
|
|
{
|
|
|
|
source = [[LDAPUserManager sharedUserManager] sourceWithID: [allSources objectAtIndex: i]];
|
2009-05-03 19:50:57 +02:00
|
|
|
entry = [source performSelector: theSelector
|
|
|
|
withObject: theValue];
|
2009-04-30 23:17:55 +02:00
|
|
|
|
|
|
|
if (entry)
|
|
|
|
break;
|
|
|
|
|
|
|
|
entry = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
NSArray *classes;
|
|
|
|
|
|
|
|
// We check to see if it's a group
|
|
|
|
classes = [[entry attributeWithName: @"objectClass"] allStringValues];
|
2009-06-12 16:05:11 +02:00
|
|
|
NSLog(@"classes for %@ = %@", theValue, classes);
|
2009-04-30 23:17:55 +02:00
|
|
|
|
|
|
|
// Found a group, let's return it.
|
|
|
|
if ([classes containsObject: @"group"] ||
|
|
|
|
[classes containsObject: @"groupOfNames"] ||
|
|
|
|
[classes containsObject: @"groupOfUniqueNames"] ||
|
|
|
|
[classes containsObject: @"posixGroup"])
|
|
|
|
{
|
2009-05-03 19:50:57 +02:00
|
|
|
o = [[self alloc] initWithIdentifier: theValue
|
2009-04-30 23:17:55 +02:00
|
|
|
source: source
|
|
|
|
entry: entry];
|
|
|
|
AUTORELEASE(o);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// This method actually try to obtain all members
|
|
|
|
// from either dynamic of static groups.
|
|
|
|
//
|
|
|
|
- (NSArray *) members
|
|
|
|
{
|
|
|
|
NSMutableArray *dns, *uids;
|
|
|
|
NSMutableArray *array;
|
2009-05-03 19:50:57 +02:00
|
|
|
NSString *dn, *login;
|
2009-04-30 23:17:55 +02:00
|
|
|
SOGoUser *user;
|
|
|
|
NSArray *o;
|
2009-05-03 19:50:57 +02:00
|
|
|
LDAPUserManager *um;
|
2009-04-30 23:17:55 +02:00
|
|
|
int i, c;
|
|
|
|
|
|
|
|
array = [NSMutableArray array];
|
|
|
|
uids = [NSMutableArray array];
|
|
|
|
dns = [NSMutableArray array];
|
|
|
|
|
|
|
|
// We check if it's a static group
|
2009-06-09 19:50:25 +02:00
|
|
|
//NSLog(@"attributes = %@", [_entry attributes]);
|
2009-04-30 23:17:55 +02:00
|
|
|
|
|
|
|
// Fetch "members" - we get DNs
|
|
|
|
o = [[_entry attributeWithName: @"member"] allStringValues];
|
|
|
|
if (o) [dns addObjectsFromArray: o];
|
|
|
|
|
|
|
|
// Fetch "uniqueMembers" - we get DNs
|
|
|
|
o = [[_entry attributeWithName: @"uniqueMember"] allStringValues];
|
|
|
|
if (o) [dns addObjectsFromArray: o];
|
|
|
|
|
|
|
|
// Fetch "memberUid" - we get UID (like login names)
|
|
|
|
o = [[_entry attributeWithName: @"memberUid"] allStringValues];
|
|
|
|
if (o) [uids addObjectsFromArray: o];
|
|
|
|
|
|
|
|
c = [dns count] + [uids count];
|
|
|
|
|
|
|
|
NSLog(@"members count (static group): %d", c);
|
|
|
|
|
|
|
|
// We deal with a static group, let's add the members
|
|
|
|
if (c)
|
|
|
|
{
|
2009-05-03 19:50:57 +02:00
|
|
|
um = [LDAPUserManager sharedUserManager];
|
|
|
|
|
2009-04-30 23:17:55 +02:00
|
|
|
// We add members for whom we have their associated DN
|
|
|
|
for (i = 0; i < [dns count]; i++)
|
|
|
|
{
|
2009-05-03 19:50:57 +02:00
|
|
|
dn = [dns objectAtIndex: i];
|
2009-07-06 23:25:54 +02:00
|
|
|
login = [um getLoginForDN: [dn lowercaseString]];
|
2009-06-09 19:50:25 +02:00
|
|
|
//NSLog(@"member = %@", login);
|
2009-05-03 19:50:57 +02:00
|
|
|
user = [SOGoUser userWithLogin: login roles: nil];
|
2009-04-30 23:17:55 +02:00
|
|
|
if (user)
|
|
|
|
[array addObject: user];
|
|
|
|
}
|
|
|
|
|
|
|
|
// We add members for whom we have their associated login name
|
|
|
|
for (i = 0; i < [uids count]; i++)
|
|
|
|
{
|
|
|
|
login = [uids objectAtIndex: i];
|
|
|
|
NSLog(@"member = %@", login);
|
|
|
|
user = [SOGoUser userWithLogin: login roles: nil];
|
|
|
|
|
|
|
|
if (user)
|
|
|
|
[array addObject: user];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 array;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|