Skip to content

Commit ed80fc6

Browse files
committed
The actions of controller for Items moved to ItemController.php
Added json ContentNegotiator
1 parent 65e73ae commit ed80fc6

File tree

4 files changed

+176
-134
lines changed

4 files changed

+176
-134
lines changed

src/assets/files/js/app.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,11 +544,10 @@ d3.xhr(routes.items).get(function (error, XMLHttpRequest) {
544544
});
545545
}
546546

547+
//TODO: reset form after rename item. Because second submit after first submit doesn't work
547548
d3.select('#submitForm').on('click', function () {
548549
$.post(routes.saveItem, $("#mainForm").serialize())
549-
.success(function (data) {
550-
var node = JSON.parse(data);
551-
550+
.success(function (node) {
552551
if (node.oldName) {
553552
json.nodes.forEach(function (n, i) {
554553
if (n.name === node.oldName) {

src/controllers/DefaultController.php

Lines changed: 0 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22
namespace githubjeka\rbac\controllers;
33

44
use yii\web\Controller;
5-
use Yii;
6-
use githubjeka\rbac\models\ItemForm;
7-
use \yii\helpers\Json;
8-
use yii\helpers\Html;
9-
use yii\web\HttpException;
10-
use yii\helpers\ArrayHelper;
115

126
class DefaultController extends Controller
137
{
@@ -16,124 +10,4 @@ public function actionIndex()
1610
$this->layout = "main.php";
1711
return $this->render('index');
1812
}
19-
20-
public function actionItems()
21-
{
22-
$roles = Yii::$app->authManager->getRoles();
23-
$permissions = Yii::$app->authManager->getPermissions();
24-
25-
$items = ArrayHelper::merge($roles, $permissions);
26-
27-
$links = [];
28-
29-
$_keys = array_keys($items);
30-
foreach ($items as $np => $oP) {
31-
foreach ($c = Yii::$app->authManager->getChildren($np) as $nC => $oC) {
32-
$links[] = [
33-
'source' => array_search($np, $_keys),
34-
'target' => array_search($nC, $_keys),
35-
];
36-
}
37-
}
38-
39-
return Json::encode(
40-
['nodes' => array_values($items), 'links' => $links]
41-
);
42-
}
43-
44-
/**
45-
* Action of create or update item.
46-
**/
47-
public function actionSaveItem()
48-
{
49-
50-
$item = null;
51-
52-
if (isset($_POST['ItemForm']['oldName'])) {
53-
$item = $this->findItem($_POST['ItemForm']['oldName']);
54-
}
55-
56-
$model = new ItemForm($item);
57-
58-
if ($model->load(Yii::$app->request->post()) && $model->save()) {
59-
return Json::encode($model);
60-
}
61-
62-
throw new HttpException(406, Json::encode($model->getErrors()));
63-
}
64-
65-
/**
66-
* Deletes an existing Item.
67-
* @param string $id
68-
* @return mixed
69-
*/
70-
public function actionDeleteItem()
71-
{
72-
if (isset($_POST['ItemForm']['oldName'])) {
73-
74-
$item = $this->findItem($_POST['ItemForm']['oldName']);
75-
76-
if ($item !== null) {
77-
return Yii::$app->getAuthManager()->remove($item);
78-
}
79-
}
80-
}
81-
82-
/**
83-
* Remove child item
84-
*/
85-
public function actionAddChild()
86-
{
87-
$post = Yii::$app->getRequest()->post();
88-
89-
if (isset($post['source']['name'], $post['target']['name'])) {
90-
$source = $this->findItem($post['source']['name']);
91-
$target = $this->findItem($post['target']['name']);
92-
93-
if ($source !== null && $target !== null && !Yii::$app->getAuthManager()->hasChild($source, $target)) {
94-
Yii::$app->getAuthManager()->addChild($source, $target);
95-
} else {
96-
throw new HttpException(406);
97-
}
98-
}
99-
100-
}
101-
102-
103-
/**
104-
* Remove child item
105-
*/
106-
public function actionRemoveChild()
107-
{
108-
$post = Yii::$app->getRequest()->post();
109-
110-
if (isset($post['source']['name'], $post['target']['name'])) {
111-
$source = $this->findItem($post['source']['name']);
112-
$target = $this->findItem($post['target']['name']);
113-
114-
if ($source !== null && $target !== null && Yii::$app->getAuthManager()->hasChild($source, $target)) {
115-
Yii::$app->getAuthManager()->removeChild($source, $target);
116-
} else {
117-
throw new HttpException(406);
118-
}
119-
}
120-
121-
}
122-
123-
protected function findItem($name)
124-
{
125-
$item = null;
126-
127-
if (!empty($name)) {
128-
$item = Yii::$app->getAuthManager()->getRole($name);
129-
if ($item === null) {
130-
$item = Yii::$app->getAuthManager()->getPermission($name);
131-
if ($item === null) {
132-
throw new HttpException(404);
133-
}
134-
}
135-
}
136-
137-
return $item;
138-
}
13913
}

src/controllers/ItemController.php

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<?php
2+
namespace githubjeka\rbac\controllers;
3+
4+
use yii\filters\ContentNegotiator;
5+
use yii\filters\VerbFilter;
6+
use yii\web\Controller;
7+
use Yii;
8+
use githubjeka\rbac\models\ItemForm;
9+
use yii\web\HttpException;
10+
use yii\helpers\ArrayHelper;
11+
use yii\web\Response;
12+
13+
/**
14+
* Class ItemController
15+
* Represent class for work with CRUD operations by Item
16+
*/
17+
class ItemController extends Controller
18+
{
19+
/**
20+
* @inheritdoc
21+
*/
22+
public function behaviors()
23+
{
24+
return [
25+
'contentNegotiator' => [
26+
'class' => ContentNegotiator::className(),
27+
'formats' => [
28+
'application/json' => Response::FORMAT_JSON,
29+
],
30+
],
31+
'verbs' => [
32+
'class' => VerbFilter::className(),
33+
'actions' => [
34+
'save' => ['post'],
35+
'delete' => ['post'],
36+
],
37+
],
38+
39+
];
40+
}
41+
42+
/**
43+
* Returns an array of nodes(roles and permissions) and links by them
44+
* @return array
45+
*/
46+
public function actionList()
47+
{
48+
$roles = Yii::$app->authManager->getRoles();
49+
$permissions = Yii::$app->authManager->getPermissions();
50+
51+
$items = ArrayHelper::merge($roles, $permissions);
52+
53+
$links = [];
54+
55+
$_keys = array_keys($items);
56+
foreach ($items as $np => $oP) {
57+
foreach ($c = Yii::$app->authManager->getChildren($np) as $nC => $oC) {
58+
$links[] = [
59+
'source' => array_search($np, $_keys),
60+
'target' => array_search($nC, $_keys),
61+
];
62+
}
63+
}
64+
65+
return ['nodes' => array_values($items), 'links' => $links];
66+
}
67+
68+
/**
69+
* Action of create or update a item(role or permission).
70+
**/
71+
public function actionSave()
72+
{
73+
$item = null;
74+
75+
if (isset($_POST['ItemForm']['oldName'])) {
76+
$item = $this->findItem($_POST['ItemForm']['oldName']);
77+
}
78+
79+
$model = new ItemForm($item);
80+
81+
if (!$model->load(Yii::$app->request->post())) {
82+
Yii::$app->response->setStatusCode(406);
83+
return ['errors' => ['Wrong Post datum']];
84+
}
85+
86+
if (!$model->load(Yii::$app->request->post()) || !$model->save()) {
87+
Yii::$app->response->setStatusCode(406);
88+
return ['errors' => $model->getErrors()];
89+
}
90+
91+
return $model;
92+
}
93+
94+
/**
95+
* Deletes an existing Item.
96+
* @return mixed
97+
*/
98+
public function actionDelete()
99+
{
100+
$postData = Yii::$app->request->post('ItemForm');
101+
102+
if (isset($postData['oldName'])) {
103+
104+
$item = $this->findItem($postData['oldName']);
105+
106+
if ($item !== null) {
107+
return Yii::$app->getAuthManager()->remove($item);
108+
}
109+
}
110+
}
111+
112+
/**
113+
* Remove child item
114+
*/
115+
public function actionAddChild()
116+
{
117+
$post = Yii::$app->getRequest()->post();
118+
119+
if (isset($post['source']['name'], $post['target']['name'])) {
120+
$source = $this->findItem($post['source']['name']);
121+
$target = $this->findItem($post['target']['name']);
122+
123+
if ($source !== null && $target !== null && !Yii::$app->getAuthManager()->hasChild($source, $target)) {
124+
Yii::$app->getAuthManager()->addChild($source, $target);
125+
} else {
126+
throw new HttpException(406);
127+
}
128+
}
129+
130+
}
131+
132+
133+
/**
134+
* Remove child item
135+
*/
136+
public function actionRemoveChild()
137+
{
138+
$post = Yii::$app->getRequest()->post();
139+
140+
if (isset($post['source']['name'], $post['target']['name'])) {
141+
$source = $this->findItem($post['source']['name']);
142+
$target = $this->findItem($post['target']['name']);
143+
144+
if ($source !== null && $target !== null && Yii::$app->getAuthManager()->hasChild($source, $target)) {
145+
Yii::$app->getAuthManager()->removeChild($source, $target);
146+
} else {
147+
throw new HttpException(406);
148+
}
149+
}
150+
151+
}
152+
153+
protected function findItem($name)
154+
{
155+
$item = null;
156+
157+
if (!empty($name)) {
158+
$item = Yii::$app->getAuthManager()->getRole($name);
159+
if ($item === null) {
160+
$item = Yii::$app->getAuthManager()->getPermission($name);
161+
if ($item === null) {
162+
throw new HttpException(404);
163+
}
164+
}
165+
}
166+
167+
return $item;
168+
}
169+
}

src/views/default/index.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
/* @var $this View */
99

1010
$routes = Json::encode([
11-
'items' => Url::to(['items']),
12-
'saveItem' => Url::to(['save-item']),
13-
'deleteItem' => Url::to(['delete-item']),
14-
'addChild' => Url::to(['add-child']),
15-
'removeChild' => Url::to(['remove-child']),
11+
'items' => Url::to(['item/list']),
12+
'saveItem' => Url::to(['item/save']),
13+
'deleteItem' => Url::to(['item/delete']),
14+
'addChild' => Url::to(['item/add-child']),
15+
'removeChild' => Url::to(['item/remove-child']),
1616
]);
1717
$this->registerJs("var routes = $routes;", View::POS_BEGIN);
1818
?>

0 commit comments

Comments
 (0)