Passed
Push — master ( aa95a2...ffbac9 )
by Alexey
02:44
created

BaseController::actionGenerateAuthKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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