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

Update item select and others: #368

Open
wants to merge 1 commit 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
3 changes: 1 addition & 2 deletions controllers/AssignmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
class AssignmentController extends Controller
{
/** @type \yii\web\IdentityInterface */
public $userClassName;
public $idField = 'id';
public $usernameField = 'username';
Expand Down Expand Up @@ -45,7 +46,6 @@ public function behaviors()
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'assign' => ['post'],
'assign' => ['post'],
'revoke' => ['post'],
],
Expand All @@ -59,7 +59,6 @@ public function behaviors()
*/
public function actionIndex()
{

if ($this->searchClass === null) {
$searchModel = new AssignmentSearch;
$dataProvider = $searchModel->search(Yii::$app->getRequest()->getQueryParams(), $this->userClassName, $this->usernameField);
Expand Down
18 changes: 12 additions & 6 deletions models/Assignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Assignment extends \mdm\admin\BaseObject
/**
* @inheritdoc
*/
public function __construct($id, $user = null, $config = array())
public function __construct($id, $user = null, $config = [])
{
$this->id = $id;
$this->user = $user;
Expand Down Expand Up @@ -87,13 +87,19 @@ public function getItems()
{
$manager = Configs::authManager();
$available = [];
foreach (array_keys($manager->getRoles()) as $name) {
$available[$name] = 'role';
foreach ($manager->getRoles() as $item) {
$avaliable[$item->name] = [
'type' => 'role',
'desc' => $item->description,
];
}

foreach (array_keys($manager->getPermissions()) as $name) {
if ($name[0] != '/') {
$available[$name] = 'permission';
foreach ($manager->getPermissions() as $item) {
if ($item->name[0] != '/') {
$avaliable[$item->name] = [
'type' => 'permission',
'desc' => $item->description,
];
}
}

Expand Down
23 changes: 17 additions & 6 deletions models/AuthItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class AuthItem extends Model

/**
* Initialize object
* @param Item $item
* @param Item $item
* @param array $config
*/
public function __construct($item = null, $config = [])
Expand Down Expand Up @@ -248,17 +248,28 @@ public function getItems()
$manager = Configs::authManager();
$available = [];
if ($this->type == Item::TYPE_ROLE) {
foreach (array_keys($manager->getRoles()) as $name) {
$available[$name] = 'role';
foreach ($manager->getRoles() as $item) {
$avaliable[$item->name] = [
'type' => 'role',
'desc' => $item->description,
];
}
}
foreach (array_keys($manager->getPermissions()) as $name) {
$available[$name] = $name[0] == '/' ? 'route' : 'permission';
foreach ($manager->getPermissions() as $item) {
$avaliable[$item->name] = [
'type' => $item->name[0] == '/' ? 'route' : 'permission',
];
if ($item->name[0] != '/') {
$avaliable[$item->name]['desc'] = $item->description;
}
}

$assigned = [];
foreach ($manager->getChildren($this->_item->name) as $item) {
$assigned[$item->name] = $item->type == 1 ? 'role' : ($item->name[0] == '/' ? 'route' : 'permission');
$assigned[$item->name] = [
'type' => $item->type == 1 ? 'role' : ($item->name[0] == '/' ? 'route' : 'permission'),
'desc' => $item->description,
];
unset($available[$item->name]);
}
unset($available[$this->name]);
Expand Down
12 changes: 8 additions & 4 deletions views/assignment/_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ function search(target) {
var q = $('.search[data-target="' + target + '"]').val();

var groups = {
role: [$('<optgroup label="Roles">'), false],
permission: [$('<optgroup label="Permission">'), false],
role: [$('<optgroup label="' + _labels['Roles'] + '">'), false],
permission: [$('<optgroup label="' + _labels['Permissions'] + '">'), false],
};
$.each(_opts.items[target], function (name, group) {
if (name.indexOf(q) >= 0) {
$('<option>').text(name).val(name).appendTo(groups[group][0]);
groups[group][1] = true;
var text = name;
if (group['desc'] !== null) {
text += ' (' + group['desc'] + ')';
}
$('<option>').text(text).val(name).appendTo(groups[group['type']][0]);
groups[group['type']][1] = true;
}
});
$.each(groups, function () {
Expand Down
31 changes: 18 additions & 13 deletions views/assignment/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,41 @@
$opts = Json::htmlEncode([
'items' => $model->getItems(),
]);
$labels = Json::htmlEncode([
'Roles' => Yii::t('rbac-admin', 'Roles'),
'Permissions' => Yii::t('rbac-admin', 'Permissions'),
]);
$this->registerJs("var _opts = {$opts};");
$this->registerJs("var _labels = {$labels};");
$this->registerJs($this->render('_script.js'));
$animateIcon = ' <i class="glyphicon glyphicon-refresh glyphicon-refresh-animate"></i>';
?>
<div class="assignment-index">
<h1><?=$this->title;?></h1>
<h1><?= $this->title; ?></h1>

<div class="row">
<div class="col-sm-5">
<input class="form-control search" data-target="available"
placeholder="<?=Yii::t('rbac-admin', 'Search for available');?>">
placeholder="<?= Yii::t('rbac-admin', 'Search for available'); ?>">
<select multiple size="20" class="form-control list" data-target="available">
</select>
</div>
<div class="col-sm-1">
<br><br>
<?=Html::a('&gt;&gt;' . $animateIcon, ['assign', 'id' => (string) $model->id], [
'class' => 'btn btn-success btn-assign',
'data-target' => 'available',
'title' => Yii::t('rbac-admin', 'Assign'),
]);?><br><br>
<?=Html::a('&lt;&lt;' . $animateIcon, ['revoke', 'id' => (string) $model->id], [
'class' => 'btn btn-danger btn-assign',
'data-target' => 'assigned',
'title' => Yii::t('rbac-admin', 'Remove'),
]);?>
<?= Html::a('&gt;&gt;' . $animateIcon, ['assign', 'id' => (string)$model->id], [
'class' => 'btn btn-success btn-assign',
'data-target' => 'available',
'title' => Yii::t('rbac-admin', 'Assign'),
]); ?><br><br>
<?= Html::a('&lt;&lt;' . $animateIcon, ['revoke', 'id' => (string)$model->id], [
'class' => 'btn btn-danger btn-assign',
'data-target' => 'assigned',
'title' => Yii::t('rbac-admin', 'Remove'),
]); ?>
</div>
<div class="col-sm-5">
<input class="form-control search" data-target="assigned"
placeholder="<?=Yii::t('rbac-admin', 'Search for assigned');?>">
placeholder="<?= Yii::t('rbac-admin', 'Search for assigned'); ?>">
<select multiple size="20" class="form-control list" data-target="assigned">
</select>
</div>
Expand Down
14 changes: 9 additions & 5 deletions views/item/_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@ function search(target) {
var q = $('.search[data-target="' + target + '"]').val();

var groups = {
role: [$('<optgroup label="Roles">'), false],
permission: [$('<optgroup label="Permission">'), false],
route: [$('<optgroup label="Routes">'), false],
role: [$('<optgroup label="' + _labels['Roles'] + '">'), false],
permission: [$('<optgroup label="' + _labels['Permissions'] + '">'), false],
route: [$('<optgroup label="' + _labels['Routes'] + '">'), false],
};
$.each(_opts.items[target], function (name, group) {
if (name.indexOf(q) >= 0) {
$('<option>').text(name).val(name).appendTo(groups[group][0]);
groups[group][1] = true;
var text = name;
if (group['desc'] !== null) {
text += ' (' + group['desc'] + ')';
}
$('<option>').text(text).val(name).appendTo(groups[group['type']][0]);
groups[group['type']][1] = true;
}
});
$.each(groups, function () {
Expand Down
68 changes: 37 additions & 31 deletions views/item/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,59 +21,65 @@
$opts = Json::htmlEncode([
'items' => $model->getItems(),
]);
$labels = Json::htmlEncode([
'Roles' => Yii::t('rbac-admin', 'Roles'),
'Permissions' => Yii::t('rbac-admin', 'Permissions'),
'Routes' => Yii::t('rbac-admin', 'Routes'),
]);
$this->registerJs("var _opts = {$opts};");
$this->registerJs("var _labels = {$labels};");
$this->registerJs($this->render('_script.js'));
$animateIcon = ' <i class="glyphicon glyphicon-refresh glyphicon-refresh-animate"></i>';
?>
<div class="auth-item-view">
<h1><?=Html::encode($this->title);?></h1>
<h1><?= Html::encode($this->title); ?></h1>
<p>
<?=Html::a(Yii::t('rbac-admin', 'Update'), ['update', 'id' => $model->name], ['class' => 'btn btn-primary']);?>
<?=Html::a(Yii::t('rbac-admin', 'Delete'), ['delete', 'id' => $model->name], [
'class' => 'btn btn-danger',
'data-confirm' => Yii::t('rbac-admin', 'Are you sure to delete this item?'),
'data-method' => 'post',
]);?>
<?=Html::a(Yii::t('rbac-admin', 'Create'), ['create'], ['class' => 'btn btn-success']);?>
<?= Html::a(Yii::t('rbac-admin', 'Update'), ['update', 'id' => $model->name], ['class' => 'btn btn-primary']); ?>
<?= Html::a(Yii::t('rbac-admin', 'Delete'), ['delete', 'id' => $model->name], [
'class' => 'btn btn-danger',
'data-confirm' => Yii::t('rbac-admin', 'Are you sure to delete this item?'),
'data-method' => 'post',
]); ?>
<?= Html::a(Yii::t('rbac-admin', 'Create'), ['create'], ['class' => 'btn btn-success']); ?>
</p>
<div class="row">
<div class="col-sm-11">
<?=
DetailView::widget([
'model' => $model,
'attributes' => [
'name',
'description:ntext',
'ruleName',
'data:ntext',
],
'template' => '<tr><th style="width:25%">{label}</th><td>{value}</td></tr>',
]);
?>
DetailView::widget([
'model' => $model,
'attributes' => [
'name',
'description:ntext',
'ruleName',
'data:ntext',
],
'template' => '<tr><th style="width:25%">{label}</th><td>{value}</td></tr>',
]);
?>
</div>
</div>
<div class="row">
<div class="col-sm-5">
<input class="form-control search" data-target="available"
placeholder="<?=Yii::t('rbac-admin', 'Search for available');?>">
placeholder="<?= Yii::t('rbac-admin', 'Search for available'); ?>">
<select multiple size="20" class="form-control list" data-target="available"></select>
</div>
<div class="col-sm-1">
<br><br>
<?=Html::a('&gt;&gt;' . $animateIcon, ['assign', 'id' => $model->name], [
'class' => 'btn btn-success btn-assign',
'data-target' => 'available',
'title' => Yii::t('rbac-admin', 'Assign'),
]);?><br><br>
<?=Html::a('&lt;&lt;' . $animateIcon, ['remove', 'id' => $model->name], [
'class' => 'btn btn-danger btn-assign',
'data-target' => 'assigned',
'title' => Yii::t('rbac-admin', 'Remove'),
]);?>
<?= Html::a('&gt;&gt;' . $animateIcon, ['assign', 'id' => $model->name], [
'class' => 'btn btn-success btn-assign',
'data-target' => 'available',
'title' => Yii::t('rbac-admin', 'Assign'),
]); ?><br><br>
<?= Html::a('&lt;&lt;' . $animateIcon, ['remove', 'id' => $model->name], [
'class' => 'btn btn-danger btn-assign',
'data-target' => 'assigned',
'title' => Yii::t('rbac-admin', 'Remove'),
]); ?>
</div>
<div class="col-sm-5">
<input class="form-control search" data-target="assigned"
placeholder="<?=Yii::t('rbac-admin', 'Search for assigned');?>">
placeholder="<?= Yii::t('rbac-admin', 'Search for assigned'); ?>">
<select multiple size="20" class="form-control list" data-target="assigned"></select>
</div>
</div>
Expand Down