Completed
Push — master ( 8a998a...498d96 )
by Alexey
05:27
created

ProfileController::actionUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
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
    /**
23
     * @inheritdoc
24
     * @return array
25
     */
26
    public function behaviors()
27
    {
28
        return [
29
            'verbs' => [
30
                'class' => VerbFilter::className(),
31
                'actions' => [
32
                    'delete' => ['post'],
33
                ],
34
            ],
35
            'access' => [
36
                'class' => AccessControl::className(),
37
                'rules' => [
38
                    [
39
                        'allow' => true,
40
                        'roles' => ['@']
41
                    ],
42
                ],
43
            ],
44
        ];
45
    }
46
47
    /**
48
     * @return string
49
     * @throws NotFoundHttpException
50
     */
51
    public function actionIndex()
52
    {
53
        $model = $this->findModel();
54
55
        $assignModel = new Assignment();
56
        $assignModel->user = $model;
57
58
        return $this->render('index', [
59
            'model' => $model,
60
            'assignModel' => $assignModel,
61
        ]);
62
    }
63
64
    /**
65
     * @return string
66
     * @throws NotFoundHttpException
67
     */
68
    public function actionUpdate()
69
    {
70
        $model = $this->findModel();
71
        return $this->render('update', [
72
            'model' => $model,
73
        ]);
74
    }
75
76
    /**
77
     * @return Response
78
     * @throws NotFoundHttpException
79
     */
80
    public function actionUpdateProfile()
81
    {
82
        $model = $this->findModel();
83
        $model->scenario = $model::SCENARIO_PROFILE_UPDATE;
84
85
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
86
            Yii::$app->session->setFlash('success', Module::t('module', 'Profile successfully changed.'));
87
        } else {
88
            Yii::$app->session->setFlash('error', Module::t('module', 'Error! Profile not changed.'));
89
        }
90
        return $this->redirect(['update', 'tab' => 'profile']);
91
    }
92
93
    /**
94
     * @return array|Response
95
     * @throws NotFoundHttpException
96
     */
97
    public function actionUpdatePassword()
98
    {
99
        $model = $this->findModel();
100
        $model->scenario = $model::SCENARIO_PASSWORD_UPDATE;
101
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
102
            Yii::$app->session->setFlash('success', Module::t('module', 'Password changed successfully.'));
103
        } else {
104
            Yii::$app->session->setFlash('error', Module::t('module', 'Error! Password changed not successfully.'));
105
        }
106
        return $this->redirect(['update', 'tab' => 'password']);
107
    }
108
109
    /**
110
     * @return array|Response
111
     * @throws NotFoundHttpException
112
     */
113
    public function actionAjaxValidatePasswordForm()
114
    {
115
        $model = $this->findModel();
116
        $model->scenario = $model::SCENARIO_PASSWORD_UPDATE;
117
        if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
118
            Yii::$app->response->format = Response::FORMAT_JSON;
119
            return ActiveForm::validate($model);
120
        }
121
        return $this->redirect(['index']);
122
    }
123
124
    /**
125
     * Deletes an existing User model.
126
     * This delete set status blocked, is successful, logout and the browser will be redirected to the 'home' page.
127
     * @return Response
128
     * @throws NotFoundHttpException
129
     */
130
    public function actionDelete()
131
    {
132
        $model = $this->findModel();
133
        $model->scenario = $model::SCENARIO_PROFILE_DELETE;
134
        $model->status = $model::STATUS_DELETED;
135
        if ($model->save())
136
            Yii::$app->user->logout();
137
        return $this->goHome();
138
    }
139
140
    /**
141
     * Generate new auth key
142
     * @throws NotFoundHttpException
143
     */
144
    public function actionGenerateAuthKey()
145
    {
146
        $model = $this->findModel();
147
        $model->generateAuthKey();
148
        $model->save();
149
        $this->redirect(['index']);
150
    }
151
152
    /**
153
     * Finds the User model based on its primary key value.
154
     * If the model is not found, a 404 HTTP exception will be thrown.
155
     * @return null|User the loaded model
156
     * @throws NotFoundHttpException if the model cannot be found
157
     */
158
    private function findModel()
159
    {
160
        if (!Yii::$app->user->isGuest) {
161
            /** @var object $identity */
162
            $identity = Yii::$app->user->identity;
163
            if (($model = User::findOne($identity->id)) !== null) {
164
                return $model;
165
            }
166
        }
167
        throw new NotFoundHttpException(Module::t('module', 'The requested page does not exist.'));
168
    }
169
}
170