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"
33 #import "SOGoPermissions.h"
34 #import "NSArray+Utilities.h"
38 static NSTimeZone *serverTimeZone = nil;
39 static NSString *fallbackIMAP4Server = nil;
40 static NSString *defaultLanguage = nil;
41 static NSURL *AgenorProfileURL = nil;
43 @interface NSObject (SOGoRoles)
45 - (NSArray *) rolesOfUser: (NSString *) uid;
49 @implementation SOGoUser
57 ud = [NSUserDefaults standardUserDefaults];
60 tzName = [ud stringForKey: @"SOGoServerTimeZone"];
63 serverTimeZone = [NSTimeZone timeZoneWithName: tzName];
64 [serverTimeZone retain];
66 if (!AgenorProfileURL)
68 profileURL = [ud stringForKey: @"AgenorProfileURL"];
69 AgenorProfileURL = [[NSURL alloc] initWithString: profileURL];
71 if (!fallbackIMAP4Server)
72 ASSIGN (fallbackIMAP4Server,
73 [ud stringForKey: @"SOGoFallbackIMAP4Server"]);
76 ASSIGN (defaultLanguage, [ud stringForKey: @"SOGoDefaultLanguage"]);
78 ASSIGN (defaultLanguage, @"English");
82 + (SOGoUser *) userWithLogin: (NSString *) newLogin
83 roles: (NSArray *) newRoles
87 user = [[self alloc] initWithLogin: newLogin roles: newRoles];
95 if ((self = [super init]))
106 - (id) initWithLogin: (NSString *) newLogin
107 roles: (NSArray *) newRoles
112 if ([newLogin isEqualToString: @"anonymous"]
113 || [newLogin isEqualToString: @"freebusy"])
114 self = [super initWithLogin: newLogin roles: newRoles];
117 um = [LDAPUserManager sharedUserManager];
118 user = [um contactInfosForUserWithUIDorEmail: newLogin];
119 self = [super initWithLogin: [user objectForKey: @"c_uid"]
128 [userDefaults release];
129 [userSettings release];
135 - (id) _fetchFieldForUser: (NSString *) field
137 NSDictionary *contactInfos;
140 um = [LDAPUserManager sharedUserManager];
141 contactInfos = [um contactInfosForUserWithUIDorEmail: login];
143 return [contactInfos objectForKey: field];
146 - (void) _fetchAllEmails
148 allEmails = [self _fetchFieldForUser: @"emails"];
154 cn = [self _fetchFieldForUser: @"cn"];
160 - (NSString *) fullEmail
162 return [[LDAPUserManager sharedUserManager] getFullEmailForUID: login];
165 - (NSString *) primaryEmail
168 [self _fetchAllEmails];
170 return [allEmails objectAtIndex: 0];
173 - (NSString *) systemEmail
176 [self _fetchAllEmails];
178 return [allEmails lastObject];
181 - (NSArray *) allEmails
184 [self _fetchAllEmails];
189 - (BOOL) hasEmail: (NSString *) email
192 [self _fetchAllEmails];
194 return [allEmails containsCaseInsensitiveString: email];
205 - (NSString *) primaryIMAP4AccountString
207 return [NSString stringWithFormat: @"%@@%@", login, fallbackIMAP4Server];
210 // - (NSString *) primaryMailServer
212 // return [[self userManager] getServerForUID: [self login]];
215 // - (NSArray *) additionalIMAP4AccountStrings
217 // return [[self userManager]getSharedMailboxAccountStringsForUID: [self login]];
220 // - (NSArray *) additionalEMailAddresses
222 // return [[self userManager] getSharedMailboxEMailsForUID: [self login]];
225 // - (NSDictionary *) additionalIMAP4AccountsAndEMails
227 // return [[self userManager] getSharedMailboxesAndEMailsForUID: [self login]];
230 - (NSURL *) freeBusyURL
237 - (NSUserDefaults *) userDefaults
240 userDefaults = [[AgenorUserDefaults alloc] initWithTableURL: AgenorProfileURL
242 fieldName: @"defaults"];
247 - (NSUserDefaults *) userSettings
250 userSettings = [[AgenorUserDefaults alloc] initWithTableURL: AgenorProfileURL
252 fieldName: @"settings"];
257 - (NSString *) language
261 language = [[self userDefaults] stringForKey: @"Language"];
263 language = defaultLanguage;
270 - (NSTimeZone *) timeZone
272 NSString *timeZoneName;
276 timeZoneName = [[self userDefaults] stringForKey: @"TimeZone"];
277 if ([timeZoneName length] > 0)
278 userTimeZone = [NSTimeZone timeZoneWithName: timeZoneName];
280 userTimeZone = serverTimeZone;
281 [userTimeZone retain];
287 - (NSTimeZone *) serverTimeZone
289 return serverTimeZone;
294 // TODO: those methods should check whether the traversal stack in the context
295 // already contains proper folders to improve caching behaviour
297 - (id) homeFolderInContext: (id) _ctx
299 /* Note: watch out for cyclic references */
300 // TODO: maybe we should add an [activeUser reset] method to SOPE
303 folder = [(WOContext *)_ctx objectForKey:@"ActiveUserHomeFolder"];
305 return [folder isNotNull] ? folder : nil;
307 folder = [[WOApplication application] lookupName:[self login]
308 inContext:_ctx acquire:NO];
309 if ([folder isKindOfClass:[NSException class]])
312 [(WOContext *)_ctx setObject:folder ? folder : [NSNull null]
313 forKey: @"ActiveUserHomeFolder"];
317 // - (id) schedulingCalendarInContext: (id) _ctx
319 // /* Note: watch out for cyclic references */
322 // folder = [(WOContext *)_ctx objectForKey:@"ActiveUserCalendar"];
323 // if (folder != nil)
324 // return [folder isNotNull] ? folder : nil;
326 // folder = [self homeFolderInContext:_ctx];
327 // if ([folder isKindOfClass:[NSException class]])
330 // folder = [folder lookupName:@"Calendar" inContext:_ctx acquire:NO];
331 // if ([folder isKindOfClass:[NSException class]])
334 // [(WOContext *)_ctx setObject:folder ? folder : [NSNull null]
335 // forKey:@"ActiveUserCalendar"];
339 - (NSArray *) rolesForObject: (NSObject *) object
340 inContext: (WOContext *) context
342 NSMutableArray *rolesForObject;
345 rolesForObject = [NSMutableArray new];
346 [rolesForObject autorelease];
348 sogoRoles = [super rolesForObject: object inContext: context];
350 [rolesForObject addObjectsFromArray: sogoRoles];
352 if ([[object ownerInContext: context] isEqualToString: [self login]])
353 [rolesForObject addObject: SoRole_Owner];
354 if ([object isKindOfClass: [SOGoObject class]])
356 sogoRoles = [(SOGoObject *) object aclsForUser: login];
358 [rolesForObject addObjectsFromArray: sogoRoles];
361 return rolesForObject;