Completed
Push — master ( c94855...6d1490 )
by Alexey
04:43
created

ProfileController::actionIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
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(),
0 ignored issues
show
Deprecated Code introduced by
The function yii\base\BaseObject::className() has been deprecated: since 2.0.14. On PHP >=5.5, use `::class` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

30
                'class' => /** @scrutinizer ignore-deprecated */ VerbFilter::className(),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
31
                'actions' => [
32
                    'delete' => ['post'],
33
                ],
34
            ],
35
            'access' => [
36
                'class' => AccessControl::className(),
0 ignored issues
show
Deprecated Code introduced by
The function yii\base\BaseObject::className() has been deprecated: since 2.0.14. On PHP >=5.5, use `::class` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

36
                'class' => /** @scrutinizer ignore-deprecated */ AccessControl::className(),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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.'));
0 ignored issues
show
Bug introduced by
The method setFlash() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
            Yii::$app->session->/** @scrutinizer ignore-call */ 
87
                                setFlash('success', Module::t('module', 'Profile successfully changed.'));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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();
0 ignored issues
show
Bug introduced by
The method logout() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

136
            Yii::$app->user->/** @scrutinizer ignore-call */ 
137
                             logout();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
137
        return $this->goHome();
138
    }
139
140
    /**
141
     * Action Generate new auth key
142
     * @throws NotFoundHttpException
143
     */
144
    public function actionGenerateAuthKey()
145
    {
146
        $model = $this->processGenerateAuthKey();
147
        if (Yii::$app->request->isAjax) {
148
            Yii::$app->response->format = Response::FORMAT_JSON;
149
            return [
150
                'success' => $model->auth_key,
151
            ];
152
        }
153
        return $this->redirect(['index']);
154
    }
155
156
    /**
157
     * Generate new auth key
158
     * @return User|null
159
     * @throws NotFoundHttpException
160
     */
161
    private function processGenerateAuthKey()
162
    {
163
        $model = $this->findModel();
164
        $model->generateAuthKey();
165
        $model->save();
166
        return $model;
167
    }
168
169
    /**
170
     * Finds the User model based on its primary key value.
171
     * If the model is not found, a 404 HTTP exception will be thrown.
172
     * @return null|User the loaded model
173
     * @throws NotFoundHttpException if the model cannot be found
174
     */
175
    private function findModel()
176
    {
177
        if (!Yii::$app->user->isGuest) {
178
            /** @var object $identity */
179
            $identity = Yii::$app->user->identity;
180
            if (($model = User::findOne($identity->id)) !== null) {
181
                return $model;
182
            }
183
        }
184
        throw new NotFoundHttpException(Module::t('module', 'The requested page does not exist.'));
185
    }
186
}
187