Completed
Push — master ( 55e0f0...d0f882 )
by Alexey
11:22
created

ProfileController   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 9
dl 0
loc 139
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 0 21 1
A actionIndex() 0 11 1
A actionUpdate() 0 7 1
A actionUpdateProfile() 0 12 3
B actionUpdatePassword() 0 17 5
A actionDelete() 0 9 2
A actionGenerateAuthKey() 0 7 1
A findModel() 0 11 3
1
<?php
2
namespace modules\users\controllers\frontend;
3
4
use Yii;
5
use yii\web\Controller;
6
use modules\users\models\User;
7
use modules\rbac\models\Assignment;
8
use yii\filters\AccessControl;
9
use yii\filters\VerbFilter;
10
use yii\web\NotFoundHttpException;
11
use yii\bootstrap\ActiveForm;
12
use yii\web\Response;
13
use modules\users\Module;
14
15
/**
16
 * Class ProfileController
17
 * @package modules\users\controllers\frontend
18
 */
19
class ProfileController extends Controller
20
{
21
    /**
22
     * @return array
23
     */
24
    public function behaviors()
25
    {
26
        return [
27
            'access' => [
28
                'class' => AccessControl::className(),
29
                'rules' => [
30
                    [
31
                        'allow' => true,
32
                        'roles' => ['@']
33
                    ],
34
                ],
35
            ],
36
            'verbs' => [
37
                'class' => VerbFilter::className(),
38
                'actions' => [
39
                    'logout' => ['post'],
40
                    'delete' => ['post'],
41
                ],
42
            ],
43
        ];
44
    }
45
46
    /**
47
     * @return string
48
     * @throws NotFoundHttpException
49
     */
50
    public function actionIndex()
51
    {
52
        $model = $this->findModel();
53
        $assignModel = new Assignment([
54
            'user' => $model
55
        ]);
56
        return $this->render('index', [
57
            'model' => $model,
58
            'assignModel' => $assignModel,
59
        ]);
60
    }
61
62
    /**
63
     * @return string|Response
64
     * @throws NotFoundHttpException
65
     */
66
    public function actionUpdate()
67
    {
68
        $model = $this->findModel();
69
        return $this->render('update', [
70
            'model' => $model,
71
        ]);
72
    }
73
74
    /**
75
     * @return string|Response
76
     * @throws NotFoundHttpException
77
     */
78
    public function actionUpdateProfile()
79
    {
80
        $model = $this->findModel();
81
        $model->scenario = $model::SCENARIO_PROFILE_UPDATE;
82
83
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
84
            Yii::$app->session->setFlash('success', Module::t('module', 'Profile successfully changed.'));
85
        } else {
86
            Yii::$app->session->setFlash('error', Module::t('module', 'Error! Profile not changed.'));
87
        }
88
        return $this->redirect(['update', 'tab' => 'profile']);
89
    }
90
91
    /**
92
     * @return array|string|Response
93
     * @throws NotFoundHttpException
94
     */
95
    public function actionUpdatePassword()
96
    {
97
        $model = $this->findModel();
98
        $model->scenario = $model::SCENARIO_PASSWORD_UPDATE;
99
100
        if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
101
            Yii::$app->response->format = Response::FORMAT_JSON;
102
            return ActiveForm::validate($model);
103
        }
104
105
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
106
            Yii::$app->session->setFlash('success', Module::t('module', 'Password changed successfully.'));
107
        } else {
108
            Yii::$app->session->setFlash('error', Module::t('module', 'Error! Password changed not successfully.'));
109
        }
110
        return $this->redirect(['update', 'tab' => 'password']);
111
    }
112
113
    /**
114
     * Deletes an existing User model.
115
     * This delete set status blocked, is successful, logout and the browser will be redirected to the 'home' page.
116
     * @return mixed
117
     */
118
    public function actionDelete()
119
    {
120
        $model = $this->findModel();
121
        $model->scenario = $model::SCENARIO_PROFILE_DELETE;
122
        $model->status = $model::STATUS_DELETED;
123
        if ($model->save())
124
            Yii::$app->user->logout();
125
        return $this->goHome();
126
    }
127
128
    /**
129
     * Generate new auth key
130
     * @throws NotFoundHttpException
131
     */
132
    public function actionGenerateAuthKey()
133
    {
134
        $model = $this->findModel();
135
        $model->generateAuthKey();
136
        $model->save();
137
        $this->redirect('index');
138
    }
139
140
    /**
141
     * Finds the User model based on its primary key value.
142
     * If the model is not found, a 404 HTTP exception will be thrown.
143
     * @return User the loaded model
144
     * @throws NotFoundHttpException if the model cannot be found
145
     */
146
    protected function findModel()
147
    {
148
        if (!Yii::$app->user->isGuest) {
149
            /** @var \modules\users\models\User $identity */
150
            $identity = Yii::$app->user->identity;
151
            if (($model = User::findOne($identity->id)) !== null) {
0 ignored issues
show
Bug Compatibility introduced by
The expression \modules\users\models\Us...findOne($identity->id); of type yii\db\ActiveRecordInterface|array|null adds the type array to the return on line 152 which is incompatible with the return type documented by modules\users\controller...leController::findModel of type modules\users\models\User.
Loading history...
152
                return $model;
153
            }
154
        }
155
        throw new NotFoundHttpException(Module::t('module', 'The requested page does not exist.'));
156
    }
157
}
158