Advertisement
Guest User

Untitled

a guest
Oct 10th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 KB | None | 0 0
  1. use appcomponentsAuthHandler;
  2.  
  3. class SiteController extends Controller
  4. {
  5. public function actions()
  6. {
  7. return [
  8. 'auth' => [
  9. 'class' => 'yiiauthclientAuthAction',
  10. 'successCallback' => [$this, 'onAuthSuccess'],
  11. ],
  12. ];
  13. }
  14.  
  15. public function onAuthSuccess($client)
  16. {
  17. (new AuthHandler($client))->handle();
  18. }
  19. }
  20.  
  21. <?php
  22. namespace appcomponents;
  23.  
  24. use appmodelsAuth;
  25. use appmodelsUser;
  26. use Yii;
  27. use yiiauthclientClientInterface;
  28. use yiihelpersArrayHelper;
  29.  
  30. /**
  31. * AuthHandler handles successful authentication via Yii auth component
  32. */
  33. class AuthHandler
  34. {
  35. /**
  36. * @var ClientInterface
  37. */
  38. private $client;
  39.  
  40. public function __construct(ClientInterface $client)
  41. {
  42. $this->client = $client;
  43. }
  44.  
  45. public function handle()
  46. {
  47. $attributes = $this->client->getUserAttributes();
  48. $email = ArrayHelper::getValue($attributes, 'email');
  49. $id = ArrayHelper::getValue($attributes, 'id');
  50. $nickname = ArrayHelper::getValue($attributes, 'login');
  51.  
  52. /* @var Auth $auth */
  53. $auth = Auth::find()->where([
  54. 'source' => $this->client->getId(),
  55. 'source_id' => $id,
  56. ])->one();
  57.  
  58. if (Yii::$app->user->isGuest) {
  59. if ($auth) { // login
  60. /* @var User $user */
  61. $user = $auth->user;
  62. $this->updateUserInfo($user);
  63. Yii::$app->user->login($user, Yii::$app->params['user.rememberMeDuration']);
  64. } else { // signup
  65. if ($email !== null && User::find()->where(['email' => $email])->exists()) {
  66. Yii::$app->getSession()->setFlash('error', [
  67. 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()]),
  68. ]);
  69. } else {
  70. $password = Yii::$app->security->generateRandomString(6);
  71. $user = new User([
  72. 'username' => $nickname,
  73. 'github' => $nickname,
  74. 'email' => $email,
  75. 'password' => $password,
  76. ]);
  77. $user->generateAuthKey();
  78. $user->generatePasswordResetToken();
  79.  
  80. $transaction = User::getDb()->beginTransaction();
  81.  
  82. if ($user->save()) {
  83. $auth = new Auth([
  84. 'user_id' => $user->id,
  85. 'source' => $this->client->getId(),
  86. 'source_id' => (string)$id,
  87. ]);
  88. if ($auth->save()) {
  89. $transaction->commit();
  90. Yii::$app->user->login($user, Yii::$app->params['user.rememberMeDuration']);
  91. } else {
  92. Yii::$app->getSession()->setFlash('error', [
  93. Yii::t('app', 'Unable to save {client} account: {errors}', [
  94. 'client' => $this->client->getTitle(),
  95. 'errors' => json_encode($auth->getErrors()),
  96. ]),
  97. ]);
  98. }
  99. } else {
  100. Yii::$app->getSession()->setFlash('error', [
  101. Yii::t('app', 'Unable to save user: {errors}', [
  102. 'client' => $this->client->getTitle(),
  103. 'errors' => json_encode($user->getErrors()),
  104. ]),
  105. ]);
  106. }
  107. }
  108. }
  109. } else { // user already logged in
  110. if (!$auth) { // add auth provider
  111. $auth = new Auth([
  112. 'user_id' => Yii::$app->user->id,
  113. 'source' => $this->client->getId(),
  114. 'source_id' => (string)$attributes['id'],
  115. ]);
  116. if ($auth->save()) {
  117. /** @var User $user */
  118. $user = $auth->user;
  119. $this->updateUserInfo($user);
  120. Yii::$app->getSession()->setFlash('success', [
  121. Yii::t('app', 'Linked {client} account.', [
  122. 'client' => $this->client->getTitle()
  123. ]),
  124. ]);
  125. } else {
  126. Yii::$app->getSession()->setFlash('error', [
  127. Yii::t('app', 'Unable to link {client} account: {errors}', [
  128. 'client' => $this->client->getTitle(),
  129. 'errors' => json_encode($auth->getErrors()),
  130. ]),
  131. ]);
  132. }
  133. } else { // there's existing auth
  134. Yii::$app->getSession()->setFlash('error', [
  135. Yii::t('app',
  136. 'Unable to link {client} account. There is another user using it.',
  137. ['client' => $this->client->getTitle()]),
  138. ]);
  139. }
  140. }
  141. }
  142.  
  143. /**
  144. * @param User $user
  145. */
  146. private function updateUserInfo(User $user)
  147. {
  148. $attributes = $this->client->getUserAttributes();
  149. $github = ArrayHelper::getValue($attributes, 'login');
  150. if ($user->github === null && $github) {
  151. $user->github = $github;
  152. $user->save();
  153. }
  154. }
  155. }
  156.  
  157. <?= yiiauthclientwidgetsAuthChoice::widget([
  158. 'baseAuthUrl' => ['site/auth'],
  159. 'popupMode' => false,
  160. ]) ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement