6765a3b83460b8b79eac73e450e6b04932151955
[mobi/.git] /
1 <?php
2
3 /**
4  * Base actions for the sfGuardForgotPasswordPlugin sfGuardForgotPassword module.
5  * 
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 $
10  */
11 abstract class BasesfGuardForgotPasswordActions extends sfActions
12 {
13   public function preExecute()
14   {
15     if ($this->getUser()->isAuthenticated())
16     {
17       $this->redirect('@homepage');
18     }
19   }
20
21   public function executeIndex($request)
22   {
23     $this->form = new sfGuardRequestForgotPasswordForm();
24
25     if ($request->isMethod('post'))
26     {
27       $this->form->bind($request->getParameter($this->form->getName()));
28       if ($this->form->isValid())
29       {
30         $this->user = $this->form->user;
31         $this->_deleteOldUserForgotPasswordRecords();
32
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();
38
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')
45         ;
46
47         $this->getMailer()->send($message);
48
49         $this->getUser()->setFlash('notice', 'Check your e-mail! You should receive something shortly!');
50         $this->redirect('@sf_guard_signin');
51       } else {
52         $this->getUser()->setFlash('error', 'Invalid e-mail address!');
53       }
54     }
55   }
56
57   public function executeChange($request)
58   {
59     $this->forgotPassword = $this->getRoute()->getObject();
60     $this->user = $this->forgotPassword->User;
61     $this->form = new sfGuardChangeUserPasswordForm($this->user);
62
63     if ($request->isMethod('post'))
64     {
65       $this->form->bind($request->getParameter($this->form->getName()));
66       if ($this->form->isValid())
67       {
68         $this->form->save();
69
70         $this->_deleteOldUserForgotPasswordRecords();
71
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'])))
77         ;
78
79         $this->getMailer()->send($message);
80
81         $this->getUser()->setFlash('notice', 'Password updated successfully!');
82         $this->redirect('@sf_guard_signin');
83       }
84     }
85   }
86
87   private function _deleteOldUserForgotPasswordRecords()
88   {
89     Doctrine_Core::getTable('sfGuardForgotPassword')
90       ->createQuery('p')
91       ->delete()
92       ->where('p.user_id = ?', $this->user->id)
93       ->execute();
94   }
95 }