Guest User

Untitled

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