]> err.no Git - scalable-opengroupware.org/blob - SOGo/SoObjects/Mailer/SOGoMailAccounts.m
use new SoUser API instead of AgenorUserManager
[scalable-opengroupware.org] / SOGo / 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 "common.h"
24 #include <NGObjWeb/SoObject+SoDAV.h>
25 #include <SOGo/WOContext+Agenor.h>
26
27 @implementation SOGoMailAccounts
28
29 static NSString *AgenorShareLoginMarker  = @".-.";
30
31 /* detect webmail being accessed from the outside */
32
33 - (BOOL)isInternetRequest {
34   return [[(WOApplication *)[WOApplication application] context] 
35             isAccessFromIntranet] ? NO : YES;
36 }
37
38 /* listing the available mailboxes */
39
40 - (NSArray *)toManyRelationshipKeys {
41   WOContext *ctx;
42   id        user;
43   id        account;
44   NSArray   *shares;
45   
46   if ((ctx = [[WOApplication application] context]) == nil) {
47     [self logWithFormat:@"ERROR(%s): cannot procede without context!",
48             __PRETTY_FUNCTION__];
49     return nil;
50   }
51   user    = [ctx activeUser];
52   account = [user valueForKey:@"primaryIMAP4AccountString"];
53   if ([account isNotNull]) account = [NSArray arrayWithObject:account];
54   
55   if ([self isInternetRequest]) /* only show primary mailbox in Internet */
56     return account;
57   
58   shares  = [user valueForKey:@"additionalIMAP4AccountStrings"];
59   return ([shares count] == 0)
60     ? account
61     : [account arrayByAddingObjectsFromArray:shares];
62 }
63
64 /* name lookup */
65
66 - (BOOL)isValidMailAccountName:(NSString *)_key {
67   if ([_key length] == 0)
68     return NO;
69   
70   return YES;
71 }
72
73 - (id)mailAccountWithName:(NSString *)_key inContext:(id)_ctx {
74   static Class ctClass = Nil;
75   id ct;
76   
77   if (ctClass == Nil)
78     ctClass = NSClassFromString(@"SOGoMailAccount");
79   if (ctClass == Nil) {
80     [self errorWithFormat:@"missing SOGoMailAccount class!"];
81     return nil;
82   }
83   
84   ct = [[ctClass alloc] initWithName:_key inContainer:self];
85   return [ct autorelease];
86 }
87
88 - (id)lookupName:(NSString *)_key inContext:(id)_ctx acquire:(BOOL)_flag {
89   id obj;
90   
91   /* first check attributes directly bound to the application */
92   if ((obj = [super lookupName:_key inContext:_ctx acquire:NO]))
93     return obj;
94   
95   if ([self isValidMailAccountName:_key]) {
96     /* forbid shares for requests coming from the Internet */
97     if ([self isInternetRequest]) {
98       if ([_key rangeOfString:AgenorShareLoginMarker].length > 0) {
99         return [NSException exceptionWithHTTPStatus:403 /* Forbidden */
100                             reason:
101                               @"Access to shares forbidden from the Internet"];
102       }
103     }
104     
105     return [self mailAccountWithName:_key inContext:_ctx];
106   }
107
108   /* return 404 to stop acquisition */
109   return [NSException exceptionWithHTTPStatus:404 /* Not Found */];
110 }
111
112 /* WebDAV */
113
114 - (BOOL)davIsCollection {
115   return YES;
116 }
117
118 @end /* SOGoMailAccounts */