public function actionIndex() { if (!\Yii::$app->user->isGuest) { return $this->redirect(['goods/index']); } //判断请求类型 if (Yii::$app->request->isPost && Yii::$app->request->isAjax) { $name = Yii::$app->request->Post('username'); $password = (string) Yii::$app->request->Post('password'); //查询管理员是否存在 $admin = Admin::findOne(['name' => $name]); if (null === $admin) { $this->ajaxReturn(false, [], '用户不存在,请先注册'); } //获取密码 $passwordServer = $admin->getAttribute('password'); if ($password !== $passwordServer) { $this->ajaxReturn(false, [], '密码错误,请重新输入'); } //获取管理员id $adminId = $admin->getPrimaryKey(); //如果存在记入session $session = Yii::$app->getSession(); $session->open(); //组装SESSION $sessionBody = ['adminId' => $adminId, 'name' => $name]; $sessionData = $session->set('username', $sessionBody); $this->ajaxReturn(true, [], '登录成功'); } return $this->renderPartial('index'); }
public function actionChangePassword() { $model = new \backend\models\Admin(['scenario' => 'admin-change-password']); if ($model->load(Yii::$app->request->post()) && $model->validate()) { $user = Admin::findOne(Yii::$app->user->identity->id); $user->setPassword($model->password); $user->generateAuthKey(); if ($user->save()) { Yii::$app->getSession()->setFlash('success', Yii::t('app', 'New password was saved.')); } return $this->redirect(['change-password']); } return $this->render('changePassword', ['model' => $model]); }
/** * Finds the User model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return User the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Admin::findOne(['id' => $id, 'status' => [Admin::STATUS_ACTIVE, Admin::STATUS_INACTIVE]])) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Finds the Admin model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Admin the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Admin::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Find helpDesk by email * @param string, $email * @return Array, HelpDesk info */ public static function getByEmail($email) { return Admin::findOne(['email' => $email]); }