]> err.no Git - sope/blob - sope-appserver/samples/iCalPortal/Pages/iCalPortalRegistrationPage.m
renamed packages as discussed in the developer list
[sope] / sope-appserver / samples / iCalPortal / Pages / iCalPortalRegistrationPage.m
1 /*
2   Copyright (C) 2000-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 // $Id$
22
23 #include "iCalPortalPage.h"
24 #include <NGObjWeb/WODirectAction.h>
25
26 @interface iCalPortalRegistrationPage : iCalPortalPage
27 {
28   NSString *login;
29   NSString *firstName;
30   NSString *lastName;
31   NSString *email;
32   NSString *address;
33   NSString *city;
34   NSString *state;
35   NSString *zip;
36   NSString *country;
37   NSString *phone;
38   NSString *wantIcalNews;
39   NSString *wantSkyrixNews;
40
41   BOOL loginOK;
42   BOOL loginAvailable;
43   BOOL pwdOK;
44   BOOL firstNameOK;
45   BOOL lastNameOK;
46   BOOL emailOK;
47 }
48
49 - (void)markLoginWrong;
50 - (void)markLoginUsed;
51 - (void)markPwdWrong;
52 - (void)markFirstNameWrong;
53 - (void)markLastNameWrong;
54 - (void)markEmailWrong;
55
56 @end
57
58 @interface iCalPortalRegistrationAction : WODirectAction
59 @end
60
61 #include "common.h"
62
63 @interface NSString(EmailAddress)
64 - (BOOL)isValidEmailAddress;
65 @end
66
67 @implementation iCalPortalRegistrationPage
68
69 - (id)init {
70   if ((self = [super init])) {
71     self->loginOK        = YES;
72     self->loginAvailable = YES;
73     self->pwdOK          = YES;
74     self->firstNameOK    = YES;
75     self->lastNameOK     = YES;
76     self->emailOK        = YES;
77   }
78   return self;
79 }
80
81 - (void)dealloc {
82   [self->login          release];
83   [self->firstName      release];
84   [self->lastName       release];
85   [self->email          release];
86   [self->address        release];
87   [self->city           release];
88   [self->state          release];
89   [self->zip            release];
90   [self->country        release];
91   [self->phone          release];
92   [self->wantIcalNews   release];
93   [self->wantSkyrixNews release];
94   [super dealloc];
95 }
96
97 /* accessors */
98
99 - (void)setLogin:(NSString *)_value {
100   ASSIGN(self->login, _value);
101 }
102 - (NSString *)login {
103   return self->login;
104 }
105
106 - (void)setFirstName:(NSString *)_value {
107   ASSIGN(self->firstName, _value);
108 }
109 - (NSString *)firstName {
110   return self->firstName;
111 }
112
113 - (void)setLastName:(NSString *)_value {
114   ASSIGN(self->lastName, _value);
115 }
116 - (NSString *)lastName {
117   return self->lastName;
118 }
119
120 - (void)setEmail:(NSString *)_value {
121   ASSIGN(self->email, _value);
122 }
123 - (NSString *)email {
124   return self->email;
125 }
126
127 - (void)setAddress:(NSString *)_value {
128   ASSIGN(self->address, _value);
129 }
130 - (NSString *)address {
131   return self->address;
132 }
133
134 - (void)setCity:(NSString *)_value {
135   ASSIGN(self->city, _value);
136 }
137 - (NSString *)city {
138   return self->city;
139 }
140
141 - (void)setState:(NSString *)_value {
142   ASSIGN(self->state, _value);
143 }
144 - (NSString *)state {
145   return self->state;
146 }
147
148 - (void)setZip:(NSString *)_value {
149   ASSIGN(self->zip, _value);
150 }
151 - (NSString *)zip {
152   return self->zip;
153 }
154
155 - (void)setCountry:(NSString *)_value {
156   ASSIGN(self->country, _value);
157 }
158 - (NSString *)country {
159   return self->country;
160 }
161
162 - (void)setPhone:(NSString *)_value {
163   ASSIGN(self->phone, _value);
164 }
165 - (NSString *)phone {
166   return self->phone;
167 }
168
169 - (void)setWantIcalNews:(NSString *)_value {
170   ASSIGN(self->wantIcalNews, _value);
171 }
172 - (NSString *)wantIcalNews {
173   return self->wantIcalNews;
174 }
175
176 - (void)setWantSkyrixNews:(NSString *)_value {
177   ASSIGN(self->wantSkyrixNews, _value);
178 }
179 - (NSString *)wantSkyrixNews {
180   return self->wantSkyrixNews;
181 }
182
183 - (void)markLoginWrong {
184   self->loginOK = NO;
185 }
186 - (BOOL)loginWrong {
187   return !self->loginOK;
188 }
189 - (void)markLoginUsed {
190   self->loginAvailable = NO;
191 }
192 - (BOOL)loginUsed {
193   return !self->loginAvailable;
194 }
195 - (void)markPwdWrong {
196   self->pwdOK = NO;
197 }
198 - (BOOL)pwdWrong {
199   return !self->pwdOK;
200 }
201 - (void)markFirstNameWrong {
202   self->firstNameOK = NO;
203 }
204 - (BOOL)firstNameWrong {
205   return !self->firstNameOK;
206 }
207 - (void)markLastNameWrong {
208   self->lastNameOK = NO;
209 }
210 - (BOOL)lastNameWrong {
211   return !self->lastNameOK;
212 }
213 - (void)markEmailWrong {
214   self->emailOK = NO;
215 }
216 - (BOOL)emailWrong {
217   return !self->emailOK;
218 }
219
220 /* actions */
221
222 - (void)validate {
223   // login: 4 to 16
224   // pwd:   6 to 20
225 }
226
227 - (BOOL)isSessionProtectedPage {
228   return NO;
229 }
230
231 - (id)run {
232   return self;
233 }
234
235 @end /* iCalPortalRegistrationPage */
236
237 #include "iCalPortalUser.h"
238 #include "iCalPortalDatabase.h"
239
240 @implementation iCalPortalRegistrationAction
241
242 - (NSMutableDictionary *)collectFormValues {
243   static NSString *keys[] = {
244     /* do not add passwords here !! */
245     @"login",
246     @"firstName",
247     @"lastName",
248     @"email",
249     @"address",
250     @"city",
251     @"state",
252     @"zip",
253     @"country",
254     @"phone",
255     @"wantIcalNews",
256     @"wantSkyrixNews",
257     nil
258   };
259   NSMutableDictionary *md;
260   WORequest *rq;
261   NSString  *key;
262   unsigned  i;
263   
264   md = [NSMutableDictionary dictionaryWithCapacity:16];
265   rq = [self request];
266   
267   for (i = 0; (key = keys[i]) != nil; i++) {
268     NSString *val;
269     
270     val = [rq formValueForKey:key];
271     if ([val isNotNull])
272       [md setObject:val forKey:key];
273   }
274   
275   return md;
276 }
277
278 - (id)saveProfile {
279   iCalPortalRegistrationPage *page;
280   iCalPortalDatabase  *db;
281   NSMutableDictionary *values;
282   NSString            *tmp;
283   BOOL loginOK        = YES;
284   BOOL loginAvailable = YES;
285   BOOL pwdOK          = YES;
286   BOOL firstNameOK    = YES;
287   BOOL lastNameOK     = YES;
288   BOOL emailOK        = YES;
289   
290   if ((db = [(id)[WOApplication application] database]) == nil)
291     return nil;
292   
293   values = [self collectFormValues];
294   
295   /* validate login */
296   
297   tmp = [values objectForKey:@"login"];
298   if ((loginOK = [db isLoginNameValid:tmp]))
299     loginAvailable = [db isLoginNameUsed:tmp] ? NO : YES;
300   
301   /* validate password */
302   
303   tmp = [[self request] formValueForKey:@"pwd1"];
304   if ((pwdOK = [db isPasswordValid:tmp])) {
305     NSString *tmp2;
306     
307     if ((tmp2 = [[self request] formValueForKey:@"pwd2"])) {
308       if (![tmp2 isEqualToString:tmp])
309         pwdOK = NO;
310     }
311     else
312       pwdOK = NO;
313   }
314   
315   /* validate names */
316   
317   tmp = [values objectForKey:@"firstName"];
318   if ([tmp length] < 3) firstNameOK = NO;
319   tmp = [values objectForKey:@"lastName"];
320   if ([tmp length] < 3) lastNameOK = NO;
321
322   /* validate email */
323   
324   emailOK = [[values objectForKey:@"email"] isValidEmailAddress];
325   
326   /* process */
327   
328   if (loginOK&&loginAvailable&&pwdOK&&firstNameOK&&lastNameOK&&emailOK) {
329     iCalPortalUser *user = nil;
330     NSString *pwd = nil, *login = nil;
331     
332     pwd = [[self request] formValueForKey:@"pwd1"];
333     
334     login = [values objectForKey:@"login"];
335     
336     if ([db createUser:login info:values password:pwd]) {
337       [self logWithFormat:@"did create account: %@ ...", login];
338       
339       user = [db userWithName:[values objectForKey:@"login"] password:pwd];
340       if (user == nil) {
341         [self logWithFormat:@"login of created account failed ?! (user=%@)",
342                 user];
343       }
344       else {
345         [[self session] setObject:user forKey:@"user"];
346         return [[self pageWithName:@"iCalPortalHomePage"] performPage];
347       }
348     }
349     else {
350       [self logWithFormat:@"failed to create account: %@ ...", user];
351     }
352   }
353   
354   page = [self pageWithName:@"iCalPortalRegistrationPage"];
355   
356   [page takeValuesFromDictionary:values];
357   
358   if (!loginOK)        [page markLoginWrong];
359   if (!loginAvailable) [page markLoginUsed];
360   if (!pwdOK)          [page markPwdWrong];
361   if (!firstNameOK)    [page markFirstNameWrong];
362   if (!lastNameOK)     [page markLastNameWrong];
363   if (!emailOK)        [page markEmailWrong];
364   
365   return [page performPage];
366 }
367
368 - (id)saveProfileAction {
369   if ([[[self request] formValueForKey:@"action"] isEqualToString:@"save"])
370     return [self saveProfile];
371   
372   return [self indexPage];
373 }
374
375 @end /* iCalPortalRegistrationAction */
376
377 @implementation NSString(EmailAddress)
378
379 - (BOOL)isValidEmailAddress {
380   BOOL emailOK;
381   
382   if ([self length] < 3) 
383     return NO;
384   
385   {
386     static NSString *forbiddenOnes[] = {
387       @"steve@apple.com",
388       @"jobs@apple.com",
389       @"steve.jobs@apple.com",
390       @"bill@microsoft",
391       @"gates@microsoft",
392       @"bill.gates@microsoft",
393       nil
394     };
395     NSString *tmp;
396     unsigned i;
397     NSRange  r;
398     
399     for (i = 0; (tmp = forbiddenOnes[i]) != nil; i++) {
400       if ([self rangeOfString:tmp].length > 0) {
401         [self logWithFormat:@"someone tried '%@' ...", tmp];
402         return NO;
403       }
404     }
405     
406     tmp = self;
407     
408     r = [tmp rangeOfString:@"@"];
409     if (r.length == 0)
410       emailOK = NO;
411     else if (r.location < 2) 
412       emailOK = NO;
413     else {
414       tmp = [tmp substringFromIndex:(r.location + r.length)];
415       r = [tmp rangeOfString:@"."];
416       if (r.length == 0)
417         emailOK = NO;
418       else if (r.location == 0)
419         emailOK = NO;
420       else {
421         tmp = [tmp substringFromIndex:(r.location + r.length)];
422         emailOK = [tmp length] > 1;
423       }
424     }
425   }
426   return emailOK;
427 }
428
429 @end /* NSString(EmailAddress) */