4 * Base actions for the sfGuardForgotPasswordPlugin sfGuardForgotPassword module.
6 * @package sfGuardForgotPasswordPlugin
7 * @subpackage sfGuardForgotPassword
8 * @author Your name here
9 * @version SVN: $Id: BaseActions.class.php 12534 2008-11-01 13:38:27Z Kris.Wallsmith $
11 abstract class BasesfGuardForgotPasswordActions extends sfActions
13 public function preExecute()
15 if ($this->getUser()->isAuthenticated())
17 $this->redirect('@homepage');
21 public function executeIndex($request)
23 $this->form = new sfGuardRequestForgotPasswordForm();
25 if ($request->isMethod('post'))
27 $this->form->bind($request->getParameter($this->form->getName()));
28 if ($this->form->isValid())
30 $this->user = $this->form->user;
31 $this->_deleteOldUserForgotPasswordRecords();
33 $forgotPassword = new sfGuardForgotPassword();
34 $forgotPassword->user_id = $this->form->user->id;
35 $forgotPassword->unique_key = md5(rand() + time());
36 $forgotPassword->expires_at = new Doctrine_Expression('NOW()');
37 $forgotPassword->save();
39 $message = Swift_Message::newInstance()
40 ->setFrom(sfConfig::get('app_sf_guard_plugin_default_from_email', 'from@noreply.com'))
41 ->setTo($this->form->user->email_address)
42 ->setSubject('Forgot Password Request for '.$this->form->user->username)
43 ->setBody($this->getPartial('sfGuardForgotPassword/send_request', array('user' => $this->form->user, 'forgot_password' => $forgotPassword)))
44 ->setContentType('text/html')
47 $this->getMailer()->send($message);
49 $this->getUser()->setFlash('notice', 'Check your e-mail! You should receive something shortly!');
50 $this->redirect('@sf_guard_signin');
52 $this->getUser()->setFlash('error', 'Invalid e-mail address!');
57 public function executeChange($request)
59 $this->forgotPassword = $this->getRoute()->getObject();
60 $this->user = $this->forgotPassword->User;
61 $this->form = new sfGuardChangeUserPasswordForm($this->user);
63 if ($request->isMethod('post'))
65 $this->form->bind($request->getParameter($this->form->getName()));
66 if ($this->form->isValid())
70 $this->_deleteOldUserForgotPasswordRecords();
72 $message = Swift_Message::newInstance()
73 ->setFrom(sfConfig::get('app_sf_guard_plugin_default_from_email', 'from@noreply.com'))
74 ->setTo($this->user->email_address)
75 ->setSubject('New Password for '.$this->user->username)
76 ->setBody($this->getPartial('sfGuardForgotPassword/new_password', array('user' => $this->user, 'password' => $request['sf_guard_user']['password'])))
79 $this->getMailer()->send($message);
81 $this->getUser()->setFlash('notice', 'Password updated successfully!');
82 $this->redirect('@sf_guard_signin');
87 private function _deleteOldUserForgotPasswordRecords()
89 Doctrine_Core::getTable('sfGuardForgotPassword')
92 ->where('p.user_id = ?', $this->user->id)