Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. public function successOAuthCallback($client)
  2. {
  3. // use BaseClient::normalizeUserAttributeMap to provide consistency for user attribute`s names
  4. $attributes = $client->getUserAttributes();
  5. $user = User::find()->where([
  6. 'oauth_client'=>$client->getName(),
  7. 'oauth_client_user_id'=>ArrayHelper::getValue($attributes, 'id')
  8. ])
  9. ->one();
  10. if (!$user) {
  11. $user = new User();
  12. $user->scenario = 'oauth_create';
  13. $user->username = ArrayHelper::getValue($attributes, 'first_name');
  14. $user->email = ArrayHelper::getValue($attributes, 'email');
  15. $user->oauth_client = $client->getName();
  16. $user->oauth_client_user_id = ArrayHelper::getValue($attributes, 'id');
  17. $password = Yii::$app->security->generateRandomString(8);
  18. $user->setPassword($password);
  19. if ($user->save()) {
  20. $profileData = [];
  21. if ($client->getName() === 'facebook') {
  22. $profileData['firstname'] = ArrayHelper::getValue($attributes, 'first_name');
  23. $profileData['lastname'] = ArrayHelper::getValue($attributes, 'last_name');
  24. }
  25. $user->afterSignup($profileData);
  26. $token = UserToken::create(
  27. $user->id,
  28. UserToken::TYPE_ACTIVATION,
  29. Time::SECONDS_IN_A_DAY
  30. );
  31. $sentSuccess = Yii::$app->commandBus->handle(new SendEmailCommand([
  32. 'view' => 'activation',
  33. 'params' => [
  34. 'user'=>$user,
  35. 'password'=>$password,
  36. 'url' => Url::to(['/user/sign-in/activation', 'token' => $token->token], true)
  37. ],
  38. 'subject' => Yii::t('frontend', '{app-name} | Activation link', ['app-name'=>Yii::$app->name]),
  39. 'to' => $user->email
  40. ]));
  41. $sentSuccess1 = Yii::$app->commandBus->handle(new SendEmailCommand([
  42. 'view' => 'oauth_welcome',
  43. 'params' => [
  44. 'user'=>$user,
  45. 'password'=>$password,
  46. ],
  47. 'subject' => Yii::t('frontend', '{app-name} | Login information', ['app-name'=>Yii::$app->name]),
  48. 'to' => $user->email
  49. ]));
  50. if ($sentSuccess && $sentSuccess1) {
  51. Yii::$app->session->setFlash(
  52. 'alert',
  53. [
  54. 'options'=>['class'=>'alert-success'],
  55. 'body'=>Yii::t('frontend', 'Welcome to {app-name}. Email with activation link and login information was sent to your email.', [
  56. 'app-name'=>Yii::$app->name
  57. ])
  58. ]
  59. );
  60. }
  61.  
  62. } else {
  63. // We already have a user with this email. Do what you want in such case
  64. if ($user->email && User::find()->where(['email'=>$user->email])->count()) {
  65. Yii::$app->session->setFlash(
  66. 'alert',
  67. [
  68. 'options'=>['class'=>'alert-danger'],
  69. 'body'=>Yii::t('frontend', 'We already have a user with email {email}', [
  70. 'email'=>$user->email
  71. ])
  72. ]
  73. );
  74. } else {
  75. Yii::$app->session->setFlash(
  76. 'alert',
  77. [
  78. 'options'=>['class'=>'alert-danger'],
  79. 'body'=>Yii::t('frontend', 'Error while oauth process.')
  80. ]
  81. );
  82. }
  83.  
  84. };
  85. }
  86. if (Yii::$app->user->login($user, 3600 * 24 * 30)) {
  87. return true;
  88. } else {
  89. throw new Exception('OAuth error');
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement