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