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 "SOGoDateFormatter.h"
33 #import "SOGoObject.h"
34 #import "SOGoPermissions.h"
35 #import "NSArray+Utilities.h"
39 static NSTimeZone *serverTimeZone = nil;
40 static NSString *fallbackIMAP4Server = nil;
41 static NSString *defaultLanguage = nil;
42 static NSURL *AgenorProfileURL = nil;
44 NSString *SOGoWeekStartHideWeekNumbers = @"HideWeekNumbers";
45 NSString *SOGoWeekStartJanuary1 = @"January1";
46 NSString *SOGoWeekStartFirst4DayWeek = @"First4DayWeek";
47 NSString *SOGoWeekStartFirstFullWeek = @"FirstFullWeek";
49 @interface NSObject (SOGoRoles)
51 - (NSArray *) rolesOfUser: (NSString *) uid;
55 @implementation SOGoUser
63 ud = [NSUserDefaults standardUserDefaults];
66 tzName = [ud stringForKey: @"SOGoServerTimeZone"];
69 serverTimeZone = [NSTimeZone timeZoneWithName: tzName];
70 [serverTimeZone retain];
72 if (!AgenorProfileURL)
74 profileURL = [ud stringForKey: @"AgenorProfileURL"];
75 AgenorProfileURL = [[NSURL alloc] initWithString: profileURL];
77 if (!fallbackIMAP4Server)
78 ASSIGN (fallbackIMAP4Server,
79 [ud stringForKey: @"SOGoFallbackIMAP4Server"]);
82 ASSIGN (defaultLanguage, [ud stringForKey: @"SOGoDefaultLanguage"]);
84 ASSIGN (defaultLanguage, @"English");
88 + (SOGoUser *) userWithLogin: (NSString *) newLogin
89 roles: (NSArray *) newRoles
93 user = [[self alloc] initWithLogin: newLogin roles: newRoles];
101 if ((self = [super init]))
107 currentPassword = nil;
114 - (id) initWithLogin: (NSString *) newLogin
115 roles: (NSArray *) newRoles
120 if ([newLogin isEqualToString: @"anonymous"]
121 || [newLogin isEqualToString: @"freebusy"])
122 self = [super initWithLogin: newLogin roles: newRoles];
125 um = [LDAPUserManager sharedUserManager];
126 user = [um contactInfosForUserWithUIDorEmail: newLogin];
127 self = [super initWithLogin: [user objectForKey: @"c_uid"]
136 [currentPassword release];
137 [userDefaults release];
138 [userSettings release];
141 [dateFormatter release];
145 - (void) setCurrentPassword: (NSString *) newPassword
147 ASSIGN (currentPassword, newPassword);
150 - (NSString *) currentPassword
152 return currentPassword;
155 - (id) _fetchFieldForUser: (NSString *) field
157 NSDictionary *contactInfos;
160 um = [LDAPUserManager sharedUserManager];
161 contactInfos = [um contactInfosForUserWithUIDorEmail: login];
163 return [contactInfos objectForKey: field];
166 - (void) _fetchAllEmails
168 allEmails = [self _fetchFieldForUser: @"emails"];
174 cn = [self _fetchFieldForUser: @"cn"];
180 - (NSString *) fullEmail
182 return [[LDAPUserManager sharedUserManager] getFullEmailForUID: login];
185 - (NSString *) primaryEmail
188 [self _fetchAllEmails];
190 return [allEmails objectAtIndex: 0];
193 - (NSString *) systemEmail
196 [self _fetchAllEmails];
198 return [allEmails lastObject];
201 - (NSArray *) allEmails
204 [self _fetchAllEmails];
209 - (BOOL) hasEmail: (NSString *) email
212 [self _fetchAllEmails];
214 return [allEmails containsCaseInsensitiveString: email];
225 - (NSString *) primaryIMAP4AccountString
227 return [NSString stringWithFormat: @"%@@%@", login, fallbackIMAP4Server];
230 // - (NSString *) primaryMailServer
232 // return [[self userManager] getServerForUID: [self login]];
235 // - (NSArray *) additionalIMAP4AccountStrings
237 // return [[self userManager]getSharedMailboxAccountStringsForUID: [self login]];
240 // - (NSArray *) additionalEMailAddresses
242 // return [[self userManager] getSharedMailboxEMailsForUID: [self login]];
245 // - (NSDictionary *) additionalIMAP4AccountsAndEMails
247 // return [[self userManager] getSharedMailboxesAndEMailsForUID: [self login]];
250 - (NSURL *) freeBusyURL
255 - (SOGoDateFormatter *) dateFormatterInContext: (WOContext *) context
262 dateFormatter = [SOGoDateFormatter new];
263 [dateFormatter setLocale: [context valueForKey: @"locale"]];
264 ud = [self userDefaults];
265 format = [ud stringForKey: @"ShortDateFormat"];
267 [dateFormatter setShortDateFormat: format];
268 format = [ud stringForKey: @"LongDateFormat"];
270 [dateFormatter setLongDateFormat: format];
271 format = [ud stringForKey: @"TimeFormat"];
273 [dateFormatter setTimeFormat: format];
276 return dateFormatter;
281 - (NSUserDefaults *) userDefaults
284 userDefaults = [[AgenorUserDefaults alloc] initWithTableURL: AgenorProfileURL
286 fieldName: @"defaults"];
291 - (NSUserDefaults *) userSettings
294 userSettings = [[AgenorUserDefaults alloc] initWithTableURL: AgenorProfileURL
296 fieldName: @"settings"];
301 - (NSString *) language
305 language = [[self userDefaults] stringForKey: @"Language"];
307 language = defaultLanguage;
314 - (NSTimeZone *) timeZone
316 NSString *timeZoneName;
320 timeZoneName = [[self userDefaults] stringForKey: @"TimeZone"];
321 if ([timeZoneName length] > 0)
322 userTimeZone = [NSTimeZone timeZoneWithName: timeZoneName];
324 userTimeZone = serverTimeZone;
325 [userTimeZone retain];
331 - (NSTimeZone *) serverTimeZone
333 return serverTimeZone;
338 // TODO: those methods should check whether the traversal stack in the context
339 // already contains proper folders to improve caching behaviour
341 - (id) homeFolderInContext: (id) _ctx
343 /* Note: watch out for cyclic references */
344 // TODO: maybe we should add an [activeUser reset] method to SOPE
347 folder = [(WOContext *)_ctx objectForKey:@"ActiveUserHomeFolder"];
349 return [folder isNotNull] ? folder : nil;
351 folder = [[WOApplication application] lookupName:[self login]
352 inContext:_ctx acquire:NO];
353 if ([folder isKindOfClass:[NSException class]])
356 [(WOContext *)_ctx setObject: ((folder)
359 forKey: @"ActiveUserHomeFolder"];
363 // - (id) schedulingCalendarInContext: (id) _ctx
365 // /* Note: watch out for cyclic references */
368 // folder = [(WOContext *)_ctx objectForKey:@"ActiveUserCalendar"];
369 // if (folder != nil)
370 // return [folder isNotNull] ? folder : nil;
372 // folder = [self homeFolderInContext:_ctx];
373 // if ([folder isKindOfClass:[NSException class]])
376 // folder = [folder lookupName:@"Calendar" inContext:_ctx acquire:NO];
377 // if ([folder isKindOfClass:[NSException class]])
380 // [(WOContext *)_ctx setObject:folder ? folder : [NSNull null]
381 // forKey:@"ActiveUserCalendar"];
385 - (NSArray *) rolesForObject: (NSObject *) object
386 inContext: (WOContext *) context
388 NSMutableArray *rolesForObject;
391 rolesForObject = [NSMutableArray new];
392 [rolesForObject autorelease];
394 sogoRoles = [super rolesForObject: object inContext: context];
396 [rolesForObject addObjectsFromArray: sogoRoles];
398 if ([[object ownerInContext: context] isEqualToString: [self login]])
399 [rolesForObject addObject: SoRole_Owner];
400 if ([object isKindOfClass: [SOGoObject class]])
402 sogoRoles = [(SOGoObject *) object aclsForUser: login];
404 [rolesForObject addObjectsFromArray: sogoRoles];
407 return rolesForObject;