Completed
Push — master ( 85bea6...86c7db )
by Alexey
02:30
created

BaseController::processGenerateAuthKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
eloc 4
nc 1
nop 1
cc 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 yii\web\Response;
14
use modules\users\Module;
15
16
/**
17
 * Class BaseController
18
 * @package modules\users\controllers\backend
19
 */
20
class BaseController extends Controller
21
{
22
    /** @var  string|bool $jsFile */
23
    private $jsFile;
24
25
    /**
26
     * @inheritdoc
27
     * @return array
28
     */
29
    public function behaviors()
30
    {
31
        return [
32
            'verbs' => $this->getVerbs(),
33
            'access' => $this->getAccess()
34
        ];
35
    }
36
37
    /**
38
     * @return array
39
     */
40
    private function getVerbs()
41
    {
42
        return [
43
            'class' => VerbFilter::className(),
44
            'actions' => [
45
                'delete' => ['POST'],
46
                'logout' => ['POST'],
47
            ],
48
        ];
49
    }
50
51
    /**
52
     * @return array
53
     */
54
    private function getAccess()
55
    {
56
        return [
57
            'class' => AccessControl::className(),
58
            'rules' => [
59
                [
60
                    'actions' => ['login'],
61
                    'allow' => true,
62
                    'roles' => ['?']
63
                ],
64
                [
65
                    'actions' => ['logout'],
66
                    'allow' => true,
67
                    'roles' => ['@']
68
                ],
69
                [
70
                    'allow' => true,
71
                    'roles' => [Permission::PERMISSION_MANAGER_USERS]
72
                ],
73
            ],
74
        ];
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80
    public function init()
81
    {
82
        parent::init();
83
        $this->processRegisterJs();
84
    }
85
86
    /**
87
     * Publish and register the required JS file
88
     */
89
    protected function processRegisterJs()
90
    {
91
        $this->jsFile = '@modules/users/views/ajax/ajax.js';
92
        $assetManager = Yii::$app->assetManager;
93
        $assetManager->publish($this->jsFile);
94
        $url = $assetManager->getPublishedUrl($this->jsFile);
95
        $this->view->registerJsFile($url,
96
            ['depends' => 'yii\web\JqueryAsset',] // depends
97
        );
98
    }
99
100
    /**
101
     * Action Generate new auth key
102
     * @throws NotFoundHttpException
103
     */
104
    public function actionGenerateAuthKey($id)
105
    {
106
        $model = $this->processGenerateAuthKey($id);
107
        if (Yii::$app->request->isAjax) {
108
            Yii::$app->response->format = Response::FORMAT_JSON;
109
            return [
110
                'body' => $this->renderAjax('tabs/col_auth_key', ['model' => $model]),
111
                'success' => true,
112
            ];
113
        }
114
        return $this->redirect(['view', 'id' => $model->id]);
115
    }
116
117
    /**
118
     * Generate new auth key
119
     * @param int|string $id
120
     * @return User|null
121
     * @throws NotFoundHttpException
122
     */
123
    public function processGenerateAuthKey($id)
124
    {
125
        $model = $this->findModel($id);
126
        $model->generateAuthKey();
127
        $model->save();
128
        return $model;
129
    }
130
131
    /**
132
     * Finds the User model based on its primary key value.
133
     * If the model is not found, a 404 HTTP exception will be thrown.
134
     * @param int|string $id
135
     * @return null|User the loaded model
136
     * @throws NotFoundHttpException if the model cannot be found
137
     */
138
    protected function findModel($id)
139
    {
140
        if (($model = User::findOne($id)) !== null) {
141
            return $model;
142
        }
143
        throw new NotFoundHttpException(Module::t('module', 'The requested page does not exist.'));
144
    }
145
146
    /**
147
     * Login action.
148
     *
149
     * @return string|\yii\web\Response
150
     */
151
    public function actionLogin()
152
    {
153
        if (!Yii::$app->user->isGuest) {
154
            return $this->goHome();
155
        }
156
157
        $this->layout = '//login';
158
159
        $model = new LoginForm();
160
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
161
            return $this->processCheckPermissionLogin();
162
        }
163
        return $this->render('login', [
164
            'model' => $model,
165
        ]);
166
    }
167
168
    /**
169
     * @return \yii\web\Response
170
     */
171
    protected function processCheckPermissionLogin()
172
    {
173
        // If access to Backend is denied, reset authorization, write a message to the session
174
        // and move it to the login page
175
        if (!Yii::$app->user->can(\modules\rbac\models\Permission::PERMISSION_VIEW_ADMIN_PAGE)) {
176
            Yii::$app->user->logout();
177
            Yii::$app->session->setFlash('error', Module::t('module', 'You do not have rights, access is denied.'));
178
            return $this->goHome();
179
        }
180
        return $this->goBack();
181
    }
182
183
    /**
184
     * Logout action.
185
     *
186
     * @return \yii\web\Response
187
     */
188
    public function actionLogout()
189
    {
190
        $model = new LoginForm();
191
        $model->logout();
192
        return $this->goHome();
193
    }
194
}
195