/** * Finds user by [[username]] * * @return User|null */ public function getUser() { if ($this->_user === false) { $this->_user = User::findByEmailOrUserName($this->email); } return $this->_user; }
public function successCallback($client) { $attributes = $client->userAttributes; $this->action->successUrl = Yii::$app->session->get('returnUrl'); $key = UserOauthKey::findOne(['provider_id' => $attributes['provider_id'], 'provider_user_id' => $attributes['provider_user_id']]); if ($key) { if (Yii::$app->user->isGuest) { return Yii::$app->user->login($key->user, 3600 * 24 * 30); } else { if ($key->user_id != Yii::$app->user->id) { Yii::$app->session->setFlash('error', 'Данный ключ уже закреплен за другим пользователем сайта.'); return true; } } } else { if (Yii::$app->user->isGuest) { $user = false; if ($attributes['User']['email'] != null) { $user = User::findByEmailOrUserName($attributes['User']['email']); } if (!$user) { $user = new User(); $user->scenario = 'oauth'; $user->load($attributes); $user->username = $this->findFreeUsername($user->username); $user->validate(); return $user->save() && $this->createKey($attributes, $user->id) && Yii::$app->user->login($user, 3600 * 24 * 30); } else { return $this->createKey($attributes, $user->id) && Yii::$app->user->login($user, 3600 * 24 * 30); } } else { $this->createKey($attributes, Yii::$app->user->id); Yii::$app->session->setFlash('success', 'Ключ успешно добавлен.'); return true; } } }