public function createUserByOAuthIfNotExists(IUserAccount $client, $account, $email) { Yii::info("Trying to create a new user for account [{$client->id}][{$client->userId}][{$email}]", __CLASS__); $user = call_user_func([$this->identityClass, 'findByEmail'], ['email' => $email]); if (!$user) { Yii::info("Creating a new user for account [{$client->id}][{$client->userId}][{$email}]", __CLASS__); $user = new $this->identityClass(); $user->email = $email; $user->name = $client->getRealName(); $user->save(false); Yii::info("User successfuly created for account [{$client->id}][{$client->userId}][{$email}]", __CLASS__); } else { if ($user->password_hash) { throw new DuplicatedUserException(); } } Yii::info("Linking user [{$email}] to account [{$client->id}][{$client->userId}]", __CLASS__); $account->link('user', $user); if (Yii::$app->user->login($user)) { Yii::info("Logging in user [{$client->id}][{$client->userId}][{$email}]", __CLASS__); return true; } Yii::error("Unable to login user [{$client->id}][{$client->userId}][{$email}]", __CLASS__); return false; }
public static function findByClient(IUserAccount $client) { return UserAccount::find()->with('user')->where(['provider' => $client->id, 'client_id' => $client->getUserId()])->one(); }