PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/registration/controllers/YumRegistrationController.php

https://github.com/ladzzzz123/yii-user-management
PHP | 238 lines | 173 code | 34 blank | 31 comment | 25 complexity | 26cbaac663a1b11f464f22c7d1d7726f MD5 | raw file
  1. <?
  2. /* This file handles a example registration process logic and some of the
  3. * most used functions for Registration and Activation. It is recommended to
  4. * extend from this class and implement your own, project-specific
  5. * Registration process. If this example does exactly what you want in your
  6. * Project, then you can feel lucky already! */
  7. Yii::import('application.modules.user.controllers.YumController');
  8. Yii::import('application.modules.user.models.*');
  9. Yii::import('application.modules.profile.models.*');
  10. Yii::import('application.modules.registration.models.*');
  11. class YumRegistrationController extends YumController {
  12. public $defaultAction = 'registration';
  13. // Only allow the registration if the user is not already logged in and
  14. // the function is activated in the Module Configuration
  15. public function beforeAction($action) {
  16. if(!Yum::hasModule('registration'))
  17. throw new CHttpException(401, 'Please activate the registration submodule in your config/main.php. See the installation instructions or registration/RegistrationModule.php for details');
  18. if(!Yii::app()->user->isGuest)
  19. $this->redirect(Yii::app()->user->returnUrl);
  20. $this->layout = Yum::module('registration')->layout;
  21. return parent::beforeAction($action);
  22. }
  23. public function accessRules() {
  24. return array(
  25. array('allow',
  26. 'actions' => array('index', 'registration', 'recovery', 'activation', 'resendactivation'),
  27. 'users' => array('*'),
  28. ),
  29. array('allow',
  30. 'actions' => array('captcha'),
  31. 'users' => array('*'),
  32. ),
  33. array('deny', // deny all other users
  34. 'users' => array('*'),
  35. ),
  36. );
  37. }
  38. public function actions() {
  39. return array(
  40. 'captcha' => array(
  41. 'class' => 'CCaptchaAction',
  42. 'backColor' => 0xFFFFFF,
  43. ),
  44. );
  45. }
  46. /*
  47. * an Example implementation of an registration of an new User in the system.
  48. *
  49. * please see the documentation of yii-user-management for examples on how to
  50. * extend from this class and implement your own registration logic in
  51. * user/docs/registration.txt
  52. */
  53. public function actionRegistration() {
  54. // When we overrie the registrationUrl, this one is not valid anymore!
  55. if(Yum::module('registration')->registrationUrl != array(
  56. '//registration/registration/registration'))
  57. throw new CHttpException(403);
  58. Yii::import('application.modules.profile.models.*');
  59. $form = new YumRegistrationForm;
  60. $profile = new YumProfile;
  61. $this->performAjaxValidation('YumRegistrationForm', $form);
  62. if (isset($_POST['YumRegistrationForm'])) {
  63. $form->attributes = $_POST['YumRegistrationForm'];
  64. $profile->attributes = $_POST['YumProfile'];
  65. $form->validate();
  66. $profile->validate();
  67. if(!$form->hasErrors() && !$profile->hasErrors()) {
  68. $user = new YumUser;
  69. $user->register($form->username, $form->password, $profile->email);
  70. $profile->user_id = $user->id;
  71. $profile->save();
  72. $this->sendRegistrationEmail($user);
  73. Yum::setFlash('Thank you for your registration. Please check your email.');
  74. $this->redirect(Yum::module()->loginUrl);
  75. }
  76. }
  77. $this->render(Yum::module()->registrationView, array(
  78. 'form' => $form,
  79. 'profile' => $profile,
  80. )
  81. );
  82. }
  83. // Send the Email to the given user object. $user->email needs to be set.
  84. public function sendRegistrationEmail($user) {
  85. if (!isset($user->profile->email))
  86. throw new CException(Yum::t('Email is not set when trying to send Registration Email'));
  87. $activation_url = $user->getActivationUrl();
  88. $body = strtr(
  89. 'Hello, {username}. Please activate your account with this url: {activation_url}', array(
  90. '{username}' => $user->username,
  91. '{activation_url}' => $activation_url));
  92. $mail = array(
  93. 'from' => Yum::module('registration')->registrationEmail,
  94. 'to' => $user->profile->email,
  95. 'subject' => strtr(
  96. 'Please activate your account for {username}', array(
  97. '{username}' => $user->username)),
  98. 'body' => $body,
  99. );
  100. $sent = YumMailer::send($mail);
  101. return $sent;
  102. }
  103. /**
  104. * Activation of an user account. The Email and the Activation key send
  105. * by email needs to correct in order to continue. The Status will
  106. * be initially set to 1 (active - first Visit) so the administrator
  107. * can see, which accounts have been activated, but not yet logged in
  108. * (more than once)
  109. */
  110. public function actionActivation($email, $key) {
  111. // If already logged in, we dont activate anymore
  112. if (!Yii::app()->user->isGuest) {
  113. Yum::setFlash('You are already logged in, please log out to activate your account');
  114. $this->redirect(Yii::app()->user->returnUrl);
  115. }
  116. // If everything is set properly, let the model handle the Validation
  117. // and do the Activation
  118. $status = YumUser::activate($email, $key);
  119. if($status instanceof YumUser) {
  120. if(Yum::module('registration')->loginAfterSuccessfulActivation) {
  121. $login = new YumUserIdentity($status->username, false);
  122. $login->authenticate(true);
  123. Yii::app()->user->login($login);
  124. }
  125. $this->render(Yum::module('registration')->activationSuccessView);
  126. }
  127. else
  128. $this->render(Yum::module('registration')->activationFailureView, array(
  129. 'error' => $status));
  130. }
  131. /**
  132. * Password recovery routine. The User will receive an email with an
  133. * activation link. If clicked, he will be prompted to enter his new
  134. * password.
  135. */
  136. public function actionRecovery($email = null, $key = null) {
  137. $form = new YumPasswordRecoveryForm;
  138. if ($email != null && $key != null) {
  139. if($profile = YumProfile::model()->find('email = :email', array(
  140. 'email' => $email))) {
  141. $user = $profile->user;
  142. if($user->activationKey == $key) {
  143. $passwordform = new YumUserChangePassword;
  144. if (isset($_POST['YumUserChangePassword'])) {
  145. $passwordform->attributes = $_POST['YumUserChangePassword'];
  146. if ($passwordform->validate()) {
  147. $user->password = YumEncrypt::encrypt($passwordform->password, $user->salt);
  148. $user->activationKey = YumEncrypt::encrypt(microtime() . $passwordform->password, $user->salt);
  149. $user->save();
  150. Yum::setFlash('Your new password has been saved.');
  151. $this->redirect(Yum::module()->loginUrl);
  152. }
  153. }
  154. $this->render(
  155. Yum::module('registration')->changePasswordView, array(
  156. 'form' => $passwordform));
  157. Yii::app()->end();
  158. } else {
  159. $form->addError('login_or_email', Yum::t('Invalid recovery key'));
  160. Yum::log(Yum::t(
  161. 'Someone tried to recover a password, but entered a wrong recovery key. Email is {email}, associated user is {username} (id: {uid})', array(
  162. '{email}' => $email,
  163. '{uid}' => $user->id,
  164. '{username}' => $user->username)));
  165. }
  166. }
  167. } else {
  168. if (isset($_POST['YumPasswordRecoveryForm'])) {
  169. $form->attributes = $_POST['YumPasswordRecoveryForm'];
  170. if ($form->validate()) {
  171. Yum::setFlash(
  172. 'Instructions have been sent to you. Please check your email.');
  173. if($form->user instanceof YumUser) {
  174. $form->user->generateActivationKey();
  175. $recovery_url = $this->createAbsoluteUrl(
  176. Yum::module('registration')->recoveryUrl[0], array(
  177. 'key' => $form->user->activationKey,
  178. 'email' => $form->user->profile->email));
  179. Yum::log(Yum::t(
  180. '{username} successfully requested a new password in the password recovery form. A email with the password recovery url {recovery_url} has been sent to {email}', array(
  181. '{email}' => $form->user->profile->email,
  182. '{recovery_url}' => $recovery_url,
  183. '{username}' => $form->user->username)));
  184. $mail = array(
  185. 'from' => Yii::app()->params['adminEmail'],
  186. 'to' => $form->user->profile->email,
  187. 'subject' => 'You requested a new password',
  188. 'body' => strtr(
  189. 'You have requested a new password. Please use this URL to continue: {recovery_url}', array(
  190. '{recovery_url}' => $recovery_url)),
  191. );
  192. $sent = YumMailer::send($mail);
  193. } else
  194. Yum::log(Yum::t(
  195. 'A password has been requested, but no associated user was found in the database. Requested user/email is: {username}', array(
  196. '{username}' => $form->login_or_email)));
  197. $this->redirect(Yum::module()->loginUrl);
  198. }
  199. }
  200. }
  201. $this->render(Yum::module('registration')->recoverPasswordView, array(
  202. 'form' => $form));
  203. }
  204. }