Passed
Push — master ( 399152...14bbe3 )
by Alexey
02:45
created

DefaultController::actionUpdatePassword()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 12
rs 9.2
cc 4
eloc 7
nc 3
nop 1
1
<?php
2
3
namespace modules\users\controllers\backend;
4
5
use Yii;
6
use modules\users\models\User;
7
use modules\users\models\search\UserSearch;
8
use yii\web\NotFoundHttpException;
9
use modules\rbac\models\Assignment;
10
use modules\users\Module;
11
12
/**
13
 * Class DefaultController
14
 * @package modules\users\controllers\backend
15
 */
16
class DefaultController extends BaseController
17
{
18
    /**
19
     * Lists all User models.
20
     * @return mixed
21
     */
22
    public function actionIndex()
23
    {
24
        $searchModel = new UserSearch();
25
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
26
        $assignModel = new Assignment();
27
        return $this->render('index', [
28
            'searchModel' => $searchModel,
29
            'dataProvider' => $dataProvider,
30
            'assignModel' => $assignModel,
31
        ]);
32
    }
33
34
    /**
35
     * Displays a single User model.
36
     * @param int|string $id
37
     * @return string|\yii\web\Response
38
     * @throws NotFoundHttpException
39
     */
40
    public function actionView($id)
41
    {
42
        if ($model = $this->findModel($id)) {
43
            $assignModel = new Assignment([
44
                'user' => $model
45
            ]);
46
            return $this->render('view', [
47
                'model' => $model,
48
                'assignModel' => $assignModel,
49
            ]);
50
        }
51
        return $this->redirect(['index']);
52
    }
53
54
    /**
55
     * Creates a new User model.
56
     * If creation is successful, the browser will be redirected to the 'view' page.
57
     * @return string|\yii\web\Response
58
     */
59
    public function actionCreate()
60
    {
61
        $model = new User();
62
        $model->status = $model::STATUS_WAIT;
63
        /** @var \modules\users\models\User $identity */
64
        $identity = Yii::$app->user->identity;
65
        $model->registration_type = $identity->id;
66
67
        if ($model->load(Yii::$app->request->post())) {
68
            if ($model->save()) {
69
                return $this->redirect(['view', 'id' => $model->id]);
70
            }
71
        }
72
73
        $model->scenario = $model::SCENARIO_ADMIN_CREATE;
74
        return $this->render('create', [
75
            'model' => $model,
76
        ]);
77
    }
78
79
    /**
80
     * Updates an existing User model.
81
     * @param int|string $id
82
     * @return string|\yii\web\Response
83
     * @throws NotFoundHttpException
84
     */
85
    public function actionUpdate($id)
86
    {
87
        if ($model = $this->findModel($id)) {
88
            return $this->render('update', [
89
                'model' => $model,
90
            ]);
91
        }
92
        return $this->redirect(['index']);
93
    }
94
95
    /**
96
     * @param int|string $id
97
     * @return \yii\web\Response
98
     * @throws NotFoundHttpException
99
     */
100
    public function actionUpdateProfile($id)
101
    {
102
        if ($model = $this->findModel($id)) {
103
            $model->scenario = $model::SCENARIO_ADMIN_UPDATE;
104
105
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
106
                Yii::$app->session->setFlash('success', Module::t('module', 'Profile successfully changed.'));
107
            } else {
108
                Yii::$app->session->setFlash('error', Module::t('module', 'Error! Profile not changed.'));
109
            }
110
        }
111
        return $this->redirect(['update', 'id' => $model->id, 'tab' => 'profile']);
112
    }
113
114
    /**
115
     * @param int|string $id
116
     * @return \yii\web\Response
117
     * @throws NotFoundHttpException
118
     */
119
    public function actionUpdatePassword($id)
120
    {
121
        if ($model = $this->findModel($id)) {
122
            $model->scenario = $model::SCENARIO_ADMIN_PASSWORD_UPDATE;
123
124
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
125
                Yii::$app->session->setFlash('success', Module::t('module', 'Password changed successfully.'));
126
            } else {
127
                Yii::$app->session->setFlash('error', Module::t('module', 'Error! Password changed not successfully.'));
128
            }
129
        }
130
        return $this->redirect(['update', 'id' => $model->id, 'tab' => 'password']);
131
    }
132
133
    /**
134
     * Change Status
135
     * @param int|string $id
136
     * @return array|\yii\web\Response
137
     * @throws NotFoundHttpException
138
     */
139
    public function actionStatus($id)
140
    {
141
        if (Yii::$app->request->isAjax) {
142
            if ($model = $this->findModel($id)) {
143
                Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
144
                /**
145
                 * Запрещаем менять статус у себя
146
                 * @var object $identity
147
                 */
148
                $identity = Yii::$app->user->identity;
149
                if ($model->id !== $identity->id && !$model->isSuperAdmin($model->id)) {
150
                    $model->setStatus();
151
                    if ($model->save()) {
152
                        return [
153
                            'body' => $model->getStatusLabelName(),
154
                            'success' => true,
155
                        ];
156
                    }
157
                }
158
            }
159
        }
160
        return $this->redirect(['index']);
161
    }
162
163
    /**
164
     * Deletes an existing User model.
165
     * If deletion is successful, the browser will be redirected to the 'index' page.
166
     * @param int|string $id
167
     * @return mixed
168
     */
169
    public function actionDelete($id)
170
    {
171
        /** @var \modules\users\models\User $model */
172
        $model = $this->findModel($id);
173
        // Запрещаем удалять самого себя
174
        /** @var object $identity */
175
        $identity = Yii::$app->user->identity;
176
        if ($model->id !== $identity->id) {
177
            if ($model->isDeleted()) {
178
                if ($model->delete() !== false) {
179
                    Yii::$app->session->setFlash('success', Module::t('module', 'The user "{:name}" have been successfully deleted.', [':name' => $model->username]));
180
                }
181
            } else {
182
                $model->scenario = User::SCENARIO_PROFILE_DELETE;
183
                $model->status = User::STATUS_DELETED;
184
                if ($model->save()) {
185
                    Yii::$app->session->setFlash('success', Module::t('module', 'The user "{:name}" are marked as deleted.', [':name' => $model->username]));
186
                }
187
            }
188
        } else {
189
            Yii::$app->session->setFlash('warning', Module::t('module', 'You can not remove yourself.'));
190
        }
191
        return $this->redirect(['index']);
192
    }
193
}
194