Skip to content

Commit ab24ff3

Browse files
committed
Add ability to view and create rules
1 parent b33b908 commit ab24ff3

File tree

7 files changed

+498
-1
lines changed

7 files changed

+498
-1
lines changed

controllers/RuleController.php

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Dektrium project.
5+
*
6+
* (c) Dektrium project <http://github.com/dektrium>
7+
*
8+
* For the full copyright and license information, please view the LICENSE.md
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace dektrium\rbac\controllers;
13+
14+
use dektrium\rbac\models\Rule;
15+
use dektrium\rbac\models\RuleSearch;
16+
use yii\web\Controller;
17+
use yii\web\Response;
18+
use yii\widgets\ActiveForm;
19+
20+
/**
21+
* Controller for managing rules.
22+
*
23+
* @author Dmitry Erofeev <[email protected]>
24+
*/
25+
class RuleController extends Controller
26+
{
27+
/**
28+
* Shows list of created rules.
29+
*
30+
* @return string
31+
* @throws \yii\base\InvalidConfigException
32+
*/
33+
public function actionIndex()
34+
{
35+
$searchModel = $this->getSearchModel();
36+
$dataProvider = $searchModel->search(\Yii::$app->request->queryParams);
37+
38+
return $this->render('index', [
39+
'searchModel' => $searchModel,
40+
'dataProvider' => $dataProvider,
41+
]);
42+
}
43+
44+
/**
45+
* Shows page where new rule can be added.
46+
*
47+
* @return array|string
48+
*/
49+
public function actionCreate()
50+
{
51+
$model = $this->getModel(Rule::SCENARIO_CREATE);
52+
53+
if (\Yii::$app->request->isAjax && $model->load(\Yii::$app->request->post())) {
54+
\Yii::$app->response->format = Response::FORMAT_JSON;
55+
return ActiveForm::validate($model);
56+
}
57+
58+
if ($model->load(\Yii::$app->request->post()) && $model->create()) {
59+
\Yii::$app->session->setFlash('success', \Yii::t('rbac', 'Rule has been added'));
60+
return $this->redirect(['index']);
61+
}
62+
63+
return $this->render('create', [
64+
'model' => $model,
65+
]);
66+
}
67+
68+
/**
69+
* Searches for rules.
70+
*
71+
* @param string|null $q
72+
* @return array
73+
*/
74+
public function actionSearch($q = null)
75+
{
76+
\Yii::$app->response->format = Response::FORMAT_JSON;
77+
78+
return ['results' => $this->getSearchModel()->getRuleNames($q)];
79+
}
80+
81+
/**
82+
* @param string $scenario
83+
* @return Rule
84+
* @throws \yii\base\InvalidConfigException
85+
*/
86+
private function getModel($scenario)
87+
{
88+
return \Yii::createObject([
89+
'class' => Rule::className(),
90+
'scenario' => $scenario,
91+
]);
92+
}
93+
94+
/**
95+
* @return RuleSearch
96+
* @throws \yii\base\InvalidConfigException
97+
*/
98+
private function getSearchModel()
99+
{
100+
return \Yii::createObject(RuleSearch::className());
101+
}
102+
}

models/Rule.php

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Dektrium project.
5+
*
6+
* (c) Dektrium project <http://github.com/dektrium>
7+
*
8+
* For the full copyright and license information, please view the LICENSE.md
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace dektrium\rbac\models;
13+
14+
use dektrium\rbac\components\DbManager;
15+
use yii\base\InvalidConfigException;
16+
use yii\base\Model;
17+
use yii\di\Instance;
18+
19+
/**
20+
* Rule model.
21+
*
22+
* @author Dmitry Erofeev <[email protected]>
23+
*/
24+
class Rule extends Model
25+
{
26+
const SCENARIO_CREATE = 'create';
27+
const SCENARIO_UPDATE = 'update';
28+
29+
/**
30+
* @var string
31+
*/
32+
public $name;
33+
34+
/**
35+
* @var string
36+
*/
37+
public $class;
38+
39+
/**
40+
* @var string|DbManager The auth manager component ID.
41+
*/
42+
public $authManager = 'authManager';
43+
44+
/**
45+
* This method will set [[authManager]] to be the 'authManager' application component, if it is `null`.
46+
*/
47+
public function init()
48+
{
49+
parent::init();
50+
51+
$this->authManager = Instance::ensure($this->authManager, DbManager::className());
52+
}
53+
54+
/**
55+
* @return array
56+
*/
57+
public function scenarios()
58+
{
59+
return [
60+
self::SCENARIO_CREATE => ['name', 'class'],
61+
self::SCENARIO_UPDATE => ['name', 'class'],
62+
];
63+
}
64+
65+
/**
66+
* @return array
67+
*/
68+
public function rules()
69+
{
70+
return [
71+
[['name', 'class'], 'trim'],
72+
[['name', 'class'], 'required'],
73+
['name', 'match', 'pattern' => '/^[\w][\w-.:]+[\w]$/'],
74+
['name', function () {
75+
$rule = $this->authManager->getRule($this->name);
76+
77+
if ($rule instanceof \yii\rbac\Rule) {
78+
$this->addError('name', \Yii::t('rbac', 'Name is already in use'));
79+
}
80+
}],
81+
['class', function () {
82+
if (!class_exists($this->class)) {
83+
$this->addError('class', \Yii::t('rbac', 'Class "{0}" does not exist', $this->class));
84+
} else {
85+
try {
86+
$class = '\yii\rbac\Rule';
87+
$rule = \Yii::createObject($this->class);
88+
89+
if (!($rule instanceof $class)) {
90+
$this->addError('class', \Yii::t('rbac', 'Rule class must extend "yii\rbac\Rule"'));
91+
}
92+
} catch (InvalidConfigException $e) {
93+
$this->addError('class', \Yii::t('rbac', 'Rule class can not be instantiated'));
94+
}
95+
}
96+
}],
97+
];
98+
}
99+
100+
/**
101+
* Creates new auth rule.
102+
*
103+
* @return bool
104+
* @throws InvalidConfigException
105+
*/
106+
public function create()
107+
{
108+
if ($this->scenario != self::SCENARIO_CREATE) {
109+
return false;
110+
}
111+
112+
if (!$this->validate()) {
113+
return false;
114+
}
115+
116+
$rule = \Yii::createObject([
117+
'class' => $this->class,
118+
'name' => $this->name,
119+
]);
120+
121+
$this->authManager->add($rule);
122+
123+
return true;
124+
}
125+
}

models/RuleSearch.php

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Dektrium project.
5+
*
6+
* (c) Dektrium project <http://github.com/dektrium>
7+
*
8+
* For the full copyright and license information, please view the LICENSE.md
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace dektrium\rbac\models;
13+
14+
use yii\base\Model;
15+
use yii\data\ActiveDataProvider;
16+
use yii\data\ArrayDataProvider;
17+
use yii\db\Query;
18+
19+
/**
20+
* Search model for rules.
21+
*
22+
* @author Dmitry Erofeev <[email protected]>
23+
*/
24+
class RuleSearch extends Rule
25+
{
26+
/**
27+
* @var string
28+
*/
29+
public $created_at;
30+
31+
/**
32+
* @return array
33+
*/
34+
public function scenarios()
35+
{
36+
return Model::scenarios();
37+
}
38+
39+
/**
40+
* @return array
41+
*/
42+
public function rules()
43+
{
44+
return [
45+
['name', 'string'],
46+
];
47+
}
48+
49+
/**
50+
* @param array $params
51+
* @return ArrayDataProvider
52+
*/
53+
public function search(array $params = [])
54+
{
55+
$query = (new Query())
56+
->select(['name', 'data', 'created_at', 'updated_at'])
57+
->from($this->authManager->ruleTable)
58+
->orderBy(['name' => SORT_ASC]);
59+
60+
$this->load($params);
61+
62+
if (!$this->validate()) {
63+
$query->where('0=1');
64+
}
65+
66+
$query->andFilterWhere(['name' => $this->name]);
67+
68+
return \Yii::createObject([
69+
'class' => ActiveDataProvider::className(),
70+
'query' => $query,
71+
'db' => $this->authManager->db,
72+
'sort' => [
73+
'attributes' => ['name', 'created_at', 'updated_at'],
74+
],
75+
]);
76+
}
77+
78+
/**
79+
* @param string|null $searchQuery
80+
* @return array
81+
*/
82+
public function getRuleNames($searchQuery = null)
83+
{
84+
$query = (new Query())
85+
->select(['id' => 'name', 'text' => 'name'])
86+
->from($this->authManager->ruleTable)
87+
->orderBy(['name' => SORT_ASC])
88+
->limit(10);
89+
90+
if ($searchQuery) {
91+
$query->where(['LIKE', 'LOWER(name)', mb_strtolower($searchQuery)]);
92+
}
93+
94+
return $query->all();
95+
}
96+
}

views/rule/_form.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Dektrium project.
5+
*
6+
* (c) Dektrium project <http://github.com/dektrium>
7+
*
8+
* For the full copyright and license information, please view the LICENSE.md
9+
* file that was distributed with this source code.
10+
*/
11+
12+
/**
13+
* @var $this \yii\web\View
14+
* @var $model \dektrium\rbac\models\Rule
15+
*/
16+
17+
use yii\widgets\ActiveForm;
18+
use yii\helpers\Html;
19+
20+
?>
21+
22+
<?php $form = ActiveForm::begin([
23+
'enableClientValidation' => false,
24+
'enableAjaxValidation' => true,
25+
]) ?>
26+
27+
<?= $form->field($model, 'name') ?>
28+
29+
<?= $form->field($model, 'class') ?>
30+
31+
<?= Html::submitButton(Yii::t('rbac', 'Save'), ['class' => 'btn btn-success btn-block']) ?>
32+
33+
<?php ActiveForm::end() ?>

views/rule/create.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Dektrium project.
5+
*
6+
* (c) Dektrium project <http://github.com/dektrium>
7+
*
8+
* For the full copyright and license information, please view the LICENSE.md
9+
* file that was distributed with this source code.
10+
*/
11+
12+
/**
13+
* @var $model \dektrium\rbac\models\Rule
14+
* @var $this \yii\web\View
15+
*/
16+
17+
$this->title = Yii::t('rbac', 'Create rule');
18+
$this->params['breadcrumbs'][] = ['label' => Yii::t('rbac', 'Rules'), 'url' => ['index']];
19+
$this->params['breadcrumbs'][] = $this->title;
20+
21+
?>
22+
23+
<?php $this->beginContent('@dektrium/rbac/views/layout.php') ?>
24+
25+
<?= $this->render('_form', [
26+
'model' => $model,
27+
]) ?>
28+
29+
<?php $this->endContent() ?>

0 commit comments

Comments
 (0)