Skip to content

Commit

Permalink
add info assigned user to role.
Browse files Browse the repository at this point in the history
  • Loading branch information
mdmunir committed Dec 31, 2020
1 parent 5b0966e commit 10e526f
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 40 deletions.
13 changes: 13 additions & 0 deletions components/Configs.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ class Configs extends \mdm\admin\BaseObject
*/
public $defaultUserStatus = 10;

/**
* @var integer Number of user role.
*/
public $userRolePageSize = 100;

/**
* @var boolean If true then AccessControl only check if route are registered.
*/
Expand Down Expand Up @@ -256,4 +261,12 @@ public static function strict()
{
return static::instance()->strict;
}

/**
* @return int
*/
public static function userRolePageSize()
{
return static::instance()->userRolePageSize;
}
}
14 changes: 14 additions & 0 deletions components/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,20 @@ public function actionAssign($id)
return array_merge($model->getItems(), ['success' => $success]);
}

/**
* Assign items
* @param string $id
* @return array
*/
public function actionGetUsers($id)
{
$page = Yii::$app->getRequest()->get('page', 0);
$model = $this->findModel($id);
Yii::$app->getResponse()->format = 'json';

return array_merge($model->getUsers($page));
}

/**
* Assign or remove items
* @param string $id
Expand Down
2 changes: 2 additions & 0 deletions models/Assignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public function getItems()
unset($available[$item->roleName]);
}

ksort($available);
ksort($assigned);
return [
'available' => $available,
'assigned' => $assigned,
Expand Down
65 changes: 61 additions & 4 deletions models/AuthItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

use mdm\admin\components\Configs;
use mdm\admin\components\Helper;
use mdm\admin\controllers\AssignmentController;
use mdm\admin\Module;
use Yii;
use yii\base\Model;
use yii\helpers\Json;
use yii\helpers\Url;
use yii\rbac\Item;
use yii\rbac\Rule;

/**
* This is the model class for table "tbl_auth_item".
Expand All @@ -30,6 +34,7 @@ class AuthItem extends Model
public $description;
public $ruleName;
public $data;

/**
* @var Item
*/
Expand Down Expand Up @@ -62,8 +67,8 @@ public function rules()
[['ruleName'], 'checkRule'],
[['name', 'type'], 'required'],
[['name'], 'checkUnique', 'when' => function () {
return $this->isNewRecord || ($this->_item->name != $this->name);
}],
return $this->isNewRecord || ($this->_item->name != $this->name);
}],
[['type'], 'integer'],
[['description', 'data', 'ruleName'], 'default'],
[['name'], 'string', 'max' => 64],
Expand Down Expand Up @@ -96,7 +101,7 @@ public function checkRule()
if (!Configs::authManager()->getRule($name)) {
try {
$rule = Yii::createObject($name);
if ($rule instanceof \yii\rbac\Rule) {
if ($rule instanceof Rule) {
$rule->name = $name;
Configs::authManager()->add($rule);
} else {
Expand Down Expand Up @@ -259,16 +264,68 @@ public function getItems()

$assigned = [];
foreach ($manager->getChildren($this->_item->name) as $item) {
$assigned[$item->name] = $item->type == 1 ? 'role' : ($item->name[0] == '/' || $advanced && $item->name[0] == '@' ? 'route' : 'permission');
$assigned[$item->name] = $item->type == 1 ? 'role' : ($item->name[0] == '/' || $advanced && $item->name[0] == '@'
? 'route' : 'permission');
unset($available[$item->name]);
}
unset($available[$this->name]);
ksort($available);
ksort($assigned);
return [
'available' => $available,
'assigned' => $assigned,
];
}

public function getUsers()
{
$module = Yii::$app->controller->module;
if (!$module || !$module instanceof Module) {
return [];
}
$ctrl = $module->createController('assignment');
$result = [];
if ($ctrl && $ctrl[0] instanceof AssignmentController) {
$ctrl = $ctrl[0];
$class = $ctrl->userClassName;
$idField = $ctrl->idField;
$usernameField = $ctrl->usernameField;

$manager = Configs::authManager();
$ids = $manager->getUserIdsByRole($this->name);

$provider = new \yii\data\ArrayDataProvider([
'allModels' => $ids,
'pagination' => [
'pageSize' => Configs::userRolePageSize(),
]
]);
$users = $class::find()
->select(['id' => $idField, 'username' => $usernameField])
->where([$idField => $provider->getModels()])
->asArray()->all();

$route = '/' . $ctrl->uniqueId . '/view';
foreach ($users as &$row) {
$row['link'] = Url::to([$route, 'id' => $row['id']]);
}
$result['users'] = $users;
$currentPage = $provider->pagination->getPage();
$pageCount = $provider->pagination->getPageCount();
if ($pageCount > 0) {
$result['first'] = 0;
$result['last'] = $pageCount - 1;
if ($currentPage > 0) {
$result['prev'] = $currentPage - 1;
}
if ($currentPage < $pageCount - 1) {
$result['next'] = $currentPage + 1;
}
}
}
return $result;
}

/**
* Get item
* @return Item
Expand Down
33 changes: 33 additions & 0 deletions views/item/_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ function updateItems(r) {
search('available');
search('assigned');
}
function updateUsers(r) {
_opts.users = r;
listUsers();
}

$('#list-users').on('click', 'a[data-target]', function () {
var $this = $(this);
var target = $this.data('target');
var page = _opts.users[target];
if (page !== undefined) {
$.get(_opts.getUserUrl, {page: page}, function (r) {
updateUsers(r);
});
}

return false;
});

$('.btn-assign').click(function () {
var $this = $(this);
Expand Down Expand Up @@ -49,6 +66,22 @@ function search(target) {
});
}

function listUsers() {
var $list = $('#list-users');
var users = _opts.users.users.map(function (user) {
return `<span class="label label-info"><a href="${user.link}">${user.username}</a></span>`;
});
users.push('<br>');
if (_opts.users.prev) {
users.push(`<span class="label label-primary"><a href="#" data-target="${_opts.users.prev}">&laquo;</a></span>`);
}
if (_opts.users.next) {
users.push(`<span class="label label-primary"><a href="#" data-target="${_opts.users.next}">&raquo;</a></span>`);
}
$list.html(users.join(' '));
}

// initial
search('available');
search('assigned');
listUsers();
98 changes: 62 additions & 36 deletions views/item/view.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<?php

use mdm\admin\AnimateAsset;
use mdm\admin\components\ItemController;
use mdm\admin\models\AuthItem;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\helpers\Url;
use yii\web\View;
use yii\web\YiiAsset;
use yii\widgets\DetailView;

/* @var $this yii\web\View */
/* @var $model mdm\admin\models\AuthItem */
/* @var $context mdm\admin\components\ItemController */
/* @var $this View */
/* @var $model AuthItem */
/* @var $context ItemController */

$context = $this->context;
$labels = $context->labels();
Expand All @@ -19,61 +23,83 @@
AnimateAsset::register($this);
YiiAsset::register($this);
$opts = Json::htmlEncode([
'items' => $model->getItems(),
]);
'items' => $model->getItems(),
'users' => $model->getUsers(),
'getUserUrl' => Url::to(['get-users', 'id' => $model->name])
]);
$this->registerJs("var _opts = {$opts};");
$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-11">
<table class="table table-striped table-bordered">
<tbody>
<tr>
<th><?= Yii::t('rbac-admin', 'Assigned users'); ?></th>
</tr>
<tr>
<td id="list-users"></td>
</tr>
</tbody>
</table>
</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

0 comments on commit 10e526f

Please sign in to comment.