/** * @return \yii\db\ActiveQuery */ public function getUserAuths() { return $this->hasMany(UserAuth::className(), ['user_id' => 'id']); }
/** * Register a new user using client attributes and then associate userAuth * @param \yii\authclient\BaseClient $client * @param \amnah\yii2\user\models\UserAuth $userAuth */ protected function registerAndLoginUser($client, $userAuth) { /** @var \amnah\yii2\user\models\User $user */ /** @var \amnah\yii2\user\models\Profile $profile */ /** @var \amnah\yii2\user\models\Role $role */ $role = $this->module->model("Role"); // set user and profile info $attributes = $client->getUserAttributes(); $function = "setInfo" . ucfirst($client->name); // "setInfoFacebook()" list($user, $profile) = $this->{$function}($attributes); // calculate and double check username (in case it is already taken) $fallbackUsername = "******"; $user = $this->doubleCheckUsername($user, $fallbackUsername); // save new models $user->setRegisterAttributes($role::ROLE_USER, $user::STATUS_ACTIVE)->save(); $profile->setUser($user->id)->save(); $userAuth->setUser($user->id)->save(); // log user in Yii::$app->user->login($user, $this->module->loginDuration); }
/** * Delete an existing User model. If deletion is successful, the browser * will be redirected to the 'index' page. * @param string $id * @return mixed */ public function actionDelete($id) { if ((Yii::$app->user->can("delete-user") || Yii::$app->user->can("user")) && $id != 1) { //Guarda a mensagem $mensagem = ""; $transaction = \Yii::$app->db->beginTransaction(); try { // delete profile and userTokens first to handle foreign key constraint $user = $this->findModel($id); $profile = $user->profile; UserToken::deleteAll(['user_id' => $user->id]); UserAuth::deleteAll(['user_id' => $user->id]); $profile->delete(); if ($user->delete()) { $transaction->commit(); } } catch (\Exception $exception) { $transaction->rollBack(); $mensagem = "Ocorreu uma falha inesperada ao tentar salvar "; } /** @var \amnah\yii2\user\models\search\UserSearch $searchModel */ $searchModel = $this->module->model("UserSearch"); $dataProvider = $searchModel->search(Yii::$app->request->getQueryParams()); return $this->render('index', compact('searchModel', 'dataProvider')); } else { throw new ForbiddenHttpException("Acesso negado!"); } }
/** * Delete an existing User model. If deletion is successful, the browser * will be redirected to the 'index' page. * @param string $id * @return mixed */ public function actionDelete($id) { // delete profile and userTokens first to handle foreign key constraint $user = $this->findModel($id); $profile = $user->profile; UserToken::deleteAll(['user_id' => $user->id]); UserAuth::deleteAll(['user_id' => $user->id]); $profile->delete(); $user->delete(); return $this->redirect(['index']); }
/** * @inheritdoc */ public function upManual() { // delete neo user that the migration automatically adds UserKey::deleteAll(['user_id' => 1]); UserAuth::deleteAll(['user_id' => 1]); Profile::deleteAll(['user_id' => 1]); User::deleteAll(['id' => 1]); }
/** * Delete an existing User model. If deletion is successful, the browser * will be redirected to the 'index' page. * * @param string $id * @return mixed */ public function actionDelete($id) { // delete profile and userkeys first to handle foreign key constraint $user = $this->findModel($id); $profile = $user->profile; $userabonnement = $user->abonnement; $company = Yii::$app->getModule("user")->model("Company"); $company = $company::findOne(['user_id' => $id]); $tariefDag = Yii::$app->getModule("user")->model("UserTarievenDag"); $tariefDag = $tariefDag::findOne(['user_id' => $id]); $tariefNacht = Yii::$app->getModule("user")->model("UserTarievenNacht"); $tariefNacht = $tariefNacht::findOne(['user_id' => $id]); $tariefWeekend = Yii::$app->getModule("user")->model("UserTarievenWeekend"); $tariefWeekend = $tariefWeekend::findOne(['user_id' => $id]); $reistarief = Yii::$app->getModule("user")->model("UserReistarief"); $reistarief = $reistarief::findOne(['user_id' => $id]); $invoice = Yii::$app->getModule("user")->model("UserInvoice"); $invoice = $invoice::findOne(['user_id' => $id]); $activedays = Yii::$app->getModule("user")->model("UserActivedays"); $activedays = $activedays::findOne(['user_id' => $id]); UserKey::deleteAll(['user_id' => $user->id]); UserAuth::deleteAll(['user_id' => $user->id]); if (isset($profile)) { $profile->delete(); } if (isset($userabonnement)) { $userabonnement->delete(); } if (isset($tariefDag)) { $tariefDag->delete(); } if (isset($tariefNacht)) { $tariefNacht->delete(); } if (isset($tariefWeekend)) { $tariefWeekend->delete(); } if (isset($reistarief)) { $reistarief->delete(); } $this->deleteZoHoContact($user); if (isset($invoice)) { $invoice->delete(); } if (isset($activedays)) { $activedays->delete(); } $this->deleteDependentUsers($user); $user->delete(); return $this->redirect(['index']); }
/** * Returns 'true' if $provider_id is not persist into user_auth * @param $provider_id * @return bool */ protected function checkIsAlreadyConnected($provider_id) { $count = UserAuth::find()->where(['provider_id' => $provider_id])->count(); return !($count > 0); }
public function safeDown() { $this->dropTable(UserAuth::tableName()); }
/** * Delete user account connection * @param $userAuthId * @return false|int * @throws */ public static function disconnect($userAuthId) { if (\Yii::$app->user->isGuest) { throw new UnauthorizedHttpException(\Yii::t('user', 'Guests can not disconnect accounts')); } $user = \Yii::$app->user->identity; if (empty($user->password) && count($user->userAuths) < 2) { throw new ErrorException(\Yii::t('user', 'Can not disconnect single authentication method. Please fill email and password and try again')); } $userAuth = UserAuth::findOne($userAuthId); if ($userAuth->user_id != \Yii::$app->user->id) { throw new UnauthorizedHttpException(\Yii::t('user', 'Сan not disconnect a foreign account')); } return $userAuth->delete(); }