Blame view

backend/modules/user/controllers/LoginController.php 2.35 KB
d1f8bd40   Alexey Boroda   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  <?php
  
  namespace backend\modules\user\controllers;
  
  use Yii;
  use yii\filters\AccessControl;
  use yii\captcha\CaptchaAction;
  use yii\web\Response;
  use yii\widgets\ActiveForm;
  //
  use thread\app\base\controllers\BackendController;
  use thread\modules\user\models\form\SignInForm;
  
  
  /**
   * Class LoginController
   *
   * @package admin\modules\user\controllers
   * @author FilamentV <vortex.filament@gmail.com>
   * @copyright (c), Thread
   */
  class LoginController extends BackendController
  {
      public $name = 'login';
      public $title = "Login";
      public $layout = "@app/layouts/nologin";
      public $defaultAction = 'index';
      protected $model = SignInForm::class;
  
      /**
       * @inheritdoc
       */
      public function behaviors()
      {
          return [
              'AccessControl' => [
                  'class' => AccessControl::class,
                  'rules' => [
                      [
                          'allow' => true,
                          'actions' => ['index', 'captcha', 'validation'],
                          'roles' => ['?'],
                      ],
                      [
                          'allow' => false,
                      ],
                  ],
              ],
          ];
      }
  
      /**
       * @inheritdoc
       */
      public function actions()
      {
          return [
              'captcha' => [
                  'class' => CaptchaAction::class,
              ],
          ];
      }
  
      /**
       * @return string|\yii\web\Response
       */
      public function actionIndex()
      {
          /** @var SignInForm $model */
          $model = new $this->model;
          $model->setScenario('signIn');
          $model->ONLY_ADMIN = true;
  
          if ($model->load(Yii::$app->getRequest()->post()) && $model->login()) {
              return $this->goHome();
          } else {
              return $this->render('index', [
                  'model' => $model,
              ]);
          }
      }
  
      /**
       * Перевірка валідації моделі(-ей)
       * Має бути встановдений у моделі scenario 'backend'
       * @return mixed
       */
      public function actionValidation()
      {
          /** @var Model $model */
          $model = new $this->model;
          $model->setScenario('signIn');
          $model->load(Yii::$app->getRequest()->post());
  
          Yii::$app->getResponse()->format = Response::FORMAT_JSON;
  
          return ActiveForm::validate($model);
      }
  }