]> err.no Git - scalable-opengroupware.org/blob - SoObjects/SOGo/SOGoUser.m
d60b5dc2c2ab0a77f1e7fac101795e5929938c41
[scalable-opengroupware.org] / SoObjects / SOGo / SOGoUser.m
1 /*
2   Copyright (C) 2005 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21
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>
29
30 #import "AgenorUserDefaults.h"
31 #import "LDAPUserManager.h"
32 #import "SOGoContentObject.h"
33 #import "SOGoPermissions.h"
34 #import "NSArray+Utilities.h"
35
36 #import "SOGoUser.h"
37
38 static NSTimeZone *serverTimeZone = nil;
39 static NSString *fallbackIMAP4Server = nil;
40 static NSString *defaultLanguage = nil;
41 static NSURL *AgenorProfileURL = nil;
42
43 @interface NSObject (SOGoRoles)
44
45 - (NSArray *) rolesOfUser: (NSString *) uid;
46
47 @end
48
49 @implementation SOGoUser
50
51 + (void) initialize
52 {
53   NSString *tzName;
54   NSUserDefaults *ud;
55   NSString *profileURL;
56
57   ud = [NSUserDefaults standardUserDefaults];
58   if (!serverTimeZone)
59     {
60       tzName = [ud stringForKey: @"SOGoServerTimeZone"];
61       if (!tzName)
62         tzName = @"UTC";
63       serverTimeZone = [NSTimeZone timeZoneWithName: tzName];
64       [serverTimeZone retain];
65     }
66   if (!AgenorProfileURL)
67     {
68       profileURL = [ud stringForKey: @"AgenorProfileURL"];
69       AgenorProfileURL = [[NSURL alloc] initWithString: profileURL];
70     }
71   if (!fallbackIMAP4Server)
72     ASSIGN (fallbackIMAP4Server,
73             [ud stringForKey: @"SOGoFallbackIMAP4Server"]);
74   if (!defaultLanguage)
75     {
76       ASSIGN (defaultLanguage, [ud stringForKey: @"SOGoDefaultLanguage"]);
77       if (!defaultLanguage)
78         ASSIGN (defaultLanguage, @"English");
79     }
80 }
81
82 + (SOGoUser *) userWithLogin: (NSString *) newLogin
83                        roles: (NSArray *) newRoles
84 {
85   SOGoUser *user;
86
87   user = [[self alloc] initWithLogin: newLogin roles: newRoles];
88   [user autorelease];
89
90   return user;
91 }
92
93 - (id) init
94 {
95   if ((self = [super init]))
96     {
97       userDefaults = nil;
98       userSettings = nil;
99       allEmails = nil;
100       language = nil;
101     }
102
103   return self;
104 }
105
106 - (id) initWithLogin: (NSString *) newLogin
107                roles: (NSArray *) newRoles
108 {
109   LDAPUserManager *um;
110   NSDictionary *user;
111
112   if ([newLogin isEqualToString: @"anonymous"]
113       || [newLogin isEqualToString: @"freebusy"])
114     self = [super initWithLogin: newLogin roles: newRoles];
115   else
116     {
117       um = [LDAPUserManager sharedUserManager];
118       user = [um contactInfosForUserWithUIDorEmail: newLogin];
119       self = [super initWithLogin: [user objectForKey: @"c_uid"]
120                     roles: newRoles];
121     }
122
123   return self;
124 }
125
126 - (void) dealloc
127 {
128   [userDefaults release];
129   [userSettings release];
130   [allEmails release];
131   [language release];
132   [super dealloc];
133 }
134
135 - (id) _fetchFieldForUser: (NSString *) field
136 {
137   NSDictionary *contactInfos;
138   LDAPUserManager *um;
139
140   um = [LDAPUserManager sharedUserManager];
141   contactInfos = [um contactInfosForUserWithUIDorEmail: login];
142
143   return [contactInfos objectForKey: field];
144 }
145
146 - (void) _fetchAllEmails
147 {
148   allEmails = [self _fetchFieldForUser: @"emails"];
149   [allEmails retain];
150 }
151
152 - (void) _fetchCN
153 {
154   cn = [self _fetchFieldForUser: @"cn"];
155   [cn retain];
156 }
157
158 /* properties */
159
160 - (NSString *) fullEmail
161 {
162   return [[LDAPUserManager sharedUserManager] getFullEmailForUID: login];
163 }
164
165 - (NSString *) primaryEmail
166 {
167   if (!allEmails)
168     [self _fetchAllEmails];
169
170   return [allEmails objectAtIndex: 0];
171 }
172
173 - (NSString *) systemEmail
174 {
175   if (!allEmails)
176     [self _fetchAllEmails];
177
178   return [allEmails lastObject];
179 }
180
181 - (NSArray *) allEmails
182 {
183   if (!allEmails)
184     [self _fetchAllEmails];
185
186   return allEmails;  
187 }
188
189 - (BOOL) hasEmail: (NSString *) email
190 {
191   if (!allEmails)
192     [self _fetchAllEmails];
193   
194   return [allEmails containsCaseInsensitiveString: email];
195 }
196
197 - (NSString *) cn
198 {
199   if (!cn)
200     [self _fetchCN];
201
202   return cn;
203 }
204
205 - (NSString *) primaryIMAP4AccountString
206 {
207   return [NSString stringWithFormat: @"%@@%@", login, fallbackIMAP4Server];
208 }
209
210 // - (NSString *) primaryMailServer
211 // {
212 //   return [[self userManager] getServerForUID: [self login]];
213 // }
214
215 // - (NSArray *) additionalIMAP4AccountStrings
216 // {
217 //   return [[self userManager]getSharedMailboxAccountStringsForUID: [self login]];
218 // }
219
220 // - (NSArray *) additionalEMailAddresses
221 // {
222 //   return [[self userManager] getSharedMailboxEMailsForUID: [self login]];
223 // }
224
225 // - (NSDictionary *) additionalIMAP4AccountsAndEMails
226 // {
227 //   return [[self userManager] getSharedMailboxesAndEMailsForUID: [self login]];
228 // }
229
230 - (NSURL *) freeBusyURL
231 {
232   return nil;
233 }
234
235 /* defaults */
236
237 - (NSUserDefaults *) userDefaults
238 {
239   if (!userDefaults)
240     userDefaults = [[AgenorUserDefaults alloc] initWithTableURL: AgenorProfileURL
241                                                uid: login
242                                                fieldName: @"defaults"];
243
244   return userDefaults;
245 }
246
247 - (NSUserDefaults *) userSettings
248 {
249   if (!userSettings)
250     userSettings = [[AgenorUserDefaults alloc] initWithTableURL: AgenorProfileURL
251                                                uid: login
252                                                fieldName: @"settings"];
253
254   return userSettings;
255 }
256
257 - (NSString *) language
258 {
259   if (!language)
260     {
261       language = [[self userDefaults] stringForKey: @"Language"];
262       if (!language)
263         language = defaultLanguage;
264       [language retain];
265     }
266
267   return language;
268 }
269
270 - (NSTimeZone *) timeZone
271 {
272   NSString *timeZoneName;
273
274   if (!userTimeZone)
275     {
276       timeZoneName = [[self userDefaults] stringForKey: @"TimeZone"];
277       if ([timeZoneName length] > 0)
278         userTimeZone = [NSTimeZone timeZoneWithName: timeZoneName];
279       if (!userTimeZone)
280         userTimeZone = serverTimeZone;
281       [userTimeZone retain];
282     }
283
284   return userTimeZone;
285 }
286
287 - (NSTimeZone *) serverTimeZone
288 {
289   return serverTimeZone;
290 }
291
292 /* folders */
293
294 // TODO: those methods should check whether the traversal stack in the context
295 //       already contains proper folders to improve caching behaviour
296
297 - (id) homeFolderInContext: (id) _ctx
298 {
299   /* Note: watch out for cyclic references */
300   // TODO: maybe we should add an [activeUser reset] method to SOPE
301   id folder;
302   
303   folder = [(WOContext *)_ctx objectForKey:@"ActiveUserHomeFolder"];
304   if (folder != nil)
305     return [folder isNotNull] ? folder : nil;
306   
307   folder = [[WOApplication application] lookupName:[self login]
308                                         inContext:_ctx acquire:NO];
309   if ([folder isKindOfClass:[NSException class]])
310     return folder;
311   
312   [(WOContext *)_ctx setObject:folder ? folder : [NSNull null] 
313                 forKey: @"ActiveUserHomeFolder"];
314   return folder;
315 }
316
317 // - (id) schedulingCalendarInContext: (id) _ctx
318 // {
319 //   /* Note: watch out for cyclic references */
320 //   id folder;
321
322 //   folder = [(WOContext *)_ctx objectForKey:@"ActiveUserCalendar"];
323 //   if (folder != nil)
324 //     return [folder isNotNull] ? folder : nil;
325
326 //   folder = [self homeFolderInContext:_ctx];
327 //   if ([folder isKindOfClass:[NSException class]])
328 //     return folder;
329   
330 //   folder = [folder lookupName:@"Calendar" inContext:_ctx acquire:NO];
331 //   if ([folder isKindOfClass:[NSException class]])
332 //     return folder;
333   
334 //   [(WOContext *)_ctx setObject:folder ? folder : [NSNull null] 
335 //                 forKey:@"ActiveUserCalendar"];
336 //   return folder;
337 // }
338
339 - (NSArray *) rolesForObject: (NSObject *) object
340                    inContext: (WOContext *) context
341 {
342   NSMutableArray *rolesForObject;
343   NSArray *sogoRoles;
344
345   rolesForObject = [NSMutableArray new];
346   [rolesForObject autorelease];
347
348   sogoRoles = [super rolesForObject: object inContext: context];
349   if (sogoRoles)
350     [rolesForObject addObjectsFromArray: sogoRoles];
351
352   if ([[object ownerInContext: context] isEqualToString: [self login]])
353     [rolesForObject addObject: SoRole_Owner];
354   if ([object isKindOfClass: [SOGoObject class]])
355     {
356       sogoRoles = [(SOGoObject *) object aclsForUser: login];
357       if (sogoRoles)
358         [rolesForObject addObjectsFromArray: sogoRoles];
359     }
360
361   return rolesForObject;
362 }
363
364 @end /* SOGoUser */