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 ([context isAccessFromIntranet] ? NO : YES);
38 /* listing the available mailboxes */
40 - (BOOL) isInHomeFolderBranchOfLoggedInAccount: (NSString *) userLogin
42 return [[[self container] nameInContainer] isEqualToString: userLogin];
45 - (NSArray *)toManyRelationshipKeys {
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.
57 But then, the home-folder would need to know about mail
58 functionality which isn't perfect either.
61 user = [context activeUser];
62 userLogin = [user login];
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]];
71 account = [user primaryIMAP4AccountString];
72 if ([account isNotNull]) account = [NSArray arrayWithObject:account];
74 if ([self isInternetRequest]) /* only show primary mailbox in Internet */
77 shares = [user valueForKey:@"additionalIMAP4AccountStrings"];
78 return ([shares count] == 0)
80 : [account arrayByAddingObjectsFromArray:shares];
83 - (NSArray *)fetchIdentitiesWithOnlyEmitterAccess:(BOOL)_flag {
86 if ([self isInternetRequest]) { /* only show primary mailbox in Internet */
87 // just return the primary identity
90 identity = [[context activeUser] primaryMailIdentity];
91 return [identity isNotNull] ? [NSArray arrayWithObject:identity] : nil;
94 return [[ctx activeUser] fetchAllMailIdentitiesWithOnlyEmitterAccess:_flag];
97 - (NSArray *)fetchAllIdentities {
98 return [self fetchIdentitiesWithOnlyEmitterAccess:NO];
101 - (NSArray *)fetchIdentitiesWithEmitterPermissions {
102 return [self fetchIdentitiesWithOnlyEmitterAccess:YES];
107 - (BOOL)isValidMailAccountName:(NSString *)_key {
108 if ([_key length] == 0)
114 - (id)mailAccountWithName:(NSString *)_key inContext:(id)_ctx {
115 static Class ctClass = Nil;
119 ctClass = NSClassFromString(@"SOGoMailAccount");
120 if (ctClass == Nil) {
121 [self errorWithFormat:@"missing SOGoMailAccount class!"];
125 ct = [[ctClass alloc] initWithName:_key inContainer:self];
126 return [ct autorelease];
129 - (id)sharedMailAccountWithName:(NSString *)_key inContext:(id)_ctx {
130 static Class ctClass = Nil;
134 ctClass = NSClassFromString(@"SOGoSharedMailAccount");
135 if (ctClass == Nil) {
136 [self errorWithFormat:@"missing SOGoSharedMailAccount class!"];
140 ct = [[ctClass alloc] initWithName:_key inContainer:self];
141 return [ct autorelease];
144 - (id)lookupName:(NSString *)_key inContext:(id)_ctx acquire:(BOOL)_flag
149 userLogin = [[context activeUser] login];
151 /* first check attributes directly bound to the application */
152 if ((obj = [super lookupName:_key inContext:_ctx acquire:NO]))
155 if (![self isInHomeFolderBranchOfLoggedInAccount: userLogin]) {
156 [self warnWithFormat:@"User %@ tried to access mail hierarchy of %@",
157 userLogin, [[self container] nameInContainer]];
159 return [NSException exceptionWithHTTPStatus:403 /* Forbidden */
160 reason:@"Tried to access the mail of another user"];
163 if ([self isValidMailAccountName:_key]) {
164 /* forbid shares for requests coming from the Internet */
167 isSharedKey = [_key rangeOfString:AgenorShareLoginMarker].length > 0;
169 if ([self isInternetRequest]) {
171 return [NSException exceptionWithHTTPStatus:403 /* Forbidden */
173 @"Access to shares forbidden from the Internet"];
178 ? [self sharedMailAccountWithName:_key inContext:_ctx]
179 : [self mailAccountWithName:_key inContext:_ctx];
182 /* return 404 to stop acquisition */
183 return [NSException exceptionWithHTTPStatus:404 /* Not Found */];
188 - (BOOL)davIsCollection {
192 @end /* SOGoMailAccounts */