Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alert view to show flash messages. #111

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 74 additions & 71 deletions RbacWebModule.php
Original file line number Diff line number Diff line change
@@ -1,71 +1,74 @@
<?php

/*
* This file is part of the Dektrium project.
*
* (c) Dektrium project <http://github.com/dektrium>
*
* 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 <[email protected]>
*/
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;
}
}
}
<?php

/*
* This file is part of the Dektrium project.
*
* (c) Dektrium project <http://github.com/dektrium>
*
* 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 <[email protected]>
*/
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;
}
}
}
32 changes: 32 additions & 0 deletions views/_alert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the Dektrium project.
*
* (c) Dektrium project <http://github.com/dektrium>
*
* 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
*/
?>

<?php if ($module->enableFlashMessages): ?>
<div class="row">
<div class="col-xs-12">
<?php foreach (Yii::$app->session->getAllFlashes() as $type => $message): ?>
<?php if (in_array($type, ['success', 'danger', 'warning', 'info'])): ?>
<?= Alert::widget([
'options' => ['class' => 'alert-dismissible alert-' . $type],
'body' => $message
]) ?>
<?php endif ?>
<?php endforeach ?>
</div>
</div>
<?php endif ?>
202 changes: 102 additions & 100 deletions views/permission/index.php
Original file line number Diff line number Diff line change
@@ -1,101 +1,103 @@
<?php

/*
* This file is part of the Dektrium project.
*
* (c) Dektrium project <http://github.com/dektrium>
*
* 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;

?>

<?php $this->beginContent('@dektrium/rbac/views/layout.php') ?>

<?php Pjax::begin() ?>

<?= GridView::widget([
'dataProvider' => $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%'
],
]
],
]) ?>

<?php Pjax::end() ?>

<?php

/*
* This file is part of the Dektrium project.
*
* (c) Dektrium project <http://github.com/dektrium>
*
* 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;

?>

<?= $this->render('/_alert', ['module' => $this->context->module]) ?>

<?php $this->beginContent('@dektrium/rbac/views/layout.php') ?>

<?php Pjax::begin() ?>

<?= GridView::widget([
'dataProvider' => $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%'
],
]
],
]) ?>

<?php Pjax::end() ?>

<?php $this->endContent() ?>
Loading