/** * @return \yii\db\ActiveQuery */ public function getUserAuths() { return $this->hasMany(UserAuth::className(), ['user_id' => 'id']); }
/** * 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; UserKey::deleteAll(['user_id' => $user->id]); UserAuth::deleteAll(['user_id' => $user->id]); $profile->delete(); $user->delete(); //删除授权 Yii::$app->authManager->revoke(Yii::$app->authManager->getRole($user->role), $user->id); return $this->redirect(['index']); }
/** * Register a new user using client attributes and then associate userAuth * * @param \yii\authclient\BaseClient $client * @param \app\modules\user\models\UserAuth $userAuth */ protected function registerAndLoginUser($client, $userAuth) { /** @var \app\modules\user\models\User $user */ /** @var \app\modules\user\models\Profile $profile */ /** @var \app\modules\user\models\Role $role */ $role = Yii::$app->getModule("user")->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, Yii::$app->request->userIP, $user::STATUS_ACTIVE)->save(false); $profile->setUser($user->id)->save(false); $userAuth->setUser($user->id)->save(false); // log user in Yii::$app->user->login($user, Yii::$app->getModule("user")->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) { // 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']); }
/** * 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); // If user has admin role, check if the one if ($user->role->id === Role::ROLE_ADMIN && User::find()->where(['role_id' => Role::ROLE_ADMIN])->count() < 2) { Yii::$app->session->setFlash('danger', Yii::t('app', "You need to create another user with 'Admin' role in order to delete your account.")); } else { $profile = $user->profile; UserKey::deleteAll(['user_id' => $user->id]); UserAuth::deleteAll(['user_id' => $user->id]); FormUser::deleteAll(["user_id" => $user->id]); $profile->delete(); $user->delete(); Yii::$app->getSession()->setFlash('success', Yii::t('app', 'The user has been successfully deleted.')); } return $this->redirect(['index']); }