Passed
Push — master ( 5b0966...10e526 )
by Misbahul D
02:41
created

ItemController::actionGetUsers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace mdm\admin\components;
4
5
use Yii;
6
use mdm\admin\models\AuthItem;
7
use mdm\admin\models\searchs\AuthItem as AuthItemSearch;
8
use yii\web\Controller;
9
use yii\web\NotFoundHttpException;
10
use yii\base\NotSupportedException;
11
use yii\filters\VerbFilter;
12
use yii\rbac\Item;
13
14
/**
15
 * AuthItemController implements the CRUD actions for AuthItem model.
16
 *
17
 * @property integer $type
18
 * @property array $labels
19
 * 
20
 * @author Misbahul D Munir <[email protected]>
21
 * @since 1.0
22
 */
23
class ItemController extends Controller
24
{
25
26
    /**
27
     * @inheritdoc
28
     */
29
    public function behaviors()
30
    {
31
        return [
32
            'verbs' => [
33
                'class' => VerbFilter::className(),
34
                'actions' => [
35
                    'delete' => ['post'],
36
                    'assign' => ['post'],
37
                    'remove' => ['post'],
38
                ],
39
            ],
40
        ];
41
    }
42
43
    /**
44
     * Lists all AuthItem models.
45
     * @return mixed
46
     */
47 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...
48
    {
49
        $searchModel = new AuthItemSearch(['type' => $this->type]);
50
        $dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());
51
52
        return $this->render('index', [
53
            'dataProvider' => $dataProvider,
54
            'searchModel' => $searchModel,
55
        ]);
56
    }
57
58
    /**
59
     * Displays a single AuthItem model.
60
     * @param  string $id
61
     * @return mixed
62
     */
63
    public function actionView($id)
64
    {
65
        $model = $this->findModel($id);
66
67
        return $this->render('view', ['model' => $model]);
68
    }
69
70
    /**
71
     * Creates a new AuthItem model.
72
     * If creation is successful, the browser will be redirected to the 'view' page.
73
     * @return mixed
74
     */
75
    public function actionCreate()
76
    {
77
        $model = new AuthItem(null);
78
        $model->type = $this->type;
79
        if ($model->load(Yii::$app->getRequest()->post()) && $model->save()) {
80
            return $this->redirect(['view', 'id' => $model->name]);
81
        } else {
82
            return $this->render('create', ['model' => $model]);
83
        }
84
    }
85
86
    /**
87
     * Updates an existing AuthItem model.
88
     * If update is successful, the browser will be redirected to the 'view' page.
89
     * @param  string $id
90
     * @return mixed
91
     */
92
    public function actionUpdate($id)
93
    {
94
        $model = $this->findModel($id);
95
        if ($model->load(Yii::$app->getRequest()->post()) && $model->save()) {
96
            return $this->redirect(['view', 'id' => $model->name]);
97
        }
98
99
        return $this->render('update', ['model' => $model]);
100
    }
101
102
    /**
103
     * Deletes an existing AuthItem model.
104
     * If deletion is successful, the browser will be redirected to the 'index' page.
105
     * @param  string $id
106
     * @return mixed
107
     */
108 View Code Duplication
    public function actionDelete($id)
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 = $this->findModel($id);
111
        Configs::authManager()->remove($model->item);
0 ignored issues
show
Documentation introduced by
$model->item is of type object<yii\rbac\Item>, but the function expects a object<yii\rbac\Role>|ob...>|object<yii\rbac\Rule>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
112
        Helper::invalidate();
113
114
        return $this->redirect(['index']);
115
    }
116
117
    /**
118
     * Assign items
119
     * @param string $id
120
     * @return array
121
     */
122 View Code Duplication
    public function actionAssign($id)
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...
123
    {
124
        $items = Yii::$app->getRequest()->post('items', []);
125
        $model = $this->findModel($id);
126
        $success = $model->addChildren($items);
127
        Yii::$app->getResponse()->format = 'json';
128
129
        return array_merge($model->getItems(), ['success' => $success]);
130
    }
131
132
    /**
133
     * Assign items
134
     * @param string $id
135
     * @return array
136
     */
137
    public function actionGetUsers($id)
138
    {
139
        $page = Yii::$app->getRequest()->get('page', 0);
140
        $model = $this->findModel($id);
141
        Yii::$app->getResponse()->format = 'json';
142
143
        return array_merge($model->getUsers($page));
0 ignored issues
show
Unused Code introduced by
The call to AuthItem::getUsers() has too many arguments starting with $page.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
144
    }
145
146
    /**
147
     * Assign or remove items
148
     * @param string $id
149
     * @return array
150
     */
151 View Code Duplication
    public function actionRemove($id)
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...
152
    {
153
        $items = Yii::$app->getRequest()->post('items', []);
154
        $model = $this->findModel($id);
155
        $success = $model->removeChildren($items);
156
        Yii::$app->getResponse()->format = 'json';
157
158
        return array_merge($model->getItems(), ['success' => $success]);
159
    }
160
161
    /**
162
     * @inheritdoc
163
     */
164
    public function getViewPath()
165
    {
166
        return $this->module->getViewPath() . DIRECTORY_SEPARATOR . 'item';
167
    }
168
169
    /**
170
     * Label use in view
171
     * @throws NotSupportedException
172
     */
173
    public function labels()
174
    {
175
        throw new NotSupportedException(get_class($this) . ' does not support labels().');
176
    }
177
178
    /**
179
     * Type of Auth Item.
180
     * @return integer
181
     */
182
    public function getType()
183
    {
184
        
185
    }
186
187
    /**
188
     * Finds the AuthItem model based on its primary key value.
189
     * If the model is not found, a 404 HTTP exception will be thrown.
190
     * @param string $id
191
     * @return AuthItem the loaded model
192
     * @throws NotFoundHttpException if the model cannot be found
193
     */
194
    protected function findModel($id)
195
    {
196
        $auth = Configs::authManager();
197
        $item = $this->type === Item::TYPE_ROLE ? $auth->getRole($id) : $auth->getPermission($id);
198
        if ($item) {
199
            return new AuthItem($item);
200
        } else {
201
            throw new NotFoundHttpException('The requested page does not exist.');
202
        }
203
    }
204
}
205