Completed
Push — master ( 80545a...d106c9 )
by Igor
04:46
created

UsersController::assignRole()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php
2
3
namespace app\modules\admin\controllers;
4
5
use Yii;
6
use yii\filters\VerbFilter;
7
use yii\helpers\Url;
8
use app\helpers\Util;
9
use app\components\BaseController;
10
use app\models\User;
11
use app\models\UserProfile;
12
use app\modules\admin\models\search\UserSearch;
13
use app\modules\admin\models\forms\UserForm;
14
15
class UsersController extends BaseController
16
{
17 32
    public function behaviors()
18
    {
19
        return [
20
            'verbs' => [
21 32
                'class' => VerbFilter::className(),
22
                'actions' => [
23
                    'set-active' => ['post'],
24
                    'set-block' => ['post'],
25
                    'delete' => ['post'],
26
                    'operations' => ['post'],
27
                    'photo-upload' => ['post'],
28
                    'autocomplete' => ['post'],
29
                ],
30 32
            ],
31
        ];
32
    }
33
34 32
    public function actions()
35
    {
36
        return [
37
            'operations' => [
38
                'class' => 'app\modules\admin\controllers\common\OperationsAction',
39
                'modelName' => 'app\models\User',
40
                'operations' => [
41
                    'delete' => [],
42
                    'set-active' => ['status' => User::STATUS_ACTIVE],
43
                    'set-block' => ['status' => User::STATUS_BLOCKED]
44
                ]
45 32
            ],
46
            'set-active' => [
47
                'class' => 'app\modules\admin\controllers\common\UpdateAttributesAction',
48
                'modelName' => 'app\models\User',
49
                'attributes' => ['status' => User::STATUS_ACTIVE],
50
            ],
51
            'set-block' => [
52
                'class' => 'app\modules\admin\controllers\common\UpdateAttributesAction',
53
                'modelName' => 'app\models\User',
54
                'attributes' => ['status' => User::STATUS_BLOCKED],
55
            ],
56
            'delete' => [
57
                'class' => 'app\modules\admin\controllers\common\DeleteAction',
58
                'modelName' => 'app\models\User',
59
            ],
60
            'photo-upload' => [
61
                'class'     => 'rkit\filemanager\actions\UploadAction',
62
                'modelName' => 'app\models\UserProfile',
63
                'attribute' => 'photo',
64
                'inputName' => 'file',
65
                'type'      => 'image',
66
            ],
67
        ];
68
    }
69
70 32
    public function actionIndex()
71
    {
72 32
        $userSearch = new UserSearch();
73 32
        $dataProvider = $userSearch->search(Yii::$app->request->get());
74 32
        $statuses = User::getStatuses();
75
76 32
        return $this->render('index', [
77 32
            'userSearch' => $userSearch,
78 32
            'dataProvider' => $dataProvider,
79 32
            'statuses' => $statuses,
80 32
            'roles' => Yii::$app->authManager->getRoles()
81
        ]);
82
    }
83
84 9
    public function actionEdit($id = null)
85
    {
86 9
        $model = new UserForm();
87
88 9
        if ($id) {
89 5
            $model = $this->loadModel($model, $id);
90
        }
91
92 9
        if (Yii::$app->request->isPost) {
93 6
            if ($model->isNewRecord) {
94 4
                $model->setConfirmed();
95
            }
96
97 6
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
98 4
                $this->assignRole($model);
99
100 4
                Yii::$app->session->setFlash('success', Yii::t('app.messages', 'Saved successfully'));
101 4
                $urlToModel = Url::toRoute(['edit', 'id' => $model->id]);
102 4
                if (Yii::$app->request->isAjax) {
103 1
                    return $this->response(['redirect' => $urlToModel]);
104
                }
105 3
                return $this->redirect($urlToModel);
106
            }
107 2
            if (Yii::$app->request->isAjax) {
108 1
                return $this->response(Util::collectModelErrors($model));
0 ignored issues
show
Bug introduced by
It seems like $model defined by new \app\modules\admin\models\forms\UserForm() on line 86 can also be of type object<app\modules\admin\models\forms\UserForm>; however, app\helpers\Util::collectModelErrors() does only seem to accept object<app\helpers\Model>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
109
            }
110
        }
111
112 8
        return $this->render('edit', [
113 8
            'model' => $model,
114 8
            'roles' => Yii::$app->authManager->getRoles()
115
        ]);
116
    }
117
118 4
    public function actionProfile($id)
119
    {
120 4
        $model = $this->loadModel(new UserProfile(), $id);
121
122 4
        if (Yii::$app->request->isPost) {
123 4
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
124 2
                Yii::$app->session->setFlash('success', Yii::t('app.messages', 'Saved successfully'));
125 2
                $urlToModel = Url::toRoute(['profile', 'id' => $model->user_id]);
126 2
                if (Yii::$app->request->isAjax) {
127 1
                    return $this->response(['redirect' => $urlToModel]);
128
                }
129 1
                return $this->redirect($urlToModel);
130
            }
131 2
            if (Yii::$app->request->isAjax) {
132 1
                return $this->response(Util::collectModelErrors($model));
133
            }
134
        }
135
136 2
        return $this->render('profile', [
137 2
            'model' => $model
138
        ]);
139
    }
140
141 1
    public function actionAutocomplete()
142
    {
143 1
        $result = [];
144 1
        if (($term = Yii::$app->request->post('term')) !== null) {
145 1
            $data = User::find()
146 1
                ->like('username', $term)
147 1
                ->asArray()
148 1
                ->limit(10)
149 1
                ->all();
150
151 1
            foreach ($data as $item) {
152 1
                $result[] = [
153 1
                    'text' => $item['username'],
154 1
                    'id' => $item['id']
155
                ];
156
            }
157
        }
158 1
        return $this->response($result);
159
    }
160
161 4
    private function assignRole($model)
162
    {
163 4
        $auth = Yii::$app->authManager;
164 4
        $auth->revokeAll($model->id);
165
166 4
        if (!empty($model->role)) {
167 1
            $role = $auth->getRole($model->role);
168 1
            if ($role) {
169 1
                $auth->assign($role, $model->id);
170
            }
171
        }
172 4
    }
173
}
174