2 Copyright (C) 2004-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 #include "SOGoMailAccounts.h"
23 #include "SOGoUser+Mail.h"
25 #include <NGObjWeb/SoObject+SoDAV.h>
26 #include <SOGo/WOContext+Agenor.h>
28 @implementation SOGoMailAccounts
30 static NSString *AgenorShareLoginMarker = @".-.";
32 /* detect webmail being accessed from the outside */
34 - (BOOL)isInternetRequest {
35 return [[(WOApplication *)[WOApplication application] context]
36 isAccessFromIntranet] ? NO : YES;
39 /* listing the available mailboxes */
41 - (BOOL)isInHomeFolderBranchOfLoggedInAccount:(id)_ctx {
44 if (_ctx == nil) _ctx = [[WOApplication application] context];
46 [self errorWithFormat:@"Missing context!"];
50 user = [_ctx activeUser];
51 return [[[self container] nameInContainer] isEqualToString:[user login]];
54 - (NSArray *)toManyRelationshipKeys {
60 if ((ctx = [[WOApplication application] context]) == nil) {
61 [self logWithFormat:@"ERROR(%s): cannot procede without context!",
67 Note: this is not strictly correct. The accounts being retrieved should be
68 the accounts based on the container object of this folder. Given
69 sufficient rights (eg delegation rights!), this would allow you to
70 browse the hierarchies of other users.
72 But then, the home-folder would need to know about mail
73 functionality which isn't perfect either.
76 user = [ctx activeUser];
78 /* for now: return nothing if the home-folder does not belong to the login */
79 if (![self isInHomeFolderBranchOfLoggedInAccount:ctx]) {
80 [self warnWithFormat:@"User %@ tried to access mail hierarchy of %@",
81 [user login], [[self container] nameInContainer]];
85 account = [user valueForKey:@"primaryIMAP4AccountString"];
86 if ([account isNotNull]) account = [NSArray arrayWithObject:account];
88 if ([self isInternetRequest]) /* only show primary mailbox in Internet */
91 shares = [user valueForKey:@"additionalIMAP4AccountStrings"];
92 return ([shares count] == 0)
94 : [account arrayByAddingObjectsFromArray:shares];
97 - (NSArray *)fetchIdentitiesWithOnlyEmitterAccess:(BOOL)_flag {
100 if ((ctx = [[WOApplication application] context]) == nil) {
101 [self logWithFormat:@"ERROR(%s): cannot procede without context!",
102 __PRETTY_FUNCTION__];
106 if ([self isInternetRequest]) { /* only show primary mailbox in Internet */
107 // just return the primary identity
110 identity = [[ctx activeUser] primaryMailIdentity];
111 return [identity isNotNull] ? [NSArray arrayWithObject:identity] : nil;
114 return [[ctx activeUser] fetchAllMailIdentitiesWithOnlyEmitterAccess:_flag];
117 - (NSArray *)fetchAllIdentities {
118 return [self fetchIdentitiesWithOnlyEmitterAccess:NO];
121 - (NSArray *)fetchIdentitiesWithEmitterPermissions {
122 return [self fetchIdentitiesWithOnlyEmitterAccess:YES];
127 - (BOOL)isValidMailAccountName:(NSString *)_key {
128 if ([_key length] == 0)
134 - (id)mailAccountWithName:(NSString *)_key inContext:(id)_ctx {
135 static Class ctClass = Nil;
139 ctClass = NSClassFromString(@"SOGoMailAccount");
140 if (ctClass == Nil) {
141 [self errorWithFormat:@"missing SOGoMailAccount class!"];
145 ct = [[ctClass alloc] initWithName:_key inContainer:self];
146 return [ct autorelease];
149 - (id)sharedMailAccountWithName:(NSString *)_key inContext:(id)_ctx {
150 static Class ctClass = Nil;
154 ctClass = NSClassFromString(@"SOGoSharedMailAccount");
155 if (ctClass == Nil) {
156 [self errorWithFormat:@"missing SOGoSharedMailAccount class!"];
160 ct = [[ctClass alloc] initWithName:_key inContainer:self];
161 return [ct autorelease];
164 - (id)lookupName:(NSString *)_key inContext:(id)_ctx acquire:(BOOL)_flag {
167 /* first check attributes directly bound to the application */
168 if ((obj = [super lookupName:_key inContext:_ctx acquire:NO]))
171 if (![self isInHomeFolderBranchOfLoggedInAccount:_ctx]) {
172 [self warnWithFormat:@"User %@ tried to access mail hierarchy of %@",
173 [[_ctx activeUser] login], [[self container] nameInContainer]];
175 return [NSException exceptionWithHTTPStatus:403 /* Forbidden */
176 reason:@"Tried to access the mail of another user"];
179 if ([self isValidMailAccountName:_key]) {
180 /* forbid shares for requests coming from the Internet */
183 isSharedKey = [_key rangeOfString:AgenorShareLoginMarker].length > 0;
185 if ([self isInternetRequest]) {
187 return [NSException exceptionWithHTTPStatus:403 /* Forbidden */
189 @"Access to shares forbidden from the Internet"];
194 ? [self sharedMailAccountWithName:_key inContext:_ctx]
195 : [self mailAccountWithName:_key inContext:_ctx];
198 /* return 404 to stop acquisition */
199 return [NSException exceptionWithHTTPStatus:404 /* Not Found */];
204 - (BOOL)davIsCollection {
208 @end /* SOGoMailAccounts */