123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- <?php
- /**
- * Created by PhpStorm.
- * User: NODELOG
- * Date: 16/7/26
- * Time: 下午5:56
- */
- namespace common\modules\user\backend\controllers;
- use common\components\Controller;
- use common\helpers\Util;
- use common\modules\user\backend\models\UserSearch;
- use common\modules\user\models\LoginForm;
- use common\modules\user\models\Profile;
- use common\modules\user\models\QrcodeLoginForm;
- use common\modules\user\models\User;
- use common\modules\user\traits\AjaxValidationTrait;
- use Yii;
- use yii\data\ActiveDataProvider;
- use yii\filters\VerbFilter;
- use yii\helpers\Url;
- use yii\web\ForbiddenHttpException;
- use yii\web\NotFoundHttpException;
- class DefaultController extends Controller
- {
- // use AjaxValidationTrait;
- /** @inheritdoc */
- public function behaviors()
- {
- return [
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- 'delete' => ['post'],
- 'confirm' => ['post'],
- 'block' => ['post'],
- ],
- ],
- ];
- }
- public function actionLogin()
- {
- $this->layout = '@common/modules/user/backend/views/default/main-login.php';
- if (!\Yii::$app->user->isGuest) {
- return $this->goHome();
- }
- $model = new LoginForm();
- if ($model->load(Yii::$app->request->post()) && $model->loginAdmin()) {
- return $this->goBack();
- } else {
- if (Yii::$app->request->isAjax) {
- return $this->renderAjax('login', [
- 'model' => $model,
- ]);
- }
- return $this->render('login', [
- 'model' => $model,
- ]);
- }
- }
- public function actionLogout()
- {
- Yii::$app->user->logout();
- return $this->goHome();
- }
- /**
- * Lists all User models.
- * @return mixed
- */
- public function actionIndex()
- {
- $searchModel = new UserSearch();
- $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
- return $this->render('index', [
- 'dataProvider' => $dataProvider,
- 'searchModel' => $searchModel,
- ]);
- }
- /**
- * Displays a single User model.
- * @param integer $id
- * @return mixed
- */
- public function actionView($id)
- {
- return $this->render('view', [
- 'model' => $this->findModel($id),
- ]);
- }
- /**
- * Creates a new User model.
- * If creation is successful, the browser will be redirected to the 'view' page.
- * @return mixed
- */
- public function actionCreate()
- {
- /** @var User $user */
- $user = \Yii::createObject([
- 'class' => User::className(),
- 'scenario' => 'create',
- ]);
- if ($user->load(\Yii::$app->request->post()) && $user->create()) {
- \Yii::$app->getSession()->setFlash('success', \Yii::t('user', 'User has been created'));
- return $this->redirect(['update', 'id' => $user->id]);
- }
- return $this->render('create', [
- 'user' => $user,
- ]);
- }
- /**
- * Updates an existing User model.
- * If update is successful, the browser will be redirected to the 'view' page.
- * @param integer $id
- * @return mixed
- */
- public function actionUpdate($id)
- {
- Url::remember('', 'actions-redirect');
- $user = $this->findModel($id);
- $user->scenario = 'update';
- if ($user->load(\Yii::$app->request->post()) && $user->save()) {
- \Yii::$app->getSession()->setFlash('success', \Yii::t('user', 'Account details have been updated'));
- return $this->refresh();
- }
- return $this->render('_account', [
- 'user' => $user,
- ]);
- }
- /**
- * Deletes an existing User model.
- * If deletion is successful, the browser will be redirected to the 'index' page.
- * @param integer $id
- * @return mixed
- */
- public function actionDelete($id)
- {
- $this->findModel($id)->delete();
- return $this->redirect(['index']);
- }
- /**
- * 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
- */
- public function findModel($id)
- {
- if (($model = User::findOne($id)) !== null) {
- return $model;
- } else {
- throw new NotFoundHttpException('The requested page does not exist.');
- }
- }
- /**
- * 重置密码
- */
- public function actionResetPassword($id)
- {
- $model = $this->findModel($id);
- $model->scenario = 'resetPassword';
- if($model->load(Yii::$app->request->post()) && $model->save()){
- Yii::$app->user->logout();
- return $this->goHome();
- }
- return $this->render('reset-password', [
- 'model' => $model
- ]);
- }
- public function actionUpdateProfile($id)
- {
- Url::remember('', 'actions-redirect');
- $user = $this->findModel($id);
- $profile = $user->profile;
- if ($profile == null) {
- $profile = \Yii::createObject(Profile::className());
- $profile->link('user', $user);
- }
- if ($profile->load(\Yii::$app->request->post()) && $profile->save()) {
- \Yii::$app->getSession()->setFlash('success', \Yii::t('user', 'Profile details have been updated'));
- return $this->refresh();
- }
- return $this->render('_profile', [
- 'user' => $user,
- 'profile' => $profile,
- ]);
- }
- public function actionBlock($id)
- {
- if ($id == \Yii::$app->user->getId()) {
- if (Yii::$app->request->isAjax) {
- Yii::$app->response->format = 'json';
- return ['status' => 0, 'message' => \Yii::t('user', 'You can not block your own account')];
- }
- \Yii::$app->getSession()->setFlash('danger', \Yii::t('user', 'You can not block your own account'));
- } else {
- $user = $this->findModel($id);
- if ($user->isAdmin) {
- throw new ForbiddenHttpException('不支持封禁管理员帐号');
- }
- if ($user->getIsBlocked()) {
- $user->unblock();
- if (Yii::$app->request->isAjax) {
- Yii::$app->response->format = 'json';
- return ['message' => \Yii::t('user', 'User has been unblocked')];
- }
- \Yii::$app->getSession()->setFlash('success', \Yii::t('user', 'User has been unblocked'));
- } else {
- $user->block();
- if (Yii::$app->request->isAjax) {
- Yii::$app->response->format = 'json';
- return ['message' => \Yii::t('user', 'User has been blocked')];
- }
- \Yii::$app->getSession()->setFlash('success', \Yii::t('user', 'User has been blocked'));
- }
- }
- return $this->redirect(Url::previous('actions-redirect'));
- }
- /**
- * @param $id
- * @return \yii\web\Response
- * @throws NotFoundHttpException
- */
- public function actionConfirm($id)
- {
- $model = $this->findModel($id);
- $model->confirm();
- \Yii::$app->getSession()->setFlash('success', \Yii::t('user', 'User has been confirmed'));
- return $this->redirect(Url::previous('actions-redirect'));
- }
- public function actionAssignments($id)
- {
- Url::remember('', 'actions-redirect');
- $user = $this->findModel($id);
- return $this->render('_assignments', [
- 'user' => $user
- ]);
- }
- /**
- * 二维码登录
- * @return string|\yii\web\Response
- * @throws \yii\base\Exception
- * @throws \yii\base\InvalidConfigException
- * @author nodelog
- */
- public function actionQrcodeLogin()
- {
- $this->layout = '@common/modules/user/backend/views/default/main-login.php';
- if (!\Yii::$app->user->isGuest) {
- return $this->goHome();
- }
- $model = new QrcodeLoginForm();
- if(Yii::$app->request->isAjax){
- $model->access_token = request('access_token');
- $model->qrcode_hash = request('qrcode_hash');
- if ($model->login()) {
- return $this->renderJson(1, '登录成功', ['returnUrl' => Yii::$app->getUser()->getReturnUrl()]);
- } else {
- return $this->renderJson(0, current($model->getErrors())[0], ['errors' => $model->getErrors()]);
- }
- } else {
- $access_token = Yii::$app->security->generateRandomString();
- $qrcode = Util::qrcode(User::generateQrcodeLoginUrl($access_token));
- $qrcode['access_token'] = $access_token;
- return $this->render('qrcode-login', $qrcode);
- }
- }
- }
|