]> err.no Git - scalable-opengroupware.org/blob - SoObjects/SOGo/SOGoAuthenticator.m
initial sync
[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 result;
76
77 //   return YES;
78   if ([authMethod isEqualToString: @"LDAP"])
79     result = [self LDAPCheckLogin: _login password: _pwd];
80   else
81     {
82       if ([_login length] == 0)
83         result = NO;
84       else
85         result = YES;
86     }
87
88   return result;
89 }
90
91 - (BOOL) LDAPCheckLogin: (NSString *) _login
92                password: (NSString *) _pwd
93 {
94   return [NGLdapConnection checkPassword: _pwd
95                            ofLogin: _login
96                            atBaseDN: LDAPBaseDN
97                            onHost: LDAPHost
98                            port: LDAPPort];
99 }
100
101 /* create SOGoUser */
102
103 - (SoUser *) userInContext:(WOContext *)_ctx
104 {
105   static SoUser *anonymous = nil, *freebusy;
106   NSString  *login;
107   
108   if (!anonymous)
109     anonymous
110       = [[SOGoUser alloc] initWithLogin:@"anonymous"
111                           roles: [NSArray arrayWithObject: SoRole_Anonymous]];
112
113   if (!freebusy)
114     freebusy
115       = [[SOGoUser alloc] initWithLogin: @"freebusy"
116                           roles: [NSArray arrayWithObject: SOGoRole_FreeBusy]];
117
118   if ((login = [self checkCredentialsInContext:_ctx]) == nil)
119     /* some error (otherwise result would have been anonymous */
120     return nil;
121   
122   if ([login isEqualToString: @"anonymous"])
123     return anonymous;
124   else if ([login isEqualToString: @"freebusy"])
125     return freebusy;
126
127 //   uroles = [NSMutableArray arrayWithArray: ];
128
129   return [[[SOGoUser alloc] initWithLogin: login
130                             roles: [self rolesForLogin: login]]
131            autorelease];
132 }
133
134 // - (BOOL) renderException: (NSException *) exception
135 //                inContext: (WOContext *) context
136 // {
137 //   id renderedException;
138 //   WOComponent *tmpComponent;
139 //   WOResponse *response;
140 //   BOOL rc;
141
142 //   rc = [super renderException: exception inContext: context];
143 //   if (!rc)
144 //     {
145 //       tmpComponent = [WOComponent new];
146 //       renderedException = [tmpComponent pageWithName: @"UIxException"];
147 //       if (renderedException)
148 //         {
149 //           rc = YES;
150 //           response = [context response];
151 //           [response setHeader: @"text/html" forKey: @"content-type"];
152 //           [renderedException setClientObject: exception];
153 //           [context setPage: renderedException];
154 //           [renderedException appendToResponse: response
155 //                              inContext: context];
156 //         }
157 //       [tmpComponent release];
158 //     }
159
160 //   return rc;
161 // }
162
163 @end /* SOGoAuthenticator */