2 Copyright (C) 2005 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #import <Foundation/NSArray.h>
23 #import <Foundation/NSNull.h>
24 #import <Foundation/NSTimeZone.h>
25 #import <Foundation/NSUserDefaults.h>
26 #import <NGObjWeb/WOApplication.h>
27 #import <NGObjWeb/SoObject.h>
28 #import <NGExtensions/NSNull+misc.h>
30 #import "AgenorUserDefaults.h"
31 #import "LDAPUserManager.h"
32 #import "SOGoContentObject.h"
34 #import "SOGoPermissions.h"
36 static NSTimeZone *serverTimeZone = nil;
37 static NSString *fallbackIMAP4Server = nil;
38 static NSURL *AgenorProfileURL = nil;
40 @interface NSObject (SOGoRoles)
42 - (NSArray *) rolesOfUser: (NSString *) uid;
46 @implementation SOGoUser
54 ud = [NSUserDefaults standardUserDefaults];
57 tzName = [ud stringForKey: @"SOGoServerTimeZone"];
59 tzName = @"Canada/Eastern";
60 serverTimeZone = [NSTimeZone timeZoneWithName: tzName];
61 [serverTimeZone retain];
63 if (!AgenorProfileURL)
65 profileURL = [ud stringForKey: @"AgenorProfileURL"];
66 AgenorProfileURL = [[NSURL alloc] initWithString: profileURL];
68 if (!fallbackIMAP4Server)
69 ASSIGN (fallbackIMAP4Server, [ud stringForKey: @"SOGoFallbackIMAP4Server"]);
72 + (SOGoUser *) userWithLogin: (NSString *) newLogin
73 roles: (NSArray *) newRoles
77 user = [[self alloc] initWithLogin: newLogin roles: newRoles];
85 if ((self = [super init]))
95 - (id) initWithLogin: (NSString *) newLogin
96 roles: (NSArray *) newRoles
101 if ([newLogin isEqualToString: @"anonymous"]
102 || [newLogin isEqualToString: @"freebusy"])
103 self = [super initWithLogin: newLogin roles: newRoles];
106 um = [LDAPUserManager sharedUserManager];
107 user = [um contactInfosForUserWithUIDorEmail: newLogin];
108 self = [super initWithLogin: [user objectForKey: @"c_uid"]
117 [userDefaults release];
118 [userSettings release];
122 - (id) _fetchFieldForUser: (NSString *) field
124 NSDictionary *contactInfos;
127 um = [LDAPUserManager sharedUserManager];
128 contactInfos = [um contactInfosForUserWithUIDorEmail: login];
130 return [contactInfos objectForKey: field];
133 - (void) _fetchAllEmails
135 allEmails = [self _fetchFieldForUser: @"emails"];
141 cn = [self _fetchFieldForUser: @"cn"];
147 - (NSString *) primaryEmail
150 [self _fetchAllEmails];
152 return [allEmails objectAtIndex: 0];
155 - (NSString *) systemEmail
158 [self _fetchAllEmails];
160 return [allEmails lastObject];
163 - (NSArray *) allEmails
166 [self _fetchAllEmails];
171 - (BOOL) hasEmail: (NSString *) email
174 NSString *currentEmail, *cmpEmail;
175 NSEnumerator *emails;
179 [self _fetchAllEmails];
180 cmpEmail = [email lowercaseString];
181 emails = [allEmails objectEnumerator];
182 currentEmail = [emails nextObject];
183 while (currentEmail && !hasEmail)
184 if ([[currentEmail lowercaseString] isEqualToString: cmpEmail])
187 currentEmail = [emails nextObject];
200 - (NSString *) primaryIMAP4AccountString
202 return [NSString stringWithFormat: @"%@@%@", login, fallbackIMAP4Server];
205 // - (NSString *) primaryMailServer
207 // return [[self userManager] getServerForUID: [self login]];
210 // - (NSArray *) additionalIMAP4AccountStrings
212 // return [[self userManager]getSharedMailboxAccountStringsForUID: [self login]];
215 // - (NSArray *) additionalEMailAddresses
217 // return [[self userManager] getSharedMailboxEMailsForUID: [self login]];
220 // - (NSDictionary *) additionalIMAP4AccountsAndEMails
222 // return [[self userManager] getSharedMailboxesAndEMailsForUID: [self login]];
225 - (NSURL *) freeBusyURL
232 - (NSUserDefaults *) userDefaults
235 userDefaults = [[AgenorUserDefaults alloc] initWithTableURL: AgenorProfileURL
237 fieldName: @"defaults"];
242 - (NSUserDefaults *) userSettings
245 userSettings = [[AgenorUserDefaults alloc] initWithTableURL: AgenorProfileURL
247 fieldName: @"settings"];
252 - (NSTimeZone *) timeZone
254 NSString *timeZoneName;
258 timeZoneName = [[self userDefaults] stringForKey: @"TimeZone"];
259 if ([timeZoneName length] > 0)
260 userTimeZone = [NSTimeZone timeZoneWithName: timeZoneName];
262 userTimeZone = serverTimeZone;
263 [userTimeZone retain];
269 - (NSTimeZone *) serverTimeZone
271 return serverTimeZone;
276 // TODO: those methods should check whether the traversal stack in the context
277 // already contains proper folders to improve caching behaviour
279 - (id) homeFolderInContext: (id) _ctx
281 /* Note: watch out for cyclic references */
282 // TODO: maybe we should add an [activeUser reset] method to SOPE
285 folder = [(WOContext *)_ctx objectForKey:@"ActiveUserHomeFolder"];
287 return [folder isNotNull] ? folder : nil;
289 folder = [[WOApplication application] lookupName:[self login]
290 inContext:_ctx acquire:NO];
291 if ([folder isKindOfClass:[NSException class]])
294 [(WOContext *)_ctx setObject:folder ? folder : [NSNull null]
295 forKey: @"ActiveUserHomeFolder"];
299 // - (id) schedulingCalendarInContext: (id) _ctx
301 // /* Note: watch out for cyclic references */
304 // folder = [(WOContext *)_ctx objectForKey:@"ActiveUserCalendar"];
305 // if (folder != nil)
306 // return [folder isNotNull] ? folder : nil;
308 // folder = [self homeFolderInContext:_ctx];
309 // if ([folder isKindOfClass:[NSException class]])
312 // folder = [folder lookupName:@"Calendar" inContext:_ctx acquire:NO];
313 // if ([folder isKindOfClass:[NSException class]])
316 // [(WOContext *)_ctx setObject:folder ? folder : [NSNull null]
317 // forKey:@"ActiveUserCalendar"];
321 - (NSArray *) rolesForObject: (NSObject *) object
322 inContext: (WOContext *) context
324 NSMutableArray *rolesForObject;
327 rolesForObject = [NSMutableArray new];
328 [rolesForObject autorelease];
330 sogoRoles = [super rolesForObject: object inContext: context];
332 [rolesForObject addObjectsFromArray: sogoRoles];
334 if ([[object ownerInContext: context] isEqualToString: [self login]])
335 [rolesForObject addObject: SoRole_Owner];
336 if ([object isKindOfClass: [SOGoObject class]])
338 sogoRoles = [(SOGoObject *) object aclsForUser: login];
340 [rolesForObject addObjectsFromArray: sogoRoles];
343 return rolesForObject;