]> err.no Git - scalable-opengroupware.org/blobdiff - UI/MainUI/SOGoRootPage.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1196 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / MainUI / SOGoRootPage.m
index a10a6f7ddd0de2ae36093f223f9275675363d139..8b82c41010a6f7ef9a261cfdfad7fc50e4f79199 100644 (file)
   02111-1307, USA.
 */
 
-#include <NGObjWeb/SoComponent.h>
+#import <NGObjWeb/WOApplication.h>
+#import <NGObjWeb/WOContext.h>
+#import <NGObjWeb/WOCookie.h>
+#import <NGObjWeb/WORequest.h>
+#import <NGObjWeb/WOResponse.h>
+#import <NGExtensions/NGBase64Coding.h>
+#import <NGExtensions/NSNull+misc.h>
+#import <NGExtensions/NSString+misc.h>
+#import <NGExtensions/NSObject+Logs.h>
 
-@interface SOGoRootPage : SoComponent
-{
-  NSString *userName;
-}
-
-@end
+#import <SoObjects/SOGo/SOGoWebAuthenticator.h>
+#import <SoObjects/SOGo/SOGoUser.h>
 
-#include <SOGo/SOGoAuthenticator.h>
-#include "common.h"
+#import "SOGoRootPage.h"
 
 @implementation SOGoRootPage
 
-static BOOL doNotRedirect = NO;
-
-+ (void)initialize {
-  NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
-  
-  if ((doNotRedirect = [ud boolForKey:@"SOGoDoNotRedirectRootPage"]))
-    NSLog(@"SOGoRootPage: home-page redirect is disabled.");
-}
-
-- (void)dealloc {
-  [self->userName release];
+- (void) dealloc
+{
+  [userName release];
   [super dealloc];
 }
 
 /* accessors */
 
-- (void)setUserName:(NSString *)_value {
-  ASSIGNCOPY(self->userName, _value);
+- (void) setUserName: (NSString *) _value
+{
+  ASSIGNCOPY (userName, _value);
+}
+
+- (NSString *) userName
+{
+  return userName;
 }
-- (NSString *)userName {
-  return self->userName;
+
+- (NSString *) connectURL
+{
+  return [NSString stringWithFormat: @"%@connect", [self applicationPath]];
 }
 
 /* actions */
-
-- (id)connectAction {
-  NSString *url;
-  
-  [self takeFormValuesForKeys:@"userName", nil];
-  
-  if ([[self userName] length] == 0)
-    return nil;
-  
-  url = [@"/" stringByAppendingString:[[self userName] stringByEscapingURL]];
-  if (![url hasSuffix:@"/"])
-    url = [url stringByAppendingString:@"/"];
-  
-  url = [[self context] urlWithRequestHandlerKey:@"so" 
-                       path:url queryString:nil];
-  return [self redirectToLocation:url];
+- (id <WOActionResults>) connectAction
+{
+  WOResponse *response;
+  WOCookie *authCookie;
+  SOGoWebAuthenticator *auth;
+  NSString *cookieValue, *cookieString;
+
+  auth = [[WOApplication application]
+          authenticatorInContext: context];
+  response = [self responseWith204];
+  cookieString = [NSString stringWithFormat: @"%@:%@",
+                          [self queryParameterForKey: @"userName"],
+                          [self queryParameterForKey: @"password"]];
+  cookieValue = [NSString stringWithFormat: @"basic %@",
+                         [cookieString stringByEncodingBase64]];
+  authCookie = [WOCookie cookieWithName: [auth cookieNameInContext: context]
+                        value: cookieValue];
+  [authCookie setPath: @"/"];
+  [response addCookie: authCookie];
+
+  return response;
 }
 
-- (id<WOActionResults>)defaultAction {
-  WOResponse *r;
-  NSString *login, *rhk;
-  id auth, user;
-  id home, base;
-
-  if (doNotRedirect)
-    return self;
-  
-  /* 
-     Note: ctx.activeUser is NOT set here. Don't know why, so we retrieve
-           the user from the authenticator.
-  */
-  
-  auth  = [[self clientObject] authenticatorInContext:[self context]];
-  user  = [auth userInContext:[self context]];
-  login = [user login];
-  
-  if ([login isEqualToString:@"anonymous"]) {
-    /* use root page for unauthenticated users */
-    return self;
-  }
-
-  /* check base */
-  
-  base = [self application];
-  rhk = [[[self context] request] requestHandlerKey];
-  if (([rhk length] == 0) || ([base requestHandlerForKey:rhk] == nil)) {
-    base = [base lookupName:@"so" inContext:[self context] acquire:NO];
-    
-    if (![base isNotNull] || [base isKindOfClass:[NSException class]]) {
-      /* use root page if home could not be found */
-      [self errorWithFormat:@"Did not find 'so' request handler!"];
-      return self;
+- (id <WOActionResults>) defaultAction
+{
+  id <WOActionResults> response;
+  NSString *login, *oldLocation;
+
+  login = [[context activeUser] login];
+  if (!login || [login isEqualToString: @"anonymous"])
+    response = self;
+  else
+    {
+      oldLocation = [[self clientObject] baseURLInContext: context];
+      response
+       = [self redirectToLocation: [NSString stringWithFormat: @"%@/%@",
+                                             oldLocation, login]];
     }
-  }
-  
-  /* lookup home-page */
-
-  home = [base lookupName:login inContext:[self context] acquire:NO];
-  if (![home isNotNull] || [home isKindOfClass:[NSException class]]) {
-    /* use root page if home could not be found */
-    return self;
-  }
-  
-  /* redirect to home-page */
-  
-  r = [[self context] response];
-  [r setStatus:302 /* moved */];
-  [r setHeader:[home baseURLInContext:[self context]] forKey:@"location"];
-  return r;
+
+  return response;
 }
 
-/* response generation */
-
-- (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
-  NSString *rhk;
-
-  // TODO: we might also want to look into the HTTP basic-auth to redirect to
-  //       the login URL!
-  
-  rhk = [[_ctx request] requestHandlerKey];
-  if ([rhk length]==0 || [[self application] requestHandlerForKey:rhk]==nil) {
-    /* a small hack to redirect to a valid URL */
-    NSString *url;
-    
-    url = [_ctx urlWithRequestHandlerKey:@"so" path:@"/" queryString:nil];
-    [_response setStatus:302 /* moved */];
-    [_response setHeader:url forKey:@"location"];
-    [self logWithFormat:@"URL: %@", url];
-    return;
-  }
-  
-  [super appendToResponse:_response inContext:_ctx];
+- (BOOL) isPublicInContext: (WOContext *) localContext
+{
+  return YES;
 }
 
 @end /* SOGoRootPage */