UserController::findModel()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace app\controllers;
4
5
use Yii;
6
use app\models\User;
7
use app\models\UserLogin;
8
use app\models\Flow;
9
use yii\helpers\ArrayHelper;
10
use yii\data\ActiveDataProvider;
11
use yii\web\Controller;
12
use yii\web\NotFoundHttpException;
13
use yii\filters\VerbFilter;
14
use yii\filters\AccessControl;
15
16
/**
17
 * UserController implements the CRUD actions for User model.
18
 */
19
class UserController extends Controller
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function behaviors()
25
    {
26
        return [
27
            'verbs' => [
28
                'class' => VerbFilter::class,
29
                'actions' => [
30
                    'delete' => ['POST'],
31
                ],
32
            ],
33
            'access' => [
34
                'class' => AccessControl::class,
35
                'only' => ['index', 'view', 'create', 'import', 'delete', 'set-roles'],
36
                'rules' => [
37
                    ['allow' => true, 'actions' => ['index', 'view', 'create', 'delete', 'set-roles'], 'roles' => ['admin']],
38
                    ['allow' => true, 'actions' => ['import'], 'matchCallback' => function () {
39
                        return Yii::$app->params['useLdap'];
40
                    }],
41
                ],
42
            ],
43
        ];
44
    }
45
46
    /**
47
     * Lists all User models.
48
     *
49
     * @return string render
50
     */
51 View Code Duplication
    public function actionIndex()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        $dataProvider = new ActiveDataProvider([
54
            'query' => User::find(),
55
        ]);
56
57
        return $this->render('index', [
58
            'dataProvider' => $dataProvider,
59
        ]);
60
    }
61
62
    /**
63
     * Displays a single User model.
64
     *
65
     * @param string $id
66
     *
67
     * @return string render
68
     */
69
    public function actionView($id)
70
    {
71
        return $this->render('view', [
72
            'model' => $this->findModel($id),
73
        ]);
74
    }
75
76
    /**
77
     * Creates a new User model.
78
     * If creation is successful, the browser will be redirected to the 'view' page.
79
     *
80
     * @return \yii\web\Response|string redirect or render
81
     */
82 View Code Duplication
    public function actionCreate()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        $model = new UserLogin();
85
86
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
87
            if (!User::findIdentity($model->username)) {
88
                if (($user = User::create($model->username, $model->password)) !== null) {
89
                    return $this->redirect(['view', 'id' => $user->getId()]);
90
                } else {
91
                    $model->addError('username', Yii::t('app', 'User creation failed'));
92
                }
93
            } else {
94
                $model->addError('username', Yii::t('app', 'This user is already registered'));
95
            }
96
        }
97
98
        return $this->render('create', [
99
            'model' => $model,
100
        ]);
101
    }
102
103
    /**
104
     * Import user from LDAP and save it.
105
     *
106
     * @return \yii\web\Response|string redirect or render
107
     */
108 View Code Duplication
    public function actionImport()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
    {
110
        $model = new UserLogin();
111
112
        if ($model->load(Yii::$app->request->post()) && $model->validate(['username'])) {
113
            if (!User::findOne($model->username)) {
114
                if (($user = User::findInLdap($model->username)) !== null) {
115
                    $user->save(false);
116
117
                    return $this->redirect(['view', 'id' => $user->username]);
118
                } else {
119
                    $model->addError('username', Yii::t('app', 'This user doesn\'t exist in LDAP'));
120
                }
121
            } else {
122
                $model->addError('username', Yii::t('app', 'This user is already registered'));
123
            }
124
        }
125
126
        return $this->render('import', [
127
            'model' => $model,
128
        ]);
129
    }
130
131
    /**
132
     * Deletes an existing User model.
133
     * If deletion is successful, the browser will be redirected to the 'index' page.
134
     *
135
     * @param string $id
136
     *
137
     * @return \yii\web\Response
138
     */
139
    public function actionDelete($id)
140
    {
141
        $this->findModel($id)->delete();
142
143
        return $this->redirect(['index']);
144
    }
145
146
    /**
147
     * Set roles and flows for a specific user.
148
     *
149
     * @param string $id user id
150
     *
151
     * @return \yii\web\Response|string redirect or render
152
     */
153
    public function actionSetRoles($id)
154
    {
155
        $model = $this->findModel($id);
156
        $auth = Yii::$app->authManager;
157
158
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
159
            return $this->redirect(['view', 'id' => $model->username]);
160
        }
161
162
        $roles = $auth->getRoles();
163
        $rolesArray = ['' => Yii::t('app', 'None')];
164
        $flowableRoles = [];
165
        foreach ($roles as $name => $role) {
166
            $rolesArray[$name] = Yii::t('app', $name);
167
            if ($role->data && array_key_exists('requireFlow', $role->data)) {
168
                $flowableRoles[] = $name;
169
            }
170
        }
171
172
        $flows = ArrayHelper::map(Flow::find()->all(), 'id', 'name');
173
174
        return $this->render('set-roles', [
175
            'model' => $model,
176
            'roles' => $rolesArray,
177
            'flows' => $flows,
178
            'flowableRoles' => $flowableRoles,
179
        ]);
180
    }
181
182
    /**
183
     * Finds the User model based on its primary key value.
184
     * If the model is not found, a 404 HTTP exception will be thrown.
185
     *
186
     * @param string $id
187
     *
188
     * @return User the loaded model
189
     *
190
     * @throws NotFoundHttpException if the model cannot be found
191
     */
192 View Code Duplication
    protected function findModel($id)
193
    {
194
        if (($model = User::findOne($id)) !== null) {
0 ignored issues
show
Bug Compatibility introduced by
The expression \app\models\User::findOne($id); of type yii\db\ActiveRecordInterface|array|null adds the type array to the return on line 195 which is incompatible with the return type documented by app\controllers\UserController::findModel of type app\models\User.
Loading history...
195
            return $model;
196
        } else {
197
            throw new NotFoundHttpException(Yii::t('app', 'The requested user does not exist.'));
198
        }
199
    }
200
}
201