]> err.no Git - scalable-opengroupware.org/blob - SoObjects/Mailer/SOGoMailAccounts.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1050 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / SoObjects / Mailer / SOGoMailAccounts.m
1 /*
2   Copyright (C) 2004-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 #include "SOGoMailAccounts.h"
23 #include "SOGoUser+Mail.h"
24 #include "common.h"
25 #include <NGObjWeb/SoObject+SoDAV.h>
26 #include <SOGo/WOContext+Agenor.h>
27
28 @implementation SOGoMailAccounts
29
30 static NSString *AgenorShareLoginMarker  = @".-.";
31
32 /* detect webmail being accessed from the outside */
33
34 - (BOOL)isInternetRequest {
35   return ([context isAccessFromIntranet] ? NO : YES);
36 }
37
38 /* listing the available mailboxes */
39
40 - (BOOL) isInHomeFolderBranchOfLoggedInAccount: (NSString *) userLogin
41 {
42   return [[[self container] nameInContainer] isEqualToString: userLogin];
43 }
44
45 - (NSArray *)toManyRelationshipKeys {
46   SOGoUser *user;
47   id        account;
48   NSArray   *shares;
49   NSString *userLogin;
50   
51   /*
52     Note: this is not strictly correct. The accounts being retrieved should be
53           the accounts based on the container object of this folder. Given
54           sufficient rights (eg delegation rights!), this would allow you to
55           browse the hierarchies of other users.
56           
57           But then, the home-folder would need to know about mail
58           functionality which isn't perfect either.
59           => TODO
60   */
61   user = [context activeUser];
62   userLogin = [user login];
63   
64   /* for now: return nothing if the home-folder does not belong to the login */
65   if (![self isInHomeFolderBranchOfLoggedInAccount: userLogin]) {
66     [self warnWithFormat:@"User %@ tried to access mail hierarchy of %@",
67           [user login], [[self container] nameInContainer]];
68     return nil;
69   }
70   
71   account = [user primaryIMAP4AccountString];
72   if ([account isNotNull]) account = [NSArray arrayWithObject:account];
73   
74   if ([self isInternetRequest]) /* only show primary mailbox in Internet */
75     return account;
76   
77   shares  = [user valueForKey:@"additionalIMAP4AccountStrings"];
78   return ([shares count] == 0)
79     ? account
80     : [account arrayByAddingObjectsFromArray:shares];
81 }
82
83 - (NSArray *)fetchIdentitiesWithOnlyEmitterAccess:(BOOL)_flag {
84   WOContext *ctx;
85   
86   if ([self isInternetRequest]) { /* only show primary mailbox in Internet */
87     // just return the primary identity
88     id identity;
89     
90     identity = [[context activeUser] primaryMailIdentity];
91     return [identity isNotNull] ? [NSArray arrayWithObject:identity] : nil;
92   }
93   
94   return [[ctx activeUser] fetchAllMailIdentitiesWithOnlyEmitterAccess:_flag];
95 }
96
97 - (NSArray *)fetchAllIdentities {
98   return [self fetchIdentitiesWithOnlyEmitterAccess:NO];
99 }
100
101 - (NSArray *)fetchIdentitiesWithEmitterPermissions {
102   return [self fetchIdentitiesWithOnlyEmitterAccess:YES];
103 }
104
105 /* name lookup */
106
107 - (BOOL)isValidMailAccountName:(NSString *)_key {
108   if ([_key length] == 0)
109     return NO;
110   
111   return YES;
112 }
113
114 - (id)mailAccountWithName:(NSString *)_key inContext:(id)_ctx {
115   static Class ctClass = Nil;
116   id ct;
117   
118   if (ctClass == Nil)
119     ctClass = NSClassFromString(@"SOGoMailAccount");
120   if (ctClass == Nil) {
121     [self errorWithFormat:@"missing SOGoMailAccount class!"];
122     return nil;
123   }
124   
125   ct = [[ctClass alloc] initWithName:_key inContainer:self];
126   return [ct autorelease];
127 }
128
129 - (id)sharedMailAccountWithName:(NSString *)_key inContext:(id)_ctx {
130   static Class ctClass = Nil;
131   id ct;
132   
133   if (ctClass == Nil)
134     ctClass = NSClassFromString(@"SOGoSharedMailAccount");
135   if (ctClass == Nil) {
136     [self errorWithFormat:@"missing SOGoSharedMailAccount class!"];
137     return nil;
138   }
139   
140   ct = [[ctClass alloc] initWithName:_key inContainer:self];
141   return [ct autorelease];
142 }
143
144 - (id)lookupName:(NSString *)_key inContext:(id)_ctx acquire:(BOOL)_flag
145 {
146   id obj;
147   NSString *userLogin;
148
149   userLogin = [[context activeUser] login];
150   
151   /* first check attributes directly bound to the application */
152   if ((obj = [super lookupName:_key inContext:_ctx acquire:NO]))
153     return obj;
154   
155   if (![self isInHomeFolderBranchOfLoggedInAccount: userLogin]) {
156     [self warnWithFormat:@"User %@ tried to access mail hierarchy of %@",
157           userLogin, [[self container] nameInContainer]];
158     
159     return [NSException exceptionWithHTTPStatus:403 /* Forbidden */
160                         reason:@"Tried to access the mail of another user"];
161   }
162   
163   if ([self isValidMailAccountName:_key]) {
164     /* forbid shares for requests coming from the Internet */
165     BOOL isSharedKey;
166     
167     isSharedKey = [_key rangeOfString:AgenorShareLoginMarker].length > 0;
168     
169     if ([self isInternetRequest]) {
170       if (isSharedKey) {
171         return [NSException exceptionWithHTTPStatus:403 /* Forbidden */
172                             reason:
173                               @"Access to shares forbidden from the Internet"];
174       }
175     }
176     
177     return isSharedKey
178       ? [self sharedMailAccountWithName:_key inContext:_ctx]
179       : [self mailAccountWithName:_key inContext:_ctx];
180   }
181
182   /* return 404 to stop acquisition */
183   return [NSException exceptionWithHTTPStatus:404 /* Not Found */];
184 }
185
186 /* WebDAV */
187
188 - (BOOL)davIsCollection {
189   return YES;
190 }
191
192 /* acls */
193 - (NSArray *) aclsForUser: (NSString *) uid
194 {
195   return nil;
196 }
197
198
199 @end /* SOGoMailAccounts */