Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. namespace frontend\components\auth;
  5.  
  6.  
  7. /**
  8. * AuthHandler handles successful authentication via Yii auth component
  9. */
  10. class AuthHandler
  11. {
  12. /**
  13. * @var ClientInterface
  14. */
  15. private $client;
  16.  
  17. public function __construct(ClientInterface $client)
  18. {
  19. $this->client = $client;
  20. }
  21.  
  22. public function handle()
  23. {
  24. $attributes = $this->client->getUserAttributes();
  25. $email = ArrayHelper::getValue($attributes, 'email');
  26. $id = ArrayHelper::getValue($attributes, 'id');
  27. $nickname = ArrayHelper::getValue($attributes, 'login');
  28.  
  29. /* @var Auth $auth */
  30. $auth = Auth::find()->where([
  31. 'source' => $this->client->getId(),
  32. 'source_id' => $id,
  33. ])->one();
  34.  
  35. if (Yii::$app->user->isGuest) {
  36. if ($auth) { // login
  37. /* @var User $user */
  38. $user = $auth->user;
  39. $this->updateUserInfo($user);
  40. Yii::$app->user->login($user, Yii::$app->params['user.rememberMeDuration']);
  41. } else { // signup
  42. if ($email !== null && User::find()->where(['email' => $email])->exists()) {
  43. Yii::$app->getSession()->setFlash('error', [
  44. Yii::t('app', "User with the same email as in {client} account already exists but isn't linked to it. Login using email first to link it.", ['client' => $this->client->getTitle()]),
  45. ]);
  46. } else {
  47. $password = Yii::$app->security->generateRandomString(6);
  48. $user = new User([
  49. 'username' => $nickname,
  50. 'github' => $nickname,
  51. 'email' => $email,
  52. 'password' => $password,
  53. ]);
  54. $user->generateAuthKey();
  55. $user->generatePasswordResetToken();
  56.  
  57. $transaction = User::getDb()->beginTransaction();
  58.  
  59. if ($user->save()) {
  60. $auth = new Auth([
  61. 'user_id' => $user->id,
  62. 'source' => $this->client->getId(),
  63. 'source_id' => (string)$id,
  64. ]);
  65. if ($auth->save()) {
  66. $transaction->commit();
  67. Yii::$app->user->login($user, Yii::$app->params['user.rememberMeDuration']);
  68. } else {
  69. Yii::$app->getSession()->setFlash('error', [
  70. Yii::t('app', 'Unable to save {client} account: {errors}', [
  71. 'client' => $this->client->getTitle(),
  72. 'errors' => json_encode($auth->getErrors()),
  73. ]),
  74. ]);
  75. }
  76. } else {
  77. Yii::$app->getSession()->setFlash('error', [
  78. Yii::t('app', 'Unable to save user: {errors}', [
  79. 'client' => $this->client->getTitle(),
  80. 'errors' => json_encode($user->getErrors()),
  81. ]),
  82. ]);
  83. }
  84. }
  85. }
  86. } else { // user already logged in
  87. if (!$auth) { // add auth provider
  88. $auth = new Auth([
  89. 'user_id' => Yii::$app->user->id,
  90. 'source' => $this->client->getId(),
  91. 'source_id' => (string)$attributes['id'],
  92. ]);
  93. if ($auth->save()) {
  94. /** @var User $user */
  95. $user = $auth->user;
  96. $this->updateUserInfo($user);
  97. Yii::$app->getSession()->setFlash('success', [
  98. Yii::t('app', 'Linked {client} account.', [
  99. 'client' => $this->client->getTitle()
  100. ]),
  101. ]);
  102. } else {
  103. Yii::$app->getSession()->setFlash('error', [
  104. Yii::t('app', 'Unable to link {client} account: {errors}', [
  105. 'client' => $this->client->getTitle(),
  106. 'errors' => json_encode($auth->getErrors()),
  107. ]),
  108. ]);
  109. }
  110. } else { // there's existing auth
  111. Yii::$app->getSession()->setFlash('error', [
  112. Yii::t('app',
  113. 'Unable to link {client} account. There is another user using it.',
  114. ['client' => $this->client->getTitle()]),
  115. ]);
  116. }
  117. }
  118. }
  119.  
  120. /**
  121. * @param User $user
  122. */
  123. private function updateUserInfo(User $user)
  124. {
  125. $attributes = $this->client->getUserAttributes();
  126. $github = ArrayHelper::getValue($attributes, 'login');
  127. if ($user->github === null && $github) {
  128. $user->github = $github;
  129. $user->save();
  130. }
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement