public function actionIndex() { $authManager = \Yii::$app->getAuthManager(); if (!$authManager instanceof DbManager) { throw new InvalidConfigException('You should configure "authManager" component to use database before executing this migration.'); } if ((new \yii\db\Query())->select('*')->from($authManager->itemTable)->where('name=:name', [':name' => 'userManager'])->count() > 0) { throw new InvalidConfigException('Illegal attempt to run setup: data already exists.'); } if (null !== User::find()->where(['email' => '*****@*****.**'])->one()) { throw new InvalidConfigException('Illegal attempt to run setup: user admin@admin.com already exists.'); } $admin = new User(); $admin->setPassword('admin'); $admin->email = '*****@*****.**'; $admin->status = User::STATUS_ACTIVE; if (!$admin->save()) { throw new ErrorException('Unable to save default admin user: '******'userManager', 'authItemEditor', 'userAssignRoles', 'authItemEditRule'] as $roleName) { $role = $authManager->createRole($roleName); $authManager->add($role); $authManager->assign($role, $admin->getId()); } }
/** * Finds user by [[username]] * * @return User|null */ public function getUser() { if ($this->_user === false) { $this->_user = User::findByEmail($this->username); } return $this->_user; }
public function actionUpdate($id) { $model = User::findOne(['id' => $id]); if (null === $model) { throw new NotFoundHttpException(Yii::t('ica_auth', 'No such user.')); } if ($this->saveUser($model)) { Yii::$app->session->setFlash('userUpdated'); } return $this->render('@icalab/auth/views/user/update', ['model' => $model]); }
/** * Return the user that is associated with this request. */ public function getUser() { return $this->hasOne(User::className(), ['id' => 'userid']); }
/** * Reset the password for a user. */ public function actionReset() { if (null === Yii::$app->request->post()) { throw new BadRequestHttpException(); } $post = Yii::$app->request->post(); if (!isset($post['User']) || !isset($post['ResetPassword']) || !isset($post['User']['email']) || !isset($post['ResetPassword']['reset_token']) || !isset($post['password']) || !isset($post['password_confirm'])) { throw new BadRequestHttpException(); } $user = User::findByEmail($post['User']['email']); if (null === $user) { throw new NotFoundHttpException(); } $request = ResetPassword::findOne(['reset_token' => $post['ResetPassword']['reset_token'], 'userid' => $user->getId()]); if (null == $request) { throw new NotFoundHttpException(); } if ($post['password'] != $post['password_confirm']) { Yii::$app->getSession()->setFlash('error', Yii::t('ica_auth', 'Passwords do not match.')); $this->redirect(Yii::$app->request->getReferrer()); return; } $user->setPassword($post['password']); if ($user->save(true, array('password_hash'))) { $request->delete(); return $this->render('@icalab/auth/views/password-reset/success'); } Yii::$app->getSession()->setFlash('error', Yii::t('ica_auth', 'Unable to update password. Please try again or contact support..')); $this->redirect(Yii::$app->request->getReferrer()); }