]> err.no Git - scalable-opengroupware.org/blob - UI/MainUI/SOGoRootPage.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1101 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / MainUI / SOGoRootPage.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 <NGObjWeb/SoComponent.h>
23
24 @interface SOGoRootPage : SoComponent
25 {
26   NSString *userName;
27 }
28
29 @end
30
31 #include <SOGo/SOGoAuthenticator.h>
32 #include "common.h"
33
34 @implementation SOGoRootPage
35
36 static BOOL doNotRedirect = NO;
37
38 + (void)initialize {
39   NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
40   
41   if ((doNotRedirect = [ud boolForKey:@"SOGoDoNotRedirectRootPage"]))
42     NSLog(@"SOGoRootPage: home-page redirect is disabled.");
43 }
44
45 - (void)dealloc {
46   [self->userName release];
47   [super dealloc];
48 }
49
50 /* accessors */
51
52 - (void)setUserName:(NSString *)_value {
53   ASSIGNCOPY(self->userName, _value);
54 }
55 - (NSString *)userName {
56   return self->userName;
57 }
58
59 /* actions */
60
61 - (id)connectAction {
62   NSString *url;
63   
64   [self takeFormValuesForKeys:@"userName", nil];
65   
66   if ([[self userName] length] == 0)
67     return nil;
68   
69   url = [@"/" stringByAppendingString:[[self userName] stringByEscapingURL]];
70   if (![url hasSuffix:@"/"])
71     url = [url stringByAppendingString:@"/"];
72   
73   url = [[self context] urlWithRequestHandlerKey:@"so" 
74                         path:url queryString:nil];
75   return [self redirectToLocation:url];
76 }
77
78 - (id<WOActionResults>)defaultAction {
79   WOResponse *r;
80   NSString *login, *rhk;
81   id auth, user;
82   id home, base;
83
84   if (doNotRedirect)
85     return self;
86   
87   /* 
88      Note: ctx.activeUser is NOT set here. Don't know why, so we retrieve
89            the user from the authenticator.
90   */
91   
92   auth  = [[self clientObject] authenticatorInContext:[self context]];
93   user  = [auth userInContext:[self context]];
94   login = [user login];
95   
96   if ([login isEqualToString:@"anonymous"]) {
97     /* use root page for unauthenticated users */
98     return self;
99   }
100
101   /* check base */
102   
103   base = [self application];
104   rhk = [[[self context] request] requestHandlerKey];
105   if (([rhk length] == 0) || ([base requestHandlerForKey:rhk] == nil)) {
106     base = [base lookupName:@"so" inContext:[self context] acquire:NO];
107     
108     if (![base isNotNull] || [base isKindOfClass:[NSException class]]) {
109       /* use root page if home could not be found */
110       [self errorWithFormat:@"Did not find 'so' request handler!"];
111       return self;
112     }
113   }
114   
115   /* lookup home-page */
116
117   home = [base lookupName:login inContext:[self context] acquire:NO];
118   if (![home isNotNull] || [home isKindOfClass:[NSException class]]) {
119     /* use root page if home could not be found */
120     return self;
121   }
122   
123   /* redirect to home-page */
124   
125   r = [[self context] response];
126   [r setStatus:302 /* moved */];
127   [r setHeader:[home baseURLInContext:[self context]] forKey:@"location"];
128   return r;
129 }
130
131 /* response generation */
132
133 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
134   NSString *rhk;
135
136   // TODO: we might also want to look into the HTTP basic-auth to redirect to
137   //       the login URL!
138   
139   rhk = [[_ctx request] requestHandlerKey];
140   if ([rhk length]==0 || [[self application] requestHandlerForKey:rhk]==nil) {
141     /* a small hack to redirect to a valid URL */
142     NSString *url;
143     
144     url = [_ctx urlWithRequestHandlerKey:@"so" path:@"/" queryString:nil];
145     [_response setStatus:302 /* moved */];
146     [_response setHeader:url forKey:@"location"];
147     [self logWithFormat:@"URL: %@", url];
148     return;
149   }
150   
151   [super appendToResponse:_response inContext:_ctx];
152 }
153
154 @end /* SOGoRootPage */