Completed
Push — master ( 52622f...734559 )
by Alexey
02:52
created

ProfileController::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace modules\users\controllers\common;
4
5
use Yii;
6
use yii\web\Controller;
7
use modules\users\models\User;
8
use modules\rbac\models\Assignment;
9
use yii\filters\AccessControl;
10
use yii\filters\VerbFilter;
11
use yii\web\NotFoundHttpException;
12
use yii\bootstrap\ActiveForm;
13
use yii\web\Response;
14
use modules\users\Module;
15
16
/**
17
 * Class ProfileController
18
 * @package modules\users\controllers\common
19
 */
20
class ProfileController extends Controller
21
{
22
    /** @var  string|bool $jsFile */
23
    protected $jsFile;
24
25
    /**
26
     * @inheritdoc
27
     * @return array
28
     */
29
    public function behaviors()
30
    {
31
        return [
32
            'verbs' => [
33
                'class' => VerbFilter::className(),
34
                'actions' => [
35
                    'delete' => ['post'],
36
                ],
37
            ],
38
            'access' => [
39
                'class' => AccessControl::className(),
40
                'rules' => [
41
                    [
42
                        'allow' => true,
43
                        'roles' => ['@']
44
                    ],
45
                ],
46
            ],
47
        ];
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function init()
54
    {
55
        parent::init();
56
        $this->processRegisterJs();
57
    }
58
59
    /**
60
     * Publish and register the required JS file
61
     */
62
    protected function processRegisterJs()
63
    {
64
        $this->jsFile = '@modules/users/views/ajax/ajax.js';
65
        $assetManager = Yii::$app->assetManager;
66
        $assetManager->publish($this->jsFile);
67
        $url = $assetManager->getPublishedUrl($this->jsFile);
68
        $this->view->registerJsFile($url,
69
            ['depends' => 'yii\web\JqueryAsset',] // depends
70
        );
71
    }
72
73
    /**
74
     * @return string
75
     * @throws NotFoundHttpException
76
     */
77
    public function actionIndex()
78
    {
79
        $model = $this->findModel();
80
81
        $assignModel = new Assignment();
82
        $assignModel->user = $model;
83
84
        return $this->render('index', [
85
            'model' => $model,
86
            'assignModel' => $assignModel,
87
        ]);
88
    }
89
90
    /**
91
     * @return string
92
     * @throws NotFoundHttpException
93
     */
94
    public function actionUpdate()
95
    {
96
        $model = $this->findModel();
97
        return $this->render('update', [
98
            'model' => $model,
99
        ]);
100
    }
101
102
    /**
103
     * @return Response
104
     * @throws NotFoundHttpException
105
     */
106
    public function actionUpdateProfile()
107
    {
108
        $model = $this->findModel();
109
        $model->scenario = $model::SCENARIO_PROFILE_UPDATE;
110
111
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
112
            Yii::$app->session->setFlash('success', Module::t('module', 'Profile successfully changed.'));
113
        } else {
114
            Yii::$app->session->setFlash('error', Module::t('module', 'Error! Profile not changed.'));
115
        }
116
        return $this->redirect(['update', 'tab' => 'profile']);
117
    }
118
119
    /**
120
     * @return array|Response
121
     * @throws NotFoundHttpException
122
     */
123
    public function actionUpdatePassword()
124
    {
125
        $model = $this->findModel();
126
        $model->scenario = $model::SCENARIO_PASSWORD_UPDATE;
127
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
128
            Yii::$app->session->setFlash('success', Module::t('module', 'Password changed successfully.'));
129
        } else {
130
            Yii::$app->session->setFlash('error', Module::t('module', 'Error! Password changed not successfully.'));
131
        }
132
        return $this->redirect(['update', 'tab' => 'password']);
133
    }
134
135
    /**
136
     * @return array|Response
137
     * @throws NotFoundHttpException
138
     */
139
    public function actionAjaxValidatePasswordForm()
140
    {
141
        $model = $this->findModel();
142
        $model->scenario = $model::SCENARIO_PASSWORD_UPDATE;
143
        if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
144
            Yii::$app->response->format = Response::FORMAT_JSON;
145
            return ActiveForm::validate($model);
146
        }
147
        return $this->redirect(['index']);
148
    }
149
150
    /**
151
     * Deletes an existing User model.
152
     * This delete set status blocked, is successful, logout and the browser will be redirected to the 'home' page.
153
     * @return Response
154
     * @throws NotFoundHttpException
155
     */
156
    public function actionDelete()
157
    {
158
        $model = $this->findModel();
159
        $model->scenario = $model::SCENARIO_PROFILE_DELETE;
160
        $model->status = $model::STATUS_DELETED;
161
        if ($model->save())
162
            Yii::$app->user->logout();
163
        return $this->goHome();
164
    }
165
166
    /**
167
     * Action Generate new auth key
168
     * @throws NotFoundHttpException
169
     */
170
    public function actionGenerateAuthKey()
171
    {
172
        $model = $this->processGenerateAuthKey();
173
        if (Yii::$app->request->isAjax) {
174
            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
175
            return [
176
                'body' => $this->renderAjax('tabs/col_auth_key', ['model' => $model]),
177
                'success' => true,
178
            ];
179
        }
180
        return $this->redirect(['index']);
181
    }
182
183
    /**
184
     * Generate new auth key
185
     * @return User|null
186
     * @throws NotFoundHttpException
187
     */
188
    private function processGenerateAuthKey()
189
    {
190
        $model = $this->findModel();
191
        $model->generateAuthKey();
192
        $model->save();
193
        return $model;
194
    }
195
196
    /**
197
     * Finds the User model based on its primary key value.
198
     * If the model is not found, a 404 HTTP exception will be thrown.
199
     * @return null|User the loaded model
200
     * @throws NotFoundHttpException if the model cannot be found
201
     */
202
    private function findModel()
203
    {
204
        if (!Yii::$app->user->isGuest) {
205
            /** @var object $identity */
206
            $identity = Yii::$app->user->identity;
207
            if (($model = User::findOne($identity->id)) !== null) {
208
                return $model;
209
            }
210
        }
211
        throw new NotFoundHttpException(Module::t('module', 'The requested page does not exist.'));
212
    }
213
}
214