Completed
Push — master ( 00392d...e92667 )
by Alexey
04:47
created

BaseController::findModel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace modules\users\controllers\backend;
4
5
use Yii;
6
use modules\users\models\LoginForm;
7
use modules\users\models\User;
8
use yii\web\Controller;
9
use yii\web\NotFoundHttpException;
10
use yii\filters\VerbFilter;
11
use yii\filters\AccessControl;
12
use modules\rbac\models\Permission;
13
use modules\users\Module;
14
15
/**
16
 * Class BaseController
17
 * @package modules\users\controllers\backend
18
 */
19
class BaseController extends Controller
20
{
21
    /** @var  string|bool $jsFile */
22
    protected $jsFile;
23
24
    /**
25
     * @inheritdoc
26
     * @return array
27
     */
28
    public function behaviors()
29
    {
30
        return [
31
            'verbs' => $this->getVerbs(),
32
            'access' => $this->getAccess()
33
        ];
34
    }
35
36
    /**
37
     * @return array
38
     */
39
    protected function getVerbs()
40
    {
41
        return [
42
            'class' => VerbFilter::className(),
43
            'actions' => [
44
                'delete' => ['POST'],
45
                'logout' => ['POST'],
46
            ],
47
        ];
48
    }
49
50
    /**
51
     * @return array
52
     */
53
    protected function getAccess()
54
    {
55
        return [
56
            'class' => AccessControl::className(),
57
            'rules' => [
58
                [
59
                    'actions' => ['login'],
60
                    'allow' => true,
61
                    'roles' => ['?']
62
                ],
63
                [
64
                    'actions' => ['logout'],
65
                    'allow' => true,
66
                    'roles' => ['@']
67
                ],
68
                [
69
                    'allow' => true,
70
                    'roles' => [Permission::PERMISSION_MANAGER_USERS]
71
                ],
72
            ],
73
        ];
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79
    public function init()
80
    {
81
        parent::init();
82
        $this->processRegisterJs();
83
    }
84
85
    /**
86
     * Publish and register the required JS file
87
     */
88
    protected function processRegisterJs()
89
    {
90
        $this->jsFile = '@modules/users/views/ajax/ajax.js';
91
        $assetManager = Yii::$app->assetManager;
92
        $assetManager->publish($this->jsFile);
93
        $url = $assetManager->getPublishedUrl($this->jsFile);
94
        $this->view->registerJsFile($url,
95
            ['depends' => 'yii\web\JqueryAsset',] // depends
96
        );
97
    }
98
99
    /**
100
     * Generate new auth key
101
     * @param int|string $id
102
     * @throws NotFoundHttpException
103
     */
104
    public function actionGenerateAuthKey($id)
105
    {
106
        $model = $this->findModel($id);
107
        $model->generateAuthKey();
108
        $model->save();
109
        $this->redirect(['view', 'id' => $model->id]);
110
    }
111
112
    /**
113
     * Finds the User model based on its primary key value.
114
     * If the model is not found, a 404 HTTP exception will be thrown.
115
     * @param int|string $id
116
     * @return null|User the loaded model
117
     * @throws NotFoundHttpException if the model cannot be found
118
     */
119
    protected function findModel($id)
120
    {
121
        if (($model = User::findOne($id)) !== null) {
122
            return $model;
123
        }
124
        throw new NotFoundHttpException(Module::t('module', 'The requested page does not exist.'));
125
    }
126
127
    /**
128
     * Login action.
129
     *
130
     * @return string|\yii\web\Response
131
     */
132
    public function actionLogin()
133
    {
134
        if (!Yii::$app->user->isGuest) {
135
            return $this->goHome();
136
        }
137
138
        $this->layout = '//login';
139
140
        $model = new LoginForm();
141
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
142
            // Если запрещен доступ к Backend сбрасываем авторизацию записываем сообщение в сессию
143
            // и перебрасываем на страницу входа
144
            if (!Yii::$app->user->can(\modules\rbac\models\Permission::PERMISSION_VIEW_ADMIN_PAGE)) {
145
                Yii::$app->user->logout();
146
                Yii::$app->session->setFlash('error', Module::t('module', 'You do not have rights, access is denied.'));
147
                return $this->goHome();
148
            }
149
            return $this->goBack();
150
        }
151
        return $this->render('login', [
152
            'model' => $model,
153
        ]);
154
    }
155
156
    /**
157
     * Logout action.
158
     *
159
     * @return \yii\web\Response
160
     */
161
    public function actionLogout()
162
    {
163
        $model = new LoginForm();
164
        $model->logout();
165
        return $this->goHome();
166
    }
167
}
168