]> err.no Git - scalable-opengroupware.org/blob - SoObjects/SOGo/SOGoAuthenticator.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1045 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / SoObjects / SOGo / SOGoAuthenticator.m
1 /*
2   Copyright (C) 2004 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 <NGLdap/NGLdapConnection.h>
23 #import "SOGoPermissions.h"
24
25 #include "SOGoAuthenticator.h"
26 #include "SOGoUser.h"
27 #include "common.h"
28
29 @implementation SOGoAuthenticator
30
31 static SOGoAuthenticator *auth = nil;
32
33 + (id) sharedSOGoAuthenticator
34 {
35   if (auth == nil)
36     auth = [[self alloc] init];
37   return auth;
38 }
39
40 - (id) init
41 {
42   if ((self = [super init]))
43     {
44       ud = [NSUserDefaults standardUserDefaults];
45
46       LDAPBaseDN = nil;
47       LDAPHost = nil;
48       LDAPPort = -1;
49
50       authMethod = [[ud stringForKey:@"AuthentificationMethod"] retain];
51       if ([authMethod isEqualToString: @"LDAP"])
52         {
53           LDAPBaseDN = [[ud stringForKey:@"LDAPRootDN"] retain];
54           LDAPHost = [[ud stringForKey:@"LDAPHost"] retain];
55           LDAPPort = [ud integerForKey:@"LDAPPort"];
56         }
57     }
58
59   return self;
60 }
61
62 - (void) dealloc
63 {
64   if (LDAPBaseDN)
65     [LDAPBaseDN release];
66   if (LDAPHost)
67     [LDAPHost release];
68   [authMethod release];
69   [super dealloc];
70 }
71
72 - (BOOL) checkLogin: (NSString *) _login
73            password: (NSString *) _pwd
74 {
75   BOOL accept;
76
77   if ([authMethod isEqualToString: @"LDAP"])
78     accept = [self LDAPCheckLogin: _login password: _pwd];
79   else
80     accept = ([_login length] > 0);
81
82   return (([_login isEqualToString: @"freebusy"]
83            && [_pwd isEqualToString: @"freebusy"])
84           || accept);
85 }
86
87 - (BOOL) LDAPCheckLogin: (NSString *) _login
88                password: (NSString *) _pwd
89 {
90   return [NGLdapConnection checkPassword: _pwd
91                            ofLogin: _login
92                            atBaseDN: LDAPBaseDN
93                            onHost: LDAPHost
94                            port: LDAPPort];
95 }
96
97 /* create SOGoUser */
98
99 - (SOGoUser *) userInContext: (WOContext *)_ctx
100 {
101   static SOGoUser *anonymous = nil, *freebusy;
102   SOGoUser *user;
103   NSArray *traversalPath;
104   NSString *login;
105
106   if (!anonymous)
107     anonymous
108       = [[SOGoUser alloc] initWithLogin:@"anonymous"
109                           roles: [NSArray arrayWithObject: SoRole_Anonymous]];
110   if (!freebusy)
111     freebusy
112       = [[SOGoUser alloc] initWithLogin: @"freebusy"
113                           roles: [NSArray arrayWithObject: SOGoRole_FreeBusy]];
114
115   login = [self checkCredentialsInContext:_ctx];
116   if (login)
117     {
118       if ([login isEqualToString: @"anonymous"])
119         {
120           traversalPath = [_ctx objectForKey: @"SoRequestTraversalPath"];
121           if ([[traversalPath lastObject] isEqualToString: @"freebusy.ifb"])
122             user = freebusy;
123           else
124             user = anonymous;
125         }
126       else
127         user = [[[SOGoUser alloc] initWithLogin: login
128                                   roles: [self rolesForLogin: login]]
129                  autorelease];
130     }
131   else
132     user = nil;
133
134   return user;
135 }
136
137 // - (BOOL) renderException: (NSException *) exception
138 //                inContext: (WOContext *) context
139 // {
140 //   id renderedException;
141 //   WOComponent *tmpComponent;
142 //   WOResponse *response;
143 //   BOOL rc;
144
145 //   rc = [super renderException: exception inContext: context];
146 //   if (!rc)
147 //     {
148 //       tmpComponent = [WOComponent new];
149 //       renderedException = [tmpComponent pageWithName: @"UIxException"];
150 //       if (renderedException)
151 //         {
152 //           rc = YES;
153 //           response = [context response];
154 //           [response setHeader: @"text/html" forKey: @"content-type"];
155 //           [renderedException setClientObject: exception];
156 //           [context setPage: renderedException];
157 //           [renderedException appendToResponse: response
158 //                              inContext: context];
159 //         }
160 //       [tmpComponent release];
161 //     }
162
163 //   return rc;
164 // }
165
166 @end /* SOGoAuthenticator */