[go: up one dir, main page]

File: SOGoGroup.m

package info (click to toggle)
sogo 1.3.16-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 21,620 kB
  • sloc: objc: 99,218; python: 4,840; sh: 1,217; perl: 861; makefile: 132; sql: 53; php: 43; ansic: 4
file content (335 lines) | stat: -rw-r--r-- 8,473 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/* SOGoGroup.m - this file is part of SOGo
 *
 * Copyright (C) 2009-2012 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

  dn: cn=inverse,ou=groups,dc=inverse,dc=ca
  objectClass: groupOfUniqueNames
  objectClass: top
  objectClass: extensibleObject
  uniqueMember: uid=flachapelle,ou=users,dc=inverse,dc=ca
  uniqueMember: uid=lmarcotte,ou=users,dc=inverse,dc=ca
  uniqueMember: uid=wsourdeau,ou=users,dc=inverse,dc=ca
  cn: inverse
  mail: inverse@inverse.ca

 */

#include "SOGoGroup.h"

#import <Foundation/NSArray.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSString.h>

#include "SOGoCache.h"
#include "SOGoSource.h"
#include "SOGoUserManager.h"
#include "SOGoUser.h"

#import <NGLdap/NGLdapConnection.h>
#import <NGLdap/NGLdapAttribute.h>
#import <NGLdap/NGLdapEntry.h>

@implementation SOGoGroup

- (id) initWithIdentifier: (NSString *) theID
		   domain: (NSString *) theDomain
		   source: (NSObject <SOGoSource> *) theSource
		    entry: (NGLdapEntry *) theEntry
{
  self = [super init];

  if (self)
    {
      ASSIGN(_identifier, theID);
      ASSIGN(_domain, theDomain);
      ASSIGN(_source, theSource);
      ASSIGN(_entry, theEntry);
      _members = nil;
    }

  return self;
}

- (void) dealloc
{
  RELEASE(_identifier);
  RELEASE(_domain);
  RELEASE(_source);
  RELEASE(_entry);
  RELEASE(_members);

  [super dealloc];
}

+ (id) groupWithIdentifier: (NSString *) theID
                  inDomain: (NSString *) domain
{
  NSString *uid;

  uid = [theID hasPrefix: @"@"] ? [theID substringFromIndex: 1] : theID;

  return [SOGoGroup groupWithValue: uid
                 andSourceSelector: @selector (lookupGroupEntryByUID:)
                          inDomain: domain];
}

+ (id) groupWithEmail: (NSString *) theEmail
             inDomain: (NSString *) domain
{
  return [SOGoGroup groupWithValue: theEmail
                 andSourceSelector: @selector (lookupGroupEntryByEmail:)
                          inDomain: domain];
}

//
// Returns nil if theValue doesn't match to a group
//  (so its objectClass isn't a group)
//
+ (id) groupWithValue: (NSString *) theValue
    andSourceSelector: (SEL) theSelector
             inDomain: (NSString *) domain
{
  NSArray *allSources;
  NGLdapEntry *entry;
  NSObject <SOGoSource> *source;
  id o;
  
  int i;

  // Don't bother looking in all sources if the
  // supplied value is nil.
  if (!theValue)
    return nil;

  allSources = [[SOGoUserManager sharedUserManager]
                 sourceIDsInDomain: domain];
  entry = nil;
  o = nil;

  for (i = 0; i < [allSources count]; i++)
    {
      source = [[SOGoUserManager sharedUserManager] sourceWithID: [allSources objectAtIndex: i]];

      // Our different sources might not all implements groups support
      if ([source respondsToSelector: theSelector])
	entry = [source performSelector: theSelector
			withObject: theValue];
      
      if (entry)
	break;

      entry = nil;
    }
  
  if (entry)
    {
      NSArray *classes;

      // We check to see if it's a group
      classes = [[entry asDictionary] objectForKey: @"objectclass"];

      if (classes)
	{
	  int i, c;
	  
	  classes = [NSMutableArray arrayWithArray: classes];
	  c = [classes count];
	  for (i = 0; i < c; i++)
	    [(id)classes replaceObjectAtIndex: i
				   withObject: [[classes objectAtIndex: i] lowercaseString]];
	}

      // Found a group, let's return it.
      if ([classes containsObject: @"group"] ||
	  [classes containsObject: @"groupofnames"] ||
	  [classes containsObject: @"groupofuniquenames"] ||
	  [classes containsObject: @"posixgroup"])
	{
	  o = [[self alloc] initWithIdentifier: theValue
			                domain: domain
                                        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, *logins;
  NSString *dn, *login;
  SOGoUserManager *um;
  NSDictionary *d;
  SOGoUser *user;
  NSArray *o;
  int i, c;

  if (!_members)
    {
      _members = [NSMutableArray new];
      uids = [NSMutableArray array];
      dns = [NSMutableArray array];
      logins = [NSMutableArray array];

      // We check if it's a static group  
      // Fetch "members" - we get DNs
      d = [_entry asDictionary];
      o = [d objectForKey: @"member"];
      if (o) [dns addObjectsFromArray: o];

      // Fetch "uniqueMembers" - we get DNs
      o = [d objectForKey: @"uniquemember"];
      if (o) [dns addObjectsFromArray: o];
  
      // Fetch "memberUid" - we get UID (like login names)
      o = [d objectForKey: @"memberuid"];
      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];

          // We add members for whom we have their associated DN
          for (i = 0; i < [dns count]; i++)
            {
              dn = [dns objectAtIndex: i];
              login = [um getLoginForDN: [dn lowercaseString]];
              user = [SOGoUser userWithLogin: login  roles: nil];
              if (user)
		{
		  [logins addObject: login];
		  [_members addObject: user];
		}
            }

          // We add members for whom we have their associated login name
          for (i = 0; i < [uids count]; i++)
            {
              login = [uids objectAtIndex: i];
              user = [SOGoUser userWithLogin: login  roles: nil];
              
              if (user)
		{
		  [logins addObject: login];
		  [_members addObject: user];
		}
            }


	  // 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: @"%@+%@", _identifier, _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;
}

//
//
//
- (BOOL) hasMemberWithUID: (NSString *) memberUID
{

  BOOL rc;

  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)
    {
      NSString *currentUID;
	
      int count, max;
      max = [_members count];
      for (count = 0; !rc && count < max; count++)
	{
	  currentUID = [[_members objectAtIndex: count] login];
	  rc = [memberUID isEqualToString: currentUID];
	}

    }
  else
    {
      NSString *key, *value;;
      NSArray *a;

      key = [NSString stringWithFormat: @"%@+%@", _identifier, _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 members];
	  value = [[SOGoCache sharedCache] valueForKey: key];
	}

      a = [value componentsSeparatedByString: @","];
      rc = [a containsObject: memberUID];
    }

  return rc;
}

@end