]> err.no Git - scalable-opengroupware.org/blob - SoObjects/SOGo/SOGoUser.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1051 d1b88da0-ebda-0310...
[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 "AgenorUserManager.h"
31 #import "SOGoContentObject.h"
32 #import "SOGoUser.h"
33 #import "SOGoPermissions.h"
34
35 static NSTimeZone *serverTimeZone = nil;
36
37 @interface NSObject (SOGoRoles)
38
39 - (NSArray *) rolesOfUser: (NSString *) uid;
40
41 @end
42
43 @implementation SOGoUser
44
45 + (void) initialize
46 {
47   NSString *tzName;
48
49   if (!serverTimeZone)
50     {
51       tzName = [[NSUserDefaults standardUserDefaults]
52                  stringForKey: @"SOGoServerTimeZone"];
53       if (!tzName)
54         tzName = @"Canada/Eastern";
55       serverTimeZone = [NSTimeZone timeZoneWithName: tzName];
56       [serverTimeZone retain];
57     }
58 }
59
60 + (SOGoUser *) userWithLogin: (NSString *) login
61                     andRoles: (NSArray *) roles
62 {
63   SOGoUser *user;
64
65   user = [[self alloc] initWithLogin: login roles: roles];
66   [user autorelease];
67
68   return user;
69 }
70
71 - (id) init
72 {
73   if ((self = [super init]))
74     {
75       userDefaults = nil;
76       userSettings = nil;
77     }
78
79   return self;
80 }
81
82 - (void) dealloc
83 {
84   [userDefaults release];
85   [userSettings release];
86   [cn    release];
87   [email release];
88   [super dealloc];
89 }
90
91 /* internals */
92
93 - (AgenorUserManager *) userManager
94 {
95   static AgenorUserManager *um = nil;
96   if (um == nil) um = [[AgenorUserManager sharedUserManager] retain];
97
98   return um;
99 }
100
101 /* properties */
102
103 - (NSString *) email
104 {
105   if (email == nil)
106     {
107       email = [[self userManager] getEmailForUID: [self login]];
108       [email retain];
109     }
110
111   return email;
112 }
113
114 - (NSString *) cn
115 {
116   if (cn == nil)
117     {
118       cn = [[self userManager] getCNForUID: [self login]];
119       [cn retain];
120     }
121
122   return cn;
123 }
124
125 - (NSString *) primaryIMAP4AccountString
126 {
127   return [[self userManager] getIMAPAccountStringForUID: [self login]];
128 }
129
130 - (NSString *) primaryMailServer
131 {
132   return [[self userManager] getServerForUID: [self login]];
133 }
134
135 - (NSArray *) additionalIMAP4AccountStrings
136 {
137   return [[self userManager]getSharedMailboxAccountStringsForUID: [self login]];
138 }
139
140 - (NSArray *) additionalEMailAddresses
141 {
142   return [[self userManager] getSharedMailboxEMailsForUID: [self login]];
143 }
144
145 - (NSDictionary *) additionalIMAP4AccountsAndEMails
146 {
147   return [[self userManager] getSharedMailboxesAndEMailsForUID: [self login]];
148 }
149
150 - (NSURL *) freeBusyURL
151 {
152   return [[self userManager] getFreeBusyURLForUID: [self login]];
153 }
154
155 /* defaults */
156
157 - (NSUserDefaults *) userDefaults
158 {
159   if (!userDefaults)
160     {
161       userDefaults = [[self userManager] getUserDefaultsForUID: [self login]];
162       [userDefaults retain];
163     }
164
165   return userDefaults;
166 }
167
168 - (NSUserDefaults *) userSettings
169 {
170   if (!userSettings)
171     {
172       userSettings = [[self userManager] getUserSettingsForUID: [self login]];
173       [userSettings retain];
174     }
175
176   return userSettings;
177 }
178
179 - (NSTimeZone *) timeZone
180 {
181   NSString *timeZoneName;
182
183   if (!userTimeZone)
184     {
185       timeZoneName = [[self userDefaults] stringForKey: @"TimeZone"];
186       if ([timeZoneName length] > 0)
187         userTimeZone = [NSTimeZone timeZoneWithName: timeZoneName];
188       if (!userTimeZone)
189         userTimeZone = serverTimeZone;
190       [userTimeZone retain];
191     }
192
193   return userTimeZone;
194 }
195
196 - (NSTimeZone *) serverTimeZone
197 {
198   return serverTimeZone;
199 }
200
201 /* folders */
202
203 // TODO: those methods should check whether the traversal stack in the context
204 //       already contains proper folders to improve caching behaviour
205
206 - (id) homeFolderInContext: (id) _ctx
207 {
208   /* Note: watch out for cyclic references */
209   // TODO: maybe we should add an [activeUser reset] method to SOPE
210   id folder;
211   
212   folder = [(WOContext *)_ctx objectForKey:@"ActiveUserHomeFolder"];
213   if (folder != nil)
214     return [folder isNotNull] ? folder : nil;
215   
216   folder = [[WOApplication application] lookupName:[self login]
217                                         inContext:_ctx acquire:NO];
218   if ([folder isKindOfClass:[NSException class]])
219     return folder;
220   
221   [(WOContext *)_ctx setObject:folder ? folder : [NSNull null] 
222                 forKey: @"ActiveUserHomeFolder"];
223   return folder;
224 }
225
226 - (id) schedulingCalendarInContext: (id) _ctx
227 {
228   /* Note: watch out for cyclic references */
229   id folder;
230
231   folder = [(WOContext *)_ctx objectForKey:@"ActiveUserCalendar"];
232   if (folder != nil)
233     return [folder isNotNull] ? folder : nil;
234
235   folder = [self homeFolderInContext:_ctx];
236   if ([folder isKindOfClass:[NSException class]])
237     return folder;
238   
239   folder = [folder lookupName:@"Calendar" inContext:_ctx acquire:NO];
240   if ([folder isKindOfClass:[NSException class]])
241     return folder;
242   
243   [(WOContext *)_ctx setObject:folder ? folder : [NSNull null] 
244                 forKey:@"ActiveUserCalendar"];
245   return folder;
246 }
247
248 - (NSArray *) rolesForObject: (NSObject *) object
249                    inContext: (WOContext *) context
250 {
251   NSMutableArray *rolesForObject;
252   NSArray *sogoRoles;
253
254   rolesForObject = [NSMutableArray new];
255   [rolesForObject autorelease];
256
257   sogoRoles = [super rolesForObject: object inContext: context];
258   if (sogoRoles)
259     [rolesForObject addObjectsFromArray: sogoRoles];
260
261   if ([[object ownerInContext: context] isEqualToString: [self login]])
262     [rolesForObject addObject: SoRole_Owner];
263   if ([object isKindOfClass: [SOGoObject class]])
264     {
265       sogoRoles = [(SOGoObject *) object aclsForUser: login];
266       if (sogoRoles)
267         [rolesForObject addObjectsFromArray: sogoRoles];
268     }
269
270   return rolesForObject;
271 }
272
273 @end /* SOGoUser */