diff --git a/RbacWebModule.php b/RbacWebModule.php index 45ecd915..88222ba6 100644 --- a/RbacWebModule.php +++ b/RbacWebModule.php @@ -1,71 +1,74 @@ - - * - * For the full copyright and license information, please view the LICENSE.md - * file that was distributed with this source code. - */ - -namespace dektrium\rbac; - -use yii\base\Module as BaseModule; -use yii\filters\AccessControl; - -/** - * @author Dmitry Erofeev - */ -class RbacWebModule extends BaseModule -{ - /** - * @var string - */ - public $defaultRoute = 'role/index'; - - /** - * @var array - */ - public $admins = []; - - /** - * @var string The Administrator permission name. - */ - public $adminPermission; - - /** @inheritdoc */ - public function behaviors() - { - return [ - 'access' => [ - 'class' => AccessControl::className(), - 'rules' => [ - [ - 'allow' => true, - 'roles' => ['@'], - 'matchCallback' => [$this, 'checkAccess'], - ] - ], - ], - ]; - } - - /** - * Checks access. - * - * @return bool - */ - public function checkAccess() - { - $user = \Yii::$app->user->identity; - - if (method_exists($user, 'getIsAdmin')) { - return $user->getIsAdmin(); - } else if ($this->adminPermission) { - return $this->adminPermission ? \Yii::$app->user->can($this->adminPermission) : false; - } else { - return isset($user->username) ? in_array($user->username, $this->admins) : false; - } - } -} + + * + * For the full copyright and license information, please view the LICENSE.md + * file that was distributed with this source code. + */ + +namespace dektrium\rbac; + +use yii\base\Module as BaseModule; +use yii\filters\AccessControl; + +/** + * @author Dmitry Erofeev + */ +class RbacWebModule extends BaseModule +{ + /** + * @var string + */ + public $defaultRoute = 'role/index'; + + /** + * @var array + */ + public $admins = []; + + /** + * @var string The Administrator permission name. + */ + public $adminPermission; + + /** @var bool Whether to show flash messages. */ + public $enableFlashMessages = true; + + /** @inheritdoc */ + public function behaviors() + { + return [ + 'access' => [ + 'class' => AccessControl::className(), + 'rules' => [ + [ + 'allow' => true, + 'roles' => ['@'], + 'matchCallback' => [$this, 'checkAccess'], + ] + ], + ], + ]; + } + + /** + * Checks access. + * + * @return bool + */ + public function checkAccess() + { + $user = \Yii::$app->user->identity; + + if (method_exists($user, 'getIsAdmin')) { + return $user->getIsAdmin(); + } else if ($this->adminPermission) { + return $this->adminPermission ? \Yii::$app->user->can($this->adminPermission) : false; + } else { + return isset($user->username) ? in_array($user->username, $this->admins) : false; + } + } +} diff --git a/views/_alert.php b/views/_alert.php new file mode 100644 index 00000000..da671e73 --- /dev/null +++ b/views/_alert.php @@ -0,0 +1,32 @@ + + * + * For the full copyright and license information, please view the LICENSE.md + * file that was distributed with this source code. + */ + +use yii\bootstrap\Alert; + +/** + * @var dektrium\user\Module $module + */ +?> + +enableFlashMessages): ?> +
+
+ session->getAllFlashes() as $type => $message): ?> + + ['class' => 'alert-dismissible alert-' . $type], + 'body' => $message + ]) ?> + + +
+
+ diff --git a/views/permission/index.php b/views/permission/index.php index 583e8d05..99e57787 100644 --- a/views/permission/index.php +++ b/views/permission/index.php @@ -1,101 +1,103 @@ - - * - * For the full copyright and license information, please view the LICENSE.md - * file that was distributed with this source code. - */ - -/** - * @var $dataProvider array - * @var $this yii\web\View - * @var $filterModel dektrium\rbac\models\Search - */ - -use kartik\select2\Select2; -use yii\grid\ActionColumn; -use yii\grid\GridView; -use yii\helpers\Url; -use yii\widgets\Pjax; - -$this->title = Yii::t('rbac', 'Permissions'); -$this->params['breadcrumbs'][] = $this->title; - -?> - -beginContent('@dektrium/rbac/views/layout.php') ?> - - - - $dataProvider, - 'filterModel' => $filterModel, - 'layout' => "{items}\n{pager}", - 'columns' => [ - [ - 'attribute' => 'name', - 'header' => Yii::t('rbac', 'Name'), - 'options' => [ - 'style' => 'width: 20%' - ], - 'filter' => Select2::widget([ - 'model' => $filterModel, - 'attribute' => 'name', - 'data' => $filterModel->getNameList(), - 'options' => [ - 'placeholder' => Yii::t('rbac', 'Select permission'), - ], - 'pluginOptions' => [ - 'allowClear' => true, - ], - ]), - ], - [ - 'attribute' => 'description', - 'header' => Yii::t('rbac', 'Description'), - 'options' => [ - 'style' => 'width: 55%', - ], - 'filterInputOptions' => [ - 'class' => 'form-control', - 'id' => null, - 'placeholder' => Yii::t('rbac', 'Enter the description') - ], - ], - [ - 'attribute' => 'rule_name', - 'header' => Yii::t('rbac', 'Rule name'), - 'options' => [ - 'style' => 'width: 20%' - ], - 'filter' => Select2::widget([ - 'model' => $filterModel, - 'attribute' => 'rule_name', - 'data' => $filterModel->getRuleList(), - 'options' => [ - 'placeholder' => Yii::t('rbac', 'Select rule'), - ], - 'pluginOptions' => [ - 'allowClear' => true, - ], - ]), - ], - [ - 'class' => ActionColumn::className(), - 'template' => '{update} {delete}', - 'urlCreator' => function ($action, $model) { - return Url::to(['/rbac/permission/' . $action, 'name' => $model['name']]); - }, - 'options' => [ - 'style' => 'width: 5%' - ], - ] - ], -]) ?> - - - + + * + * For the full copyright and license information, please view the LICENSE.md + * file that was distributed with this source code. + */ + +/** + * @var $dataProvider array + * @var $this yii\web\View + * @var $filterModel dektrium\rbac\models\Search + */ + +use kartik\select2\Select2; +use yii\grid\ActionColumn; +use yii\grid\GridView; +use yii\helpers\Url; +use yii\widgets\Pjax; + +$this->title = Yii::t('rbac', 'Permissions'); +$this->params['breadcrumbs'][] = $this->title; + +?> + +render('/_alert', ['module' => $this->context->module]) ?> + +beginContent('@dektrium/rbac/views/layout.php') ?> + + + + $dataProvider, + 'filterModel' => $filterModel, + 'layout' => "{items}\n{pager}", + 'columns' => [ + [ + 'attribute' => 'name', + 'header' => Yii::t('rbac', 'Name'), + 'options' => [ + 'style' => 'width: 20%' + ], + 'filter' => Select2::widget([ + 'model' => $filterModel, + 'attribute' => 'name', + 'data' => $filterModel->getNameList(), + 'options' => [ + 'placeholder' => Yii::t('rbac', 'Select permission'), + ], + 'pluginOptions' => [ + 'allowClear' => true, + ], + ]), + ], + [ + 'attribute' => 'description', + 'header' => Yii::t('rbac', 'Description'), + 'options' => [ + 'style' => 'width: 55%', + ], + 'filterInputOptions' => [ + 'class' => 'form-control', + 'id' => null, + 'placeholder' => Yii::t('rbac', 'Enter the description') + ], + ], + [ + 'attribute' => 'rule_name', + 'header' => Yii::t('rbac', 'Rule name'), + 'options' => [ + 'style' => 'width: 20%' + ], + 'filter' => Select2::widget([ + 'model' => $filterModel, + 'attribute' => 'rule_name', + 'data' => $filterModel->getRuleList(), + 'options' => [ + 'placeholder' => Yii::t('rbac', 'Select rule'), + ], + 'pluginOptions' => [ + 'allowClear' => true, + ], + ]), + ], + [ + 'class' => ActionColumn::className(), + 'template' => '{update} {delete}', + 'urlCreator' => function ($action, $model) { + return Url::to(['/rbac/permission/' . $action, 'name' => $model['name']]); + }, + 'options' => [ + 'style' => 'width: 5%' + ], + ] + ], +]) ?> + + + endContent() ?> \ No newline at end of file diff --git a/views/role/index.php b/views/role/index.php index 868c4d57..8fb81f1a 100644 --- a/views/role/index.php +++ b/views/role/index.php @@ -1,102 +1,104 @@ - - * - * For the full copyright and license information, please view the LICENSE.md - * file that was distributed with this source code. - */ - -/** - * @var $dataProvider array - * @var $filterModel dektrium\rbac\models\Search - * @var $this yii\web\View - */ - - -use kartik\select2\Select2; -use yii\grid\ActionColumn; -use yii\grid\GridView; -use yii\helpers\Url; -use yii\widgets\Pjax; - -$this->title = Yii::t('rbac', 'Roles'); -$this->params['breadcrumbs'][] = $this->title; - -?> - -beginContent('@dektrium/rbac/views/layout.php') ?> - - - - $dataProvider, - 'filterModel' => $filterModel, - 'layout' => "{items}\n{pager}", - 'columns' => [ - [ - 'attribute' => 'name', - 'header' => Yii::t('rbac', 'Name'), - 'options' => [ - 'style' => 'width: 20%' - ], - 'filter' => Select2::widget([ - 'model' => $filterModel, - 'attribute' => 'name', - 'data' => $filterModel->getNameList(), - 'options' => [ - 'placeholder' => Yii::t('rbac', 'Select role'), - ], - 'pluginOptions' => [ - 'allowClear' => true, - ], - ]), - ], - [ - 'attribute' => 'description', - 'header' => Yii::t('rbac', 'Description'), - 'options' => [ - 'style' => 'width: 55%', - ], - 'filterInputOptions' => [ - 'class' => 'form-control', - 'id' => null, - 'placeholder' => Yii::t('rbac', 'Enter the description') - ], - ], - [ - 'attribute' => 'rule_name', - 'header' => Yii::t('rbac', 'Rule name'), - 'options' => [ - 'style' => 'width: 20%' - ], - 'filter' => Select2::widget([ - 'model' => $filterModel, - 'attribute' => 'rule_name', - 'data' => $filterModel->getRuleList(), - 'options' => [ - 'placeholder' => Yii::t('rbac', 'Select rule'), - ], - 'pluginOptions' => [ - 'allowClear' => true, - ], - ]), - ], - [ - 'class' => ActionColumn::className(), - 'template' => '{update} {delete}', - 'urlCreator' => function ($action, $model) { - return Url::to(['/rbac/role/' . $action, 'name' => $model['name']]); - }, - 'options' => [ - 'style' => 'width: 5%' - ], - ] - ], -]) ?> - - - + + * + * For the full copyright and license information, please view the LICENSE.md + * file that was distributed with this source code. + */ + +/** + * @var $dataProvider array + * @var $filterModel dektrium\rbac\models\Search + * @var $this yii\web\View + */ + + +use kartik\select2\Select2; +use yii\grid\ActionColumn; +use yii\grid\GridView; +use yii\helpers\Url; +use yii\widgets\Pjax; + +$this->title = Yii::t('rbac', 'Roles'); +$this->params['breadcrumbs'][] = $this->title; + +?> + +render('/_alert', ['module' => $this->context->module]) ?> + +beginContent('@dektrium/rbac/views/layout.php') ?> + + + + $dataProvider, + 'filterModel' => $filterModel, + 'layout' => "{items}\n{pager}", + 'columns' => [ + [ + 'attribute' => 'name', + 'header' => Yii::t('rbac', 'Name'), + 'options' => [ + 'style' => 'width: 20%' + ], + 'filter' => Select2::widget([ + 'model' => $filterModel, + 'attribute' => 'name', + 'data' => $filterModel->getNameList(), + 'options' => [ + 'placeholder' => Yii::t('rbac', 'Select role'), + ], + 'pluginOptions' => [ + 'allowClear' => true, + ], + ]), + ], + [ + 'attribute' => 'description', + 'header' => Yii::t('rbac', 'Description'), + 'options' => [ + 'style' => 'width: 55%', + ], + 'filterInputOptions' => [ + 'class' => 'form-control', + 'id' => null, + 'placeholder' => Yii::t('rbac', 'Enter the description') + ], + ], + [ + 'attribute' => 'rule_name', + 'header' => Yii::t('rbac', 'Rule name'), + 'options' => [ + 'style' => 'width: 20%' + ], + 'filter' => Select2::widget([ + 'model' => $filterModel, + 'attribute' => 'rule_name', + 'data' => $filterModel->getRuleList(), + 'options' => [ + 'placeholder' => Yii::t('rbac', 'Select rule'), + ], + 'pluginOptions' => [ + 'allowClear' => true, + ], + ]), + ], + [ + 'class' => ActionColumn::className(), + 'template' => '{update} {delete}', + 'urlCreator' => function ($action, $model) { + return Url::to(['/rbac/role/' . $action, 'name' => $model['name']]); + }, + 'options' => [ + 'style' => 'width: 5%' + ], + ] + ], +]) ?> + + + endContent() ?> \ No newline at end of file diff --git a/views/rule/index.php b/views/rule/index.php index e8b02439..1a00b810 100644 --- a/views/rule/index.php +++ b/views/rule/index.php @@ -1,105 +1,107 @@ - - * - * For the full copyright and license information, please view the LICENSE.md - * file that was distributed with this source code. - */ - -/** - * @var $this \yii\web\View - * @var $searchModel \dektrium\rbac\models\RuleSearch - * @var $dataProvider \yii\data\ArrayDataProvider - */ - -use kartik\select2\Select2; -use yii\grid\ActionColumn; -use yii\grid\GridView; -use yii\helpers\Url; -use yii\web\JsExpression; -use yii\widgets\Pjax; - -$this->title = Yii::t('rbac', 'Rules'); -$this->params['breadcrumbs'][] = $this->title; - -?> - -beginContent('@dektrium/rbac/views/layout.php') ?> - - - - $dataProvider, - 'filterModel' => $searchModel, - 'layout' => "{items}\n{pager}", - 'columns' => [ - [ - 'attribute' => 'name', - 'label' => Yii::t('rbac', 'Name'), - 'options' => [ - 'style' => 'width: 20%' - ], - 'filter' => Select2::widget([ - 'model' => $searchModel, - 'attribute' => 'name', - 'options' => [ - 'placeholder' => Yii::t('rbac', 'Select rule'), - ], - 'pluginOptions' => [ - 'ajax' => [ - 'url' => Url::to(['search']), - 'dataType' => 'json', - 'data' => new JsExpression('function(params) { return {q:params.term}; }') - ], - 'allowClear' => true, - - ], - ]), - ], - [ - 'attribute' => 'class', - 'label' => Yii::t('rbac', 'Class'), - 'value' => function ($row) { - $rule = unserialize($row['data']); - - return get_class($rule); - }, - 'options' => [ - 'style' => 'width: 20%' - ], - ], - [ - 'attribute' => 'created_at', - 'label' => Yii::t('rbac', 'Created at'), - 'format' => 'datetime', - 'options' => [ - 'style' => 'width: 20%' - ], - ], - [ - 'attribute' => 'updated_at', - 'label' => Yii::t('rbac', 'Updated at'), - 'format' => 'datetime', - 'options' => [ - 'style' => 'width: 20%' - ], - ], - [ - 'class' => ActionColumn::className(), - 'template' => '{update} {delete}', - 'urlCreator' => function ($action, $model) { - return Url::to(['/rbac/rule/' . $action, 'name' => $model['name']]); - }, - 'options' => [ - 'style' => 'width: 5%' - ], - ] - ], -]) ?> - - - -endContent() ?> + + * + * For the full copyright and license information, please view the LICENSE.md + * file that was distributed with this source code. + */ + +/** + * @var $this \yii\web\View + * @var $searchModel \dektrium\rbac\models\RuleSearch + * @var $dataProvider \yii\data\ArrayDataProvider + */ + +use kartik\select2\Select2; +use yii\grid\ActionColumn; +use yii\grid\GridView; +use yii\helpers\Url; +use yii\web\JsExpression; +use yii\widgets\Pjax; + +$this->title = Yii::t('rbac', 'Rules'); +$this->params['breadcrumbs'][] = $this->title; + +?> + +render('/_alert', ['module' => $this->context->module]) ?> + +beginContent('@dektrium/rbac/views/layout.php') ?> + + + + $dataProvider, + 'filterModel' => $searchModel, + 'layout' => "{items}\n{pager}", + 'columns' => [ + [ + 'attribute' => 'name', + 'label' => Yii::t('rbac', 'Name'), + 'options' => [ + 'style' => 'width: 20%' + ], + 'filter' => Select2::widget([ + 'model' => $searchModel, + 'attribute' => 'name', + 'options' => [ + 'placeholder' => Yii::t('rbac', 'Select rule'), + ], + 'pluginOptions' => [ + 'ajax' => [ + 'url' => Url::to(['search']), + 'dataType' => 'json', + 'data' => new JsExpression('function(params) { return {q:params.term}; }') + ], + 'allowClear' => true, + + ], + ]), + ], + [ + 'attribute' => 'class', + 'label' => Yii::t('rbac', 'Class'), + 'value' => function ($row) { + $rule = unserialize($row['data']); + + return get_class($rule); + }, + 'options' => [ + 'style' => 'width: 20%' + ], + ], + [ + 'attribute' => 'created_at', + 'label' => Yii::t('rbac', 'Created at'), + 'format' => 'datetime', + 'options' => [ + 'style' => 'width: 20%' + ], + ], + [ + 'attribute' => 'updated_at', + 'label' => Yii::t('rbac', 'Updated at'), + 'format' => 'datetime', + 'options' => [ + 'style' => 'width: 20%' + ], + ], + [ + 'class' => ActionColumn::className(), + 'template' => '{update} {delete}', + 'urlCreator' => function ($action, $model) { + return Url::to(['/rbac/rule/' . $action, 'name' => $model['name']]); + }, + 'options' => [ + 'style' => 'width: 5%' + ], + ] + ], +]) ?> + + + +endContent() ?>