root/trunk/apps/main/modules/sfGuardAuth/actions/actions.class.php

Revision 54, 12.4 kB (checked in by nperriault, 2 years ago)

Moved signing and activation email resending forms, added use of sfContext::set() to store form instance which can now be accessible from templates, layout, partial and so on (useful for global error messages in the layout for example)

Line 
1 <?php
2 require_once dirname(__FILE__).'/../lib/BasesfGuardAuthActions.class.php';
3 /**
4  * sfGuardAuth plugin overriden actions
5  *
6  * @package    symfony
7  * @subpackage plugin
8  */
9 class sfGuardAuthActions extends BasesfGuardAuthActions
10 {
11
12   public function executeActivate(sfWebRequest $request)
13   {
14     /*
15      * $this->user and $this->activation has been set in validateActivate()
16      */
17     $this->user->setIsActive(true);
18     $this->user->save();
19     $this->activation->delete();
20     $this->purgePersonRelatedCache($this->user);
21     try
22     {
23       $this->getUser()->setFlash('notice', $this->__('Your account has been activated. You can now log in using the username and password you provided at registration time.'));
24       $this->getUser()->setAttribute('redirect_after_login', '@user_profile');
25       $this->redirect('@sf_guard_signin');
26     }
27     catch (sfStopException $e)
28     {
29       throw $e;
30     }
31     catch (Exception $e)
32     {
33       $request->setError('errors', $this->__('Unable to activate your account'));
34       sfContext::getInstance()->getLogger()->err('Blocking error at account activation time: '.$e->getMessage());
35       return sfView::SUCCESS;
36     }
37   }
38  
39   public function executeChangePassword(sfWebRequest $request)
40   {
41     if ($request->isMethod('post'))
42     {
43       $user = $this->getUser()->getGuardUser();
44       $password = $request->getParameter('new_password');
45       $user->setPassword($password);
46       $user->save();
47       $this->getUser()->setFlash('notice', $this->__('Your password has been changed'));
48       $this->redirect('@user_profile');
49     }
50   }
51  
52   public function executeDeleteAvatar(sfWebRequest $request)
53   {
54     $user = $this->getUser()->getGuardUser();
55     if (!$user || !$user->getAvatarPath())
56     {
57       return sfView::NONE;
58     }
59     $basedir = sfConfig::get('sf_upload_dir').DIRECTORY_SEPARATOR.sfConfig::get('app_people_upload_dir', 'people');
60     if (!ImagesTools::deleteImagesFromFormatParameters($user->getAvatarPath(), $basedir, sfConfig::get('app_people_formats')))
61     {
62       $user->setAvatarPath(null);
63       $user->save();
64       $this->purgePersonRelatedCache($user);
65       return $this->renderText($this->__('Picture deleted'));
66     }
67     else
68     {
69       sfContext::getInstance()->getLogger()->err('Unable to remove user avatar');
70       return $this->renderText($this->__('Picture deletion failed'));
71     }
72   }
73  
74   public function executeForgotPassword(sfWebRequest $request)
75   {
76     if ($request->isMethod('post'))
77     {
78       $password = substr(md5(rand(100000, 999999)), 0, 8);
79       $email = $request->getParameter('email');
80       $user = sfGuardUserPeer::retrieveByEmail($email);
81       $this->forward404Unless($user, sprintf('User not with email "%s" found', $email));
82       $user->setPassword($password);
83       $user->save();
84       
85       $request->setAttribute('user', $user);
86       $request->setAttribute('password', $password);
87       $mailSent = $this->sendSwiftSmtpPlainMail('mail', 'forgotPassword',
88                                                 $user->getEmail(),
89                                                 $this->__('Your new requested password'));
90       if (!$mailSent)
91       {
92         $request->setError('errors', $this->__('We were not able to send you an email. Please try again later.'));
93         return sfView::SUCCESS;
94       }
95       $this->getUser()->setFlash('notice', $this->__('A new password has been emailed to %email%',
96                                           array('%email%' => $email)));
97       $this->redirect('@sf_guard_signin');
98     }
99   }
100
101   public function executeProfile(sfWebRequest $request)
102   {
103     $this->embedGoogleJavascriptApi();
104     
105     $this->user = $this->getUser()->getGuardUser();
106
107     if ($request->getMethod() != sfRequest::POST)
108     {
109       return sfView::SUCCESS;
110     }
111     
112     $profile = $request->getParameter('profile', array());
113     
114     # 1. Boolean values
115     if (!isset($profile['allow_contact']))
116     {
117       $profile['allow_contact'] = false;
118     }
119     if (!isset($profile['allow_localization']))
120     {
121       $profile['allow_localization'] = false;
122     }
123     if (!isset($profile['is_for_hire']))
124     {
125       $profile['is_for_hire'] = false;
126     }
127     
128     # 2. Dates
129     if (!$profile['birthdate'])
130     {
131       unset($profile['birthdate']);
132     }
133     if (!$profile['php_at'])
134     {
135       unset($profile['php_at']);
136     }
137     if (!$profile['symfony_at'])
138     {
139       unset($profile['symfony_at']);
140     }
141     
142     try
143     {
144       $thumb = ImagesTools::createThumbs (
145         'profile[avatar_path]',
146         sfConfig::get('sf_upload_dir').DIRECTORY_SEPARATOR.sfConfig::get('app_people_upload_dir', 'people'),
147         md5($this->user->getUsername()).'.jpg',
148         sfConfig::get('app_people_formats')
149       );
150       if ($thumb)
151       {
152         $profile['avatar_path'] = $thumb;
153       }
154       $this->user->fromArray($profile, BasePeer::TYPE_FIELDNAME);
155       $localization = $this->user->getLocalization();
156       if ($profile['latitude'] && $profile['longitude'])
157       {
158         $localization->setLatitude($profile['latitude']);
159         $localization->setLongitude($profile['longitude']);
160         $localization->save();
161         unset($profile['latitude']);
162         unset($profile['longitude']);
163       }
164       else
165       {
166         $this->user->updateLocalization();
167       }
168       $this->user->save();
169       $this->purgePersonRelatedCache($this->user);
170       $this->getUser()->setFlash('notice', $this->__('Your profile has been updated'));
171       $this->redirect('@user_profile');
172     }
173     catch (sfStopException $e)
174     {
175       throw $e;
176     }
177     catch (Exception $e)
178     {
179       $request->setError('errors', $this->__('Unable to save your profile'));
180       sfContext::getInstance()->getLogger()->err('Blocking error at profile save time: '.$e->getMessage());
181       return sfView::SUCCESS;
182     }
183   }
184
185   public function executeSkills(sfWebRequest $request)
186   {
187     $this->user = $this->getUser()->getGuardUser();
188     $this->user_skills = $this->user->getSkills();
189     $this->skill_tags = sfConfig::get('app_people_skill_tags', array());
190     asort($this->skill_tags);
191     if ($request->isMethod('post'))
192     {
193       $user_skills = $request->getParameter('skills', array());
194       $this->user->removeAllTags();
195       $this->user->save();
196       foreach ($user_skills as $skill)
197       {
198         if (!in_array($skill, $this->skill_tags))
199         {
200           $this->logMessage(sprintf('Skill "%s" is not an allowed one (submitted by user "%s")',
201                                     $skill, $this->user->getUsername()));
202           continue;
203         }
204         $this->user->addTag($skill);
205       }
206       $this->user->save();
207       $this->getUser()->setFlash('notice', $this->__('Your skills have been updated'));
208       $this->redirect('@user_profile_skills');
209     }
210   }
211  
212   public function handleErrorProfile()
213   {
214     $this->embedGoogleJavascriptApi();
215     $this->user = $this->getUser()->getGuardUser();
216     return sfView::SUCCESS;
217   }
218  
219   public function handleErrorForgotPassword()
220   {
221     return sfView::SUCCESS;
222   }
223  
224   public function executeRegister(sfWebRequest $request)
225   {
226     if ($this->getUser()->isAuthenticated())
227     {
228       $this->redirect('@homepage');
229     }
230     
231     $this->form = new RegisterForm();
232     
233     // Recaptcha parameters
234     $captcha = array(
235       'recaptcha_challenge_field' => $request->getParameter('recaptcha_challenge_field'),
236       'recaptcha_response_field'  => $request->getParameter('recaptcha_response_field'),
237     );
238     $params = array_merge($request->getParameter('user', array()), array('captcha' => $captcha));
239     
240     if (!$request->isMethod('post') or !$this->form->bindAndSave($params))
241     {
242       return sfView::SUCCESS; // redisplay form with errors
243     }
244     
245     // At this point we got a valid form and a created sfGuardUser object
246     $user = $this->form->getObject();
247     
248     // Create activation entry
249     $activation = new Activation();
250     $activation->setUserId($user->getId());
251     $activation->setHash(md5(rand(100000, 999999)));
252     $activation->save();
253     
254     // Send user an activation email
255     $request->setAttribute('user', $user);
256     $request->setAttribute('activation', $activation);
257     $mailSent = $this->sendSwiftSmtpPlainMail('mail', 'register',
258                                               $user->getEmail(),
259                                               $this->__('Please confirm your Symfonians account creation request'));
260     
261     // If mail sending failed,
262     if (!$mailSent)
263     {
264       $request->setError('errors', $this->__('We were unable to send you an activation email. Registration process failed.'));
265       $activation->delete();
266       $user->delete();
267       return sfView::SUCCESS;
268     }
269     $this->getUser()->setFlash('notice', $this->__('A confirmation mail has been sent to %mail%',
270                                                    array('%mail%' => $user->getEmail())));
271     $this->redirect('@user_register_done');
272   }
273  
274   public function executeRegisterDone(sfWebRequest $request)
275   {
276     $this->user = $request->getAttribute('user');
277     return sfView::SUCCESS;
278   }
279  
280   public function executeResendActivation(sfWebRequest $request)
281   {
282     $this->form = new ResendActivationForm();
283     
284     if ($request->isMethod('post'))
285     {
286       // Recaptcha parameters
287       $captcha = array(
288         'recaptcha_challenge_field' => $request->getParameter('recaptcha_challenge_field'),
289         'recaptcha_response_field'  => $request->getParameter('recaptcha_response_field'),
290       );
291       $params = array_merge($request->getParameter('user', array()), array('captcha' => $captcha));
292       
293       $this->form->bind($params);
294       
295       if ($this->form->isValid())
296       {
297         $user = sfGuardUserPeer::retrieveByEmail($params['email']);
298         $this->forward404Unless($user); // theoricaly, can't happen
299         
300         $c = new Criteria();
301         $c->add(ActivationPeer::USER_ID, $user->getId());
302         $activation = ActivationPeer::doSelectOne($c);
303         $request->setAttribute('activation', $activation);
304         
305         $mailSent = $this->sendSwiftSmtpPlainMail('mail', 'resendActivation',
306                                                   $user->getEmail(),
307                                                   $this->__('Please activate your account'));
308         if (!$mailSent)
309         {
310           $this->getUser()->setFlash('warning', $this->__('We were unable to send you an email. We are digging the problem, stay tuned.'));
311           return sfView::SUCCESS;
312         }
313         $this->getUser()->setFlash('notice', $this->__('Your activation mail has been sent to '.$user->getEmail()));
314         $this->redirect('@homepage');
315       }
316     }
317   }
318  
319   public function handleErrorChangePassword()
320   {
321     return sfView::SUCCESS;
322   }
323  
324   public function handleErrorRegister()
325   {
326     return sfView::SUCCESS;
327   }
328  
329   public function validateActivate()
330   {
331     $key = $this->getRequestParameter('key');
332     $c = new Criteria();
333     $c->add(ActivationPeer::HASH, $key);
334     $c->addJoin(ActivationPeer::USER_ID, sfGuardUserPeer::ID);
335     
336     $this->activation = ActivationPeer::doSelectOne($c);
337     if (!$this->activation)
338     {
339       $request->setError('errors', $this->__('Unable to find this activation key: '.$key));
340       return false;
341     }
342     
343     $this->user = $this->activation->getsfGuardUser();
344     if (!$this->user)
345     {
346       $this->getRequest()->setError('errors', $this->__('Unable to find user to activate'));
347       return false;
348     }
349     
350     if ($this->user->getIsActive())
351     {
352       $this->getRequest()->setError('errors', $this->__('Related account has already been activated'));
353       return false;
354     }
355     
356     return true;
357   }
358  
359   public function validateChangePassword()
360   {
361     if ($this->getRequest()->isMethod('post'))
362     {
363       $user = $this->getUser()->getGuardUser();
364       if (!$user->checkPassword($this->getRequestParameter('current_password')))
365       {
366         $request->setError('errors', $this->__('You did not provide your current password correctly!'));
367         return false;
368       }
369       return true;
370     }
371   }
372  
373   public function validateForgotPassword()
374   {
375     if ($this->getRequest()->isMethod('post'))
376     {
377       $user = sfGuardUserPeer::retrieveByEmail(trim($this->getRequestParameter('email')));
378       if (!$user)
379       {
380         $this->getRequest()->setError('errors', $this->__('This email has never been registered here'));
381         return false;
382       }
383       if (!$user->getIsActive())
384       {
385         $this->getRequest()->setError('errors', $this->__('The related account is currently not activated'));
386         return false;
387       }
388     }
389     return true;
390   }
391  
392 }
393
Note: See TracBrowser for help on using the browser.