/protected/modules/social/controllers/UserController.php

https://gitlab.com/RonLab1987/YupePlusClear · PHP · 262 lines · 198 code · 60 blank · 4 comment · 30 complexity · c4445e99361415f233da3729695054c5 MD5 · raw file

  1. <?php
  2. namespace application\modules\social\controllers;
  3. use application\modules\social\components\UserIdentity;
  4. use application\modules\social\models\SocialUser;
  5. use CHttpException;
  6. use EAuthException;
  7. use LoginForm;
  8. use RegistrationForm;
  9. use User;
  10. use Yii;
  11. use yupe\widgets\YFlashMessages;
  12. class UserController extends \yupe\components\controllers\FrontController
  13. {
  14. protected $service;
  15. public function actions()
  16. {
  17. return [
  18. 'captcha' => [
  19. 'class' => 'yupe\components\actions\YCaptchaAction',
  20. 'backColor' => 0xFFFFFF,
  21. 'testLimit' => 1,
  22. 'minLength' => Yii::app()->getModule('user')->minCaptchaLength,
  23. ],
  24. ];
  25. }
  26. protected function beforeAction($action)
  27. {
  28. $id = Yii::app()->getRequest()->getQuery('service');
  29. $this->service = Yii::app()->getComponent('eauth')->getIdentity($id);
  30. return parent::beforeAction($action);
  31. }
  32. /**
  33. * @var $service \IAuthService
  34. */
  35. public function actionLogin()
  36. {
  37. try {
  38. if ($this->service->authenticate()) {
  39. $identity = new UserIdentity($this->service);
  40. if ($identity->authenticate() && Yii::app()->getUser()->login($identity)) {
  41. Yii::app()->getUser()->setFlash(
  42. YFlashMessages::SUCCESS_MESSAGE,
  43. Yii::t('SocialModule.social', 'You successfully logged in!')
  44. );
  45. $module = Yii::app()->getModule('user');
  46. $redirect = (Yii::app()->getUser()->isSuperUser() && $module->loginAdminSuccess)
  47. ? [$module->loginAdminSuccess]
  48. : [$module->loginSuccess];
  49. Yii::app()->authenticationManager->setBadLoginCount(Yii::app()->getUser(), 0);
  50. $this->redirect(Yii::app()->getUser()->getReturnUrl($redirect));
  51. }
  52. /* @var $user User */
  53. if ($this->service->hasAttribute('email') &&
  54. ($user = Yii::app()->userManager->findUserByEmail($this->service->email))
  55. ) {
  56. if ($user->status == User::STATUS_NOT_ACTIVE) {
  57. Yii::app()->getUser()->setFlash(
  58. YFlashMessages::INFO_MESSAGE,
  59. Yii::t(
  60. 'SocialModule.social',
  61. 'You need to activate your account. Check your email.'
  62. )
  63. );
  64. $this->redirect(['/user/account/login']);
  65. }
  66. if ($user->status == User::STATUS_BLOCK) {
  67. Yii::app()->getUser()->setFlash(
  68. YFlashMessages::WARNING_MESSAGE,
  69. Yii::t(
  70. 'SocialModule.social',
  71. 'You account is blocked.'
  72. )
  73. );
  74. $this->redirect(['/user/account/login']);
  75. }
  76. if ($user->status == User::STATUS_ACTIVE) {
  77. Yii::app()->getUser()->setFlash(
  78. YFlashMessages::INFO_MESSAGE,
  79. Yii::t(
  80. 'SocialModule.social',
  81. 'Account with this email address already exists! Please, login if you want to join this social network to your account.'
  82. )
  83. );
  84. $this->redirect(['/social/connect', 'service' => $this->service->getServiceName()]);
  85. }
  86. }
  87. Yii::app()->getUser()->setFlash(
  88. YFlashMessages::SUCCESS_MESSAGE,
  89. Yii::t(
  90. 'SocialModule.social',
  91. 'You\'ve successfully logged in, please complete the registration!'
  92. )
  93. );
  94. $this->redirect(['/social/register', 'service' => $this->service->getServiceName()]);
  95. }
  96. $this->redirect('/login');
  97. } catch (EAuthException $e) {
  98. Yii::app()->getUser()->setFlash('error', 'EAuthException: ' . $e->getMessage());
  99. $this->redirect('/login');
  100. }
  101. }
  102. public function actionRegister()
  103. {
  104. $authData = $this->service->getAuthData();
  105. if (null === $authData || Yii::app()->getUser()->isAuthenticated()) {
  106. $this->redirect(Yii::app()->getUser()->returnUrl);
  107. }
  108. $module = Yii::app()->getModule('user');
  109. if ($module->registrationDisabled) {
  110. throw new CHttpException(404, Yii::t('SocialModule.social', 'Page not found!'));
  111. }
  112. $form = new RegistrationForm();
  113. if (isset($authData['email'])) {
  114. $form->email = $authData['email'];
  115. }
  116. $form->disableCaptcha = true;
  117. if (Yii::app()->getRequest()->getIsPostRequest() && !empty($_POST['RegistrationForm'])) {
  118. $form->setAttributes(Yii::app()->getRequest()->getPost('RegistrationForm'));
  119. if (!isset($authData['email']) && Yii::app()->userManager->isUserExist($form->email)) {
  120. Yii::app()->getUser()->setFlash(
  121. YFlashMessages::INFO_MESSAGE,
  122. Yii::t(
  123. 'SocialModule.social',
  124. 'Account with this email address already exists! Please, login if you want to join this social network to your account.'
  125. )
  126. );
  127. $this->redirect(['/social/connect', 'service' => $this->service->getServiceName()]);
  128. }
  129. $password = Yii::app()->userManager->hasher->generateRandomPassword();
  130. $form->setAttributes(
  131. [
  132. 'password' => $password,
  133. 'cPassword' => $password,
  134. 'verifyCode' => null
  135. ]
  136. );
  137. if ($form->validate()) {
  138. if ($user = Yii::app()->userManager->createUser($form)) {
  139. $social = new SocialUser();
  140. $social->user_id = $user->id;
  141. $social->provider = $authData['service'];
  142. $social->uid = $authData['uid'];
  143. if ($social->save()) {
  144. Yii::app()->getUser()->setFlash(
  145. YFlashMessages::SUCCESS_MESSAGE,
  146. Yii::t(
  147. 'SocialModule.social',
  148. 'Registration is successful!'
  149. )
  150. );
  151. $this->redirect([$module->registrationSuccess]);
  152. }
  153. }
  154. }
  155. $form->addError('', Yii::t('SocialModule.social', 'Error!'));
  156. }
  157. $this->render('register', ['model' => $form, 'module' => $module]);
  158. }
  159. public function actionConnect()
  160. {
  161. if (Yii::app()->getUser()->isAuthenticated()) {
  162. $this->redirect(Yii::app()->getUser()->returnUrl);
  163. }
  164. $authData = $this->service->getAuthData();
  165. $badLoginCount = Yii::app()->authenticationManager->getBadLoginCount(Yii::app()->getUser());
  166. $scenario = $badLoginCount > 3 ? 'loginLimit' : '';
  167. $form = new LoginForm($scenario);
  168. if (Yii::app()->getRequest()->getIsPostRequest() && !empty($_POST['LoginForm'])) {
  169. $form->setAttributes(Yii::app()->getRequest()->getPost('LoginForm'));
  170. if ($form->validate() && Yii::app()->authenticationManager->login(
  171. $form,
  172. Yii::app()->getUser(),
  173. Yii::app()->getRequest()
  174. )
  175. ) {
  176. $social = new SocialUser();
  177. $social->user_id = Yii::app()->getUser()->getId();
  178. $social->provider = $authData['service'];
  179. $social->uid = $authData['uid'];
  180. if ($social->save()) {
  181. Yii::app()->getUser()->setFlash(
  182. YFlashMessages::SUCCESS_MESSAGE,
  183. Yii::t(
  184. 'SocialModule.social',
  185. 'Social network successfully attached to your account, you can use it to log in now.'
  186. )
  187. );
  188. $module = Yii::app()->getModule('user');
  189. $redirect = (Yii::app()->getUser()->isSuperUser() && $module->loginAdminSuccess)
  190. ? [$module->loginAdminSuccess]
  191. : [$module->loginSuccess];
  192. Yii::app()->authenticationManager->setBadLoginCount(Yii::app()->getUser(), 0);
  193. $this->redirect(Yii::app()->getUser()->getReturnUrl($redirect));
  194. }
  195. } else {
  196. $form->addError('hash', Yii::t('SocialModule.social', 'Wrong email or password!'));
  197. Yii::app()->authenticationManager->setBadLoginCount(Yii::app()->getUser(), $badLoginCount + 1);
  198. }
  199. }
  200. $this->render('connect', ['authData' => $authData, 'model' => $form]);
  201. }
  202. }