2 Copyright (C) 2000-2004 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
23 #include "iCalAction.h"
24 #include "iCalPortalDatabase.h"
25 #include "iCalPortalUser.h"
28 @implementation iCalAction
30 - (id)initWithContext:(WOContext *)_ctx {
31 self->ctx = [_ctx retain];
41 - (WOContext *)context {
44 - (WORequest *)request {
45 return [self->ctx request];
48 return [WOApplication application];
51 - (NSString *)requestUser {
55 s = [[self request] requestHandlerPath];
56 r = [s rangeOfString:@"/"];
57 if (r.length == 0) return nil;
58 return [s substringToIndex:r.location];
60 - (NSString *)requestCalendarPath {
64 s = [[self request] requestHandlerPath];
65 r = [s rangeOfString:@"/"];
66 if (r.length == 0) return nil;
67 return [s substringFromIndex:(r.location + 1)];
76 - (NSString *)credentials {
81 if ((rq = [self->ctx request]) == nil)
83 if ((creds = [rq headerForKey:@"authorization"]) == nil)
86 r = [creds rangeOfString:@" " options:NSBackwardsSearch];
88 [self logWithFormat:@"invalid 'authorization' header: '%@'", creds];
91 return [creds substringFromIndex:(r.location + r.length)];
94 - (NSString *)credentialsLogin {
97 creds = [creds stringByDecodingBase64];
98 creds = [creds componentsSeparatedByString:@":"];
99 if ([creds count] < 2) {
100 [self logWithFormat:@"invalid credentials"];
104 return [creds objectAtIndex:0];
107 - (iCalPortalDatabase *)database {
108 return [(id)[WOApplication application] database];
111 - (iCalPortalUser *)user {
112 iCalPortalDatabase *db;
113 iCalPortalUser *user;
115 NSString *login, *pwd;
117 if ((db = [self database]) == nil)
120 if ((creds = [self credentials]) == nil)
123 /* assuming basic authentication ... */
124 creds = [creds stringByDecodingBase64];
125 creds = [creds componentsSeparatedByString:@":"];
126 if ([creds count] < 2) {
127 [self logWithFormat:@"invalid credentials"];
131 login = [creds objectAtIndex:0];
132 pwd = [creds objectAtIndex:1];
134 user = [db userWithName:login password:pwd];
139 - (NSString *)authRealm {
140 WOApplication *app = [self application];
144 - (WOResponse *)missingAuthResponse {
148 auth = [NSString stringWithFormat:@"Basic realm=\"%@\"",[self authRealm]];
150 resp = [(WOResponse *)[WOResponse alloc] initWithRequest:[self request]];
151 [resp setStatus:401 /* unauthorized */];
152 [resp setHeader:auth forKey:@"www-authenticate"];
153 //[resp setHeader:@"close" forKey:@"connection"];
154 [resp setHeader:@"text/html; charset=iso-8859-1" forKey:@"content-type"];
155 [resp appendContentString:
156 @"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">"
158 @"<TITLE>401 Authorization Required</TITLE>"
160 @"<H1>Authorization Required</H1>"
161 @"<ADDRESS>Apache/1.3.26 Server at dogbert Port 9000</ADDRESS>"
165 return AUTORELEASE(resp);
168 - (WOResponse *)accessDeniedResponse {
172 auth = [NSString stringWithFormat:@"Basic realm=\"%@\"",[self authRealm]];
174 [self logWithFormat:@"access was denied"];
176 resp = [(WOResponse *)[WOResponse alloc] initWithRequest:[self request]];
177 [resp setStatus:403 /* forbidden */];
178 [resp setHeader:auth forKey:@"www-authenticate"];
179 return [resp autorelease];
182 - (WOResponse *)notFoundResponse {
185 resp = [(WOResponse *)[WOResponse alloc] initWithRequest:[self request]];
186 [resp setStatus:404 /* not found */];
187 return [resp autorelease];
190 @end /* iCalAction */
192 @implementation iCalFakeAction
194 - (id)initWithContext:(WOContext *)_ctx code:(int)_status {
195 if ((self = [super initWithContext:_ctx])) {
196 self->code = _status;
201 - (id)initWithContext:(WOContext *)_ctx {
202 return [self initWithContext:_ctx code:200];
205 - (WOResponse *)run {
208 r = [WOResponse responseWithRequest:[self request]];
209 [r setStatus:self->code];
211 [r setHeader:@"close" forKey:@"connection"];
212 [r setHeader:@"text/plain; charset=iso-8859-1" forKey:@"content-type"];
214 [r appendContentString:@"operation executed\r\n"];
219 @end /* iCalFakeAction */