diff --git a/components/Configs.php b/components/Configs.php index c4aef523..3687b4b6 100644 --- a/components/Configs.php +++ b/components/Configs.php @@ -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. */ @@ -256,4 +261,12 @@ public static function strict() { return static::instance()->strict; } + + /** + * @return int + */ + public static function userRolePageSize() + { + return static::instance()->userRolePageSize; + } } diff --git a/components/ItemController.php b/components/ItemController.php index d5130264..10398e6d 100644 --- a/components/ItemController.php +++ b/components/ItemController.php @@ -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 diff --git a/models/Assignment.php b/models/Assignment.php index c677bd0c..4d72f742 100644 --- a/models/Assignment.php +++ b/models/Assignment.php @@ -103,6 +103,8 @@ public function getItems() unset($available[$item->roleName]); } + ksort($available); + ksort($assigned); return [ 'available' => $available, 'assigned' => $assigned, diff --git a/models/AuthItem.php b/models/AuthItem.php index 2c22929d..53a53ea1 100644 --- a/models/AuthItem.php +++ b/models/AuthItem.php @@ -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". @@ -30,6 +34,7 @@ class AuthItem extends Model public $description; public $ruleName; public $data; + /** * @var Item */ @@ -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], @@ -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 { @@ -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 diff --git a/views/item/_script.js b/views/item/_script.js index 34a9f83c..3385943d 100644 --- a/views/item/_script.js +++ b/views/item/_script.js @@ -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); @@ -49,6 +66,22 @@ function search(target) { }); } +function listUsers() { + var $list = $('#list-users'); + var users = _opts.users.users.map(function (user) { + return `${user.username}`; + }); + users.push('
'); + if (_opts.users.prev) { + users.push(`«`); + } + if (_opts.users.next) { + users.push(`»`); + } + $list.html(users.join(' ')); +} + // initial search('available'); search('assigned'); +listUsers(); diff --git a/views/item/view.php b/views/item/view.php index a8b7b565..f063456f 100644 --- a/views/item/view.php +++ b/views/item/view.php @@ -1,14 +1,18 @@ context; $labels = $context->labels(); @@ -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 = ' '; ?>
-

title);?>

+

title); ?>

- $model->name], ['class' => 'btn btn-primary']);?> - $model->name], [ - 'class' => 'btn btn-danger', - 'data-confirm' => Yii::t('rbac-admin', 'Are you sure to delete this item?'), - 'data-method' => 'post', -]);?> - 'btn btn-success']);?> + $model->name], ['class' => 'btn btn-primary']); ?> + $model->name], [ + 'class' => 'btn btn-danger', + 'data-confirm' => Yii::t('rbac-admin', 'Are you sure to delete this item?'), + 'data-method' => 'post', + ]); + ?> + 'btn btn-success']); ?>

$model, - 'attributes' => [ - 'name', - 'description:ntext', - 'ruleName', - 'data:ntext', - ], - 'template' => '{label}{value}', -]); -?> + DetailView::widget([ + 'model' => $model, + 'attributes' => [ + 'name', + 'description:ntext', + 'ruleName', + 'data:ntext', + ], + 'template' => '{label}{value}', + ]); + ?> +
+
+
+
+ + + + + + + + + +
+ placeholder="">


- $model->name], [ - 'class' => 'btn btn-success btn-assign', - 'data-target' => 'available', - 'title' => Yii::t('rbac-admin', 'Assign'), -]);?>

- $model->name], [ - 'class' => 'btn btn-danger btn-assign', - 'data-target' => 'assigned', - 'title' => Yii::t('rbac-admin', 'Remove'), -]);?> + $model->name], [ + 'class' => 'btn btn-success btn-assign', + 'data-target' => 'available', + 'title' => Yii::t('rbac-admin', 'Assign'), + ]); + ?>

+ $model->name], [ + 'class' => 'btn btn-danger btn-assign', + 'data-target' => 'assigned', + 'title' => Yii::t('rbac-admin', 'Remove'), + ]); + ?>
+ placeholder="">