ItemController::labels()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace toir427\admin\components;
4
5
use Yii;
6
use toir427\admin\models\AuthItem;
7
use toir427\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
    public function actionIndex()
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
    public function actionDelete($id)
109
    {
110
        $model = $this->findModel($id);
111
        Configs::authManager()->remove($model->item);
112
        Helper::invalidate();
113
114
        return $this->redirect(['index']);
115
    }
116
117
    /**
118
     * Assign items
119
     * @param string $id
120
     * @return array
121
     */
122
    public function actionAssign($id)
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 or remove items
134
     * @param string $id
135
     * @return array
136
     */
137
    public function actionRemove($id)
138
    {
139
        $items = Yii::$app->getRequest()->post('items', []);
140
        $model = $this->findModel($id);
141
        $success = $model->removeChildren($items);
142
        Yii::$app->getResponse()->format = 'json';
143
144
        return array_merge($model->getItems(), ['success' => $success]);
145
    }
146
147
    /**
148
     * @inheritdoc
149
     */
150
    public function getViewPath()
151
    {
152
        return $this->module->getViewPath() . DIRECTORY_SEPARATOR . 'item';
153
    }
154
155
    /**
156
     * Label use in view
157
     * @throws NotSupportedException
158
     */
159
    public function labels()
160
    {
161
        throw new NotSupportedException(get_class($this) . ' does not support labels().');
162
    }
163
164
    /**
165
     * Type of Auth Item.
166
     * @return integer
167
     */
168
    public function getType()
169
    {
170
        
171
    }
172
173
    /**
174
     * Finds the AuthItem model based on its primary key value.
175
     * If the model is not found, a 404 HTTP exception will be thrown.
176
     * @param string $id
177
     * @return AuthItem the loaded model
178
     * @throws NotFoundHttpException if the model cannot be found
179
     */
180
    protected function findModel($id)
181
    {
182
        $auth = Configs::authManager();
183
        $item = $this->type === Item::TYPE_ROLE ? $auth->getRole($id) : $auth->getPermission($id);
184
        if ($item) {
185
            return new AuthItem($item);
186
        } else {
187
            throw new NotFoundHttpException('The requested page does not exist.');
188
        }
189
    }
190
}
191