This repository has been archived by the owner on Aug 14, 2019. It is now read-only.
/
sfApplyActionsLibrary.class.php
531 lines (506 loc) · 18.6 KB
/
sfApplyActionsLibrary.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
<?php
/**
* sfApplyActionsLibrary is an sfActions based library for sfApplyActions class.
* Inherit it if you want to override any methods, and still use some of the
* original functionality.
*
* @author fizyk
*/
class sfApplyActionsLibrary extends sfActions
{
//When user is applying for new account
public function executeApply(sfRequest $request)
{
// we're getting default or customized applyForm for the task
if( !( ($this->form = $this->newForm( 'applyForm' ) ) instanceof sfApplyApplyForm) )
{
// if the form isn't instance of sfApplyApplyForm, we don't accept it
throw new InvalidArgumentException(
'The custom apply form should be instance of sfApplyApplyForm' );
}
//Code below is used when user is sending his application!
if( $request->isMethod('post') )
{
//gathering form request in one array
$formValues = $request->getParameter( $this->form->getName() );
if(sfConfig::get('app_recaptcha_enabled') )
{
$captcha = array(
'recaptcha_challenge_field' => $request->getParameter('recaptcha_challenge_field'),
'recaptcha_response_field' => $request->getParameter('recaptcha_response_field'),
);
//Adding captcha to form array
$formValues = array_merge( $formValues, array('captcha' => $captcha) );
}
//binding request form parameters with form
$this->form->bind($formValues);
if ($this->form->isValid())
{
$guid = "n" . self::createGuid();
$this->form->setValidate($guid);
$this->form->save();
$confirmation = sfConfig::get( 'app_sfForkedApply_confirmation' );
if( $confirmation['apply'] )
{
try
{
//Extracting object and sending creating verification mail
$profile = $this->form->getObject();
$this->sendVerificationMail($profile);
return 'After';
}
catch (Exception $e)
{
//Cleaning after possible exception thrown in ::sendVerificationMail() method
$profile = $this->form->getObject();
$user = $profile->getUser();
$user->delete();
//We rethrow exception for the dev environment. This catch
//catches other than mailer exception, i18n as well. So developer
//now knows what he's up to.
if( sfContext::getInstance()->getConfiguration()->getEnvironment() === 'dev' )
{
throw $e;
}
return 'MailerError';
}
}
else
{
$this->activateUser( $this->form->getObject()->getUser() );
return $this->redirect( '@homepage' );
}
}
}
}
//Processes reset requests
public function executeResetRequest(sfRequest $request)
{
$user = $this->getUser();
$confirmation = sfConfig::get( 'app_sfForkedApply_confirmation' );
//if user is authenticated and confirmation for reset for logged users is disabled
if( $user->isAuthenticated() && !$confirmation['reset_logged'] )
{
$this->redirect( 'sfApply/reset' );
}
//if user is authenticated and confirmation for reset for logged users is enabled
elseif( $user->isAuthenticated() && $confirmation['reset_logged'] )
{
return $this->resetRequestBody( $this->getUser()->getGuardUser() );
}
else
{
$this->forward404Unless( $confirmation['reset'] );
// we're getting default or customized resetRequestForm for the task
if( !( ($this->form = $this->newForm( 'resetRequestForm' ) ) instanceof sfApplyResetRequestForm) )
{
// if the form isn't instance of sfApplySettingsForm, we don't accept it
throw new InvalidArgumentException(
'The custom resetRequest form should be instance of sfApplyResetRequestForm'
);
}
if ($request->isMethod('post'))
{
//gathering form request in one array
$formValues = $request->getParameter( $this->form->getName() );
if(sfConfig::get('app_recaptcha_enabled') )
{
$captcha = array(
'recaptcha_challenge_field' => $request->getParameter('recaptcha_challenge_field'),
'recaptcha_response_field' => $request->getParameter('recaptcha_response_field'),
);
//Adding captcha to form array
$formValues = array_merge( $formValues, array('captcha' => $captcha) );
}
//binding request form parameters with form
$this->form->bind($formValues);
if ($this->form->isValid())
{
// The form matches unverified users, but retrieveByUsername does not, so
// use an explicit query. We'll special-case the unverified users in
// resetRequestBody
$username_or_email = $this->form->getValue('username_or_email');
if (strpos($username_or_email, '@') !== false)
{
$user = Doctrine::getTable('sfGuardUser')->createQuery('u')->
innerJoin('u.Profile p')->where('p.email = ?', $username_or_email)->
fetchOne();
}
else
{
$user = Doctrine::getTable('sfGuardUser')->createQuery('u')->
where('username = ?', $username_or_email)->fetchOne();
}
return $this->resetRequestBody($user);
}
}
}
}
public function executeConfirm(sfRequest $request)
{
$validate = $this->request->getParameter('validate');
// 0.6.3: oops, this was in sfGuardUserProfilePeer in my application
// and therefore never got shipped with the plugin until I built
// a second site and spotted it!
// Note that this only works if you set foreignAlias and
// foreignType correctly
$sfGuardUser = Doctrine_Query::create()
->from("sfGuardUser u")
->innerJoin("u.Profile p with p.validate = ?", $validate)
->fetchOne();
if (!$sfGuardUser)
{
return 'Invalid';
}
$type = self::getValidationType($validate);
if (!strlen($validate))
{
return 'Invalid';
}
$profile = $sfGuardUser->getProfile();
$profile->setValidate(null);
$profile->save();
if ($type == 'New')
{
$this->activateUser( $sfGuardUser );
}
if ($type == 'Reset')
{
$this->getUser()->setAttribute('sfApplyReset', $sfGuardUser->getId());
return $this->redirect('sfApply/reset');
}
if( $type == 'Email' )
{
if( sfConfig::get( 'app_sfForkedApply_mail_editable' ) )
{
$profile->setEmail( $profile->getEmailNew() );
$profile->setEmailNew( null );
$profile->save();
$this->getUser()->setFlash( 'sf_forked_apply',
sfContext::getInstance()->getI18N()->
__( 'Your email has been changed.',
array(), 'sfForkedApply' ) );
return $this->redirect( '@homepage' );
}
return 'Invalid';
}
}
public function executeReset(sfRequest $request)
{
//won't present this page to users that are not authenticated or haven't got confirmation code
if( !$this->getUser()->isAuthenticated() && !$this->getUser()->getAttribute('sfApplyReset', false) )
{
$this->redirect( '@sf_guard_signin' );
}
// we're getting default or customized resetForm for the task
if( !( ($this->form = $this->newForm( 'resetForm') ) instanceof sfApplyResetForm) )
{
// if the form isn't instance of sfApplyResetForm, we don't accept it
throw new InvalidArgumentException(
'The custom reset form should be instance of sfApplyResetForm'
);
}
if ($request->isMethod('post'))
{
$this->form->bind($request->getParameter( $this->form->getName() ));
if ($this->form->isValid())
{
//This got fixed (0.9.1), so if user is authenticated, and requests password change, we're still getting his id.
$this->id = ( $this->getUser()->isAuthenticated() ) ? $this->getUser()->getGuardUser()->getId() : $this->getUser()->getAttribute('sfApplyReset', false);
$this->forward404Unless($this->id);
$this->sfGuardUser = Doctrine::getTable('sfGuardUser')->find($this->id);
$this->forward404Unless($this->sfGuardUser);
$sfGuardUser = $this->sfGuardUser;
$sfGuardUser->setPassword($this->form->getValue('password'));
$sfGuardUser->save();
$this->getUser()->signIn($sfGuardUser);
$this->getUser()->setAttribute('sfApplyReset', null);
return 'After';
}
}
if( $this->getUser()->isAuthenticated() )
{
return 'Logged';
}
}
public function executeResetCancel()
{
$this->getUser()->setAttribute('sfApplyReset', null);
return $this->redirect(sfConfig::get('app_sfApplyPlugin_after', '@homepage'));
}
public function executeSettings(sfRequest $request)
{
// sfApplySettingsForm inherits from sfApplyApplyForm, which
// inherits from sfGuardUserProfile. That minimizes the amount
// of duplication of effort. If you want, you can use a different
// form class. I suggest inheriting from sfApplySettingsForm and
// making further changes after calling parent::configure() from
// your own configure() method.
$profile = $this->getUser()->getProfile();
// we're getting default or customized settingsForm for the task
if( !( ($this->form = $this->newForm( 'settingsForm', $profile) ) instanceof sfApplySettingsForm) )
{
// if the form isn't instance of sfApplySettingsForm, we don't accept it
throw new InvalidArgumentException( sfContext::getInstance()->
getI18N()->
__( 'The custom %action% form should be instance of %form%',
array( '%action%' => 'settings',
'%form%' => 'sfApplySettingsForm' ), 'sfForkedApply' )
);
}
if ($request->isMethod('post'))
{
$this->form->bind($request->getParameter( $this->form->getName() ));
if ($this->form->isValid())
{
$this->form->save();
return $this->redirect('@homepage');
}
}
}
public function executeEditEmail(sfRequest $request)
{
$this->forward404Unless( sfConfig::get( 'app_sfForkedApply_mail_editable' ) );
if( !( ($this->form = $this->newForm( 'editEmailForm') ) instanceof sfApplyEditEmailForm) )
{
// if the form isn't instance of sfApplySettingsForm, we don't accept it
throw new InvalidArgumentException( sfContext::getInstance()->
getI18N()->
__( 'The custom %action% form should be instance of %form%',
array( '%action%' => 'editEmail',
'%form%' => 'sfApplyEditEmailForm' ), 'sfForkedApply' )
);
}
if ($request->isMethod('post'))
{
$this->form->bind($request->getParameter( $this->form->getName() ));
if ($this->form->isValid())
{
$profile = $this->getUser()->getGuardUser()->getProfile();
$confirmation = sfConfig::get( 'app_sfForkedApply_confirmation' );
if( $confirmation['email'] )
{
$profile->setEmailNew( $this->form->getValue( 'email' ) );
$profile->setValidate('e' . self::createGuid());
$profile->save();
$this->mail(array('subject' => sfConfig::get('app_sfApplyPlugin_apply_subject',
sfContext::getInstance()->getI18N()->__("Please verify your email on %1%",
array('%1%' => $this->getRequest()->getHost()), 'sfForkedApply')),
'fullname' => $profile->getFullname(),
'email' => $profile->getEmail(),
'parameters' => array('username' => $profile->getUser()->getUsername(),
'validate' => $profile->getValidate(),
'oldmail' => $profile->getEmail(),
'newmail' => $profile->getEmailNew() ),
'text' => 'sfApply/sendValidateEmailText',
'html' => 'sfApply/sendValidateEmail'));
$this->getUser()->setFlash( 'sf_forked_apply',
sfContext::getInstance()->getI18N()->
__( 'To complete email change, follow a link included in a confirmation email we have sent to your old email address: %OLDEMAIL%.',
array( '%OLDEMAIL%' => $profile->getEmail() ), 'sfForkedApply' ) );
}
else
{
$profile->setEmail( $this->form->getValue( 'email' ) );
$profile->save();
$this->getUser()->setFlash( 'sf_forked_apply',
sfContext::getInstance()->getI18N()->
__( 'Your email has been changed.',
array(), 'sfForkedApply' ) );
}
return $this->redirect('@settings');
}
}
}
/**
* gets From information for email. may throw Exception.
* @return array
*/
protected function getFromAddress()
{
$from = sfConfig::get('app_sfApplyPlugin_from', false);
if (!$from)
{
throw new Exception('app_sfApplyPlugin_from is not set');
}
// i18n the full name
return array('email' => $from['email'], 'fullname' => sfContext::getInstance()->getI18N()->__($from['fullname']));
}
/**
* apply uses this. Password reset also uses it in the case of a user who
* was never verified to begin with.
* @param object $profile
*/
protected function sendVerificationMail( $profile )
{
$this->mail(array('subject' => sfConfig::get('app_sfApplyPlugin_apply_subject',
sfContext::getInstance()->getI18N()->__("Please verify your account on %1%",
array('%1%' => $this->getRequest()->getHost()), 'sfForkedApply')),
'fullname' => $profile->getFullname(),
'email' => $profile->getEmail(),
'parameters' => array('fullname' => $profile->getFullname(),
'validate' => $profile->getValidate()),
'text' => 'sfApply/sendValidateNewText',
'html' => 'sfApply/sendValidateNew'));
}
/**
* This function has been overriden. Original used Zend_Mail here. It's used
* to actually compose and send e-mail verification message.
* @param array $options
*/
protected function mail( $options )
{
//Checking for all required options
$required = array('subject', 'parameters', 'email', 'fullname', 'html', 'text');
foreach ($required as $option)
{
if (!isset($options[$option]))
{
throw new sfException("Required option $option not supplied to sfApply::mail");
}
}
$message = $this->getMailer()->compose();
$message->setSubject($options['subject']);
// Render message parts
$message->setBody($this->getPartial($options['html'], $options['parameters']), 'text/html');
$message->addPart($this->getPartial($options['text'], $options['parameters']), 'text/plain');
//getting information on sender (that's us). May be source of exception.
$address = $this->getFromAddress();
$message->setFrom(array($address['email'] => $address['fullname']));
$message->setTo(array($options['email'] => $options['fullname']));
//Sending email
$this->getMailer()->send($message);
}
/**
* Method that creates forms
* @param sfForm $formClass
* @param object $object
* @return class
*/
protected function newForm($formClass, $object = null)
{
$key = 'app_sfForkedApply_'.$formClass;
$class = sfConfig::get( $key );
if ($object !== null)
{
return new $class($object);
}
return new $class();
}
/**
* Method to activate user
* @param sfGuardUser $user
* @author Grzegorz Śliwiński
*/
protected function activateUser( sfGuardUser $user )
{
$user->setIsActive(true);
$user->save();
$this->getUser()->signIn($user);
}
static public function createGuid()
{
$guid = "";
// This was 16 before, which produced a string twice as
// long as desired. I could change the schema instead
// to accommodate a validation code twice as big, but
// that is completely unnecessary and would break
// the code of anyone upgrading from the 1.0 version.
// Ridiculously unpasteable validation URLs are a
// pet peeve of mine anyway.
for ($i = 0; ($i < 16); $i++)
{
$guid .= sprintf("%02x", mt_rand(0, 255));
}
return $guid;
}
//Returns validation type
static public function getValidationType($validate)
{
$t = substr($validate, 0, 1);
if( $t == 'n' )
{
return 'New';
}
elseif( $t == 'r' )
{
return 'Reset';
}
elseif( $t == 'e' )
{
return 'Email';
}
else
{
return sfView::NONE;
}
}
public function resetRequestBody( $user )
{
if (!$user)
{
return 'NoSuchUser';
}
$this->forward404Unless($user);
$profile = $user->getProfile();
if (!$user->getIsActive())
{
$type = $this->getValidationType($profile->getValidate());
if ($type === 'New')
{
try
{
$this->sendVerificationMail($profile);
}
catch (Exception $e)
{
//We rethrow exception for the dev environment. This catch
//catches other than mailer exception, i18n as well. So developer
//now knows what he's up to.
if( sfContext::getInstance()->getConfiguration()->getEnvironment() === 'dev' )
{
throw $e;
}
return 'UnverifiedMailerError';
}
return 'Unverified';
}
elseif ($type === 'Reset')
{
// They lost their first password reset email. That's OK. let them try again
}
else
{
return 'Locked';
}
}
$profile->setValidate('r' . self::createGuid());
$profile->save();
try
{
$this->mail(array('subject' => sfConfig::get('app_sfApplyPlugin_reset_subject',
sfContext::getInstance()->getI18N()
->__("Please verify your password reset request on %1%",
array('%1%' => $this->getRequest()->getHost()), 'sfForkedApply')),
'fullname' => $profile->getFullname(),
'email' => $profile->getEmail(),
'parameters' => array('fullname' => $profile->getFullname(),
'validate' => $profile->getValidate(), 'username' => $user->getUsername()),
'text' => 'sfApply/sendValidateResetText',
'html' => 'sfApply/sendValidateReset'));
}
catch (Exception $e)
{
//We rethrow exception for the dev environment. This catch
//catches other than mailer exception, i18n as well. So developer
//now knows what he's up to.
if( sfContext::getInstance()->getConfiguration()->getEnvironment() === 'dev' )
{
throw $e;
}
return 'MailerError';
}
return 'After';
}
}
?>