Skip to content

Commit

Permalink
页面展示内容优化
Browse files Browse the repository at this point in the history
1. 调整页面显示,只能看到自己有权限的页面
2. 分配: 给对方自己有的权限及子权限,且对方没有这个权限或者个权限的父权
限
3. 分配、角色、权限、路由展示:描述信息(权限名字) 或者 权限名字  没
有描述的时候
  • Loading branch information
noname007 committed May 28, 2019
1 parent d7fc2f4 commit d47c0c8
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 45 deletions.
29 changes: 14 additions & 15 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace mdm\admin;

use mdm\admin\components\Configs;
use Yii;
use yii\helpers\Inflector;

Expand Down Expand Up @@ -61,13 +62,13 @@ class Module extends \yii\base\Module
* @see [[menus]]
*/
private $_coreItems = [
'user' => 'Users',
'assignment' => 'Assignments',
'role' => 'Roles',
'permission' => 'Permissions',
'route' => 'Routes',
'rule' => 'Rules',
'menu' => 'Menus',
'user/index' => 'Users',
'assignment/index' => 'Assignments',
'role/index' => 'Roles',
'permission/index' => 'Permissions',
'route/index' => 'Routes',
'rule/index' => 'Rules',
'menu/index' => 'Menus',
];
/**
* @var array
Expand Down Expand Up @@ -122,20 +123,18 @@ public function getMenus()
// resolve core menus
$this->_normalizeMenus = [];

$config = components\Configs::instance();
$config = Configs::instance();
$conditions = [
'user' => $config->db && $config->db->schema->getTableSchema($config->userTable),
'assignment' => ($userClass = Yii::$app->getUser()->identityClass) && is_subclass_of($userClass, 'yii\db\BaseActiveRecord'),
'menu' => $config->db && $config->db->schema->getTableSchema($config->menuTable),
'user/index' => $config->db && $config->db->schema->getTableSchema($config->userTable),
'assignment/index' => ($userClass = Yii::$app->getUser()->identityClass) && is_subclass_of($userClass, 'yii\db\BaseActiveRecord'),
'menu/index' => $config->db && $config->db->schema->getTableSchema($config->menuTable),
];

foreach ($this->_coreItems as $id => $lable) {
if (!isset($conditions[$id]) || $conditions[$id]) {
$this->_normalizeMenus[$id] = ['label' => Yii::t('rbac-admin', $lable), 'url' => [$mid . $id]];
}
}
foreach (array_keys($this->controllerMap) as $id) {
$this->_normalizeMenus[$id] = ['label' => Yii::t('rbac-admin', Inflector::humanize($id)), 'url' => [$mid . $id]];
}

// user configure menus
foreach ($this->_menus as $id => $value) {
Expand All @@ -147,7 +146,7 @@ public function getMenus()
$value = ['label' => $value];
}
$this->_normalizeMenus[$id] = isset($this->_normalizeMenus[$id]) ? array_merge($this->_normalizeMenus[$id], $value)
: $value;
: $value;
if (!isset($this->_normalizeMenus[$id]['url'])) {
$this->_normalizeMenus[$id]['url'] = [$mid . $id];
}
Expand Down
69 changes: 54 additions & 15 deletions models/Assignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,22 @@ public function assign($items)
{
$manager = Configs::authManager();
$success = 0;

$current_user_id = Yii::$app->getUser()->getId();

foreach ($items as $name) {
try {
$item = $manager->getRole($name);
$item = $item ?: $manager->getPermission($name);
$manager->assign($item, $this->id);
$success++;
$verify_result = $manager->checkAccess($current_user_id, $name);

Yii::debug("verify role|permission: $name, result: "
. ($verify_result ? "Y" : "N"));

if ($verify_result) {
$item = $manager->getRole($name);
$item = $item ?: $manager->getPermission($name);
$manager->assign($item, $this->id);
$success++;
}
} catch (\Exception $exc) {
Yii::error($exc->getMessage(), __METHOD__);
}
Expand All @@ -63,14 +73,22 @@ public function assign($items)
*/
public function revoke($items)
{
$current_user_id = Yii::$app->getUser()->getId();
$manager = Configs::authManager();
$success = 0;
foreach ($items as $name) {
try {
$item = $manager->getRole($name);
$item = $item ?: $manager->getPermission($name);
$manager->revoke($item, $this->id);
$success++;
$verify_result = $manager->checkAccess($current_user_id, $name);

Yii::debug("verify role|permission: $name, result: "
. ($verify_result ? "Y" : "N"));

if ($verify_result) {
$item = $manager->getRole($name);
$item = $item ?: $manager->getPermission($name);
$manager->revoke($item, $this->id);
$success++;
}
} catch (\Exception $exc) {
Yii::error($exc->getMessage(), __METHOD__);
}
Expand All @@ -85,27 +103,48 @@ public function revoke($items)
*/
public function getItems()
{
$current_user_id = Yii::$app->getUser()->getId();
$manager = Configs::authManager();
$available = [];
foreach (array_keys($manager->getRoles()) as $name) {
$available[$name] = 'role';

$roles = $manager->getRolesByUser($current_user_id);

foreach ($roles as $role) {
$name = $role->name;
$available[$name][0] = 'role';
$available[$name][1] = $role->description;

$child_roles = $manager->getChildRoles($name);
foreach ($child_roles as $childRole)
{
$name = $childRole->name;
$available[$name][0] = 'role';
$available[$name][1] = $childRole->description;
}
}

foreach (array_keys($manager->getPermissions()) as $name) {

$permissions = $manager->getPermissionsByUser($current_user_id);

foreach ($permissions as $permission) {
$name = $permission->name;
if ($name[0] != '/') {
$available[$name] = 'permission';
$available[$name][0] = 'permission';
$available[$name][1] = $permission->description;
}
}

$assigned = [];
foreach ($manager->getAssignments($this->id) as $item) {
$assigned[$item->roleName] = $available[$item->roleName];
unset($available[$item->roleName]);
if(isset($available[$item->roleName])) {
$assigned[$item->roleName] = $available[$item->roleName];
unset($available[$item->roleName]);
}
}

return [
'available' => $available,
'assigned' => $assigned,
'assigned' => $assigned,
];
}

Expand Down
16 changes: 11 additions & 5 deletions models/AuthItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,17 +248,23 @@ 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) {
$name = $item->name;
$available[$name][0] = 'role';
$available[$name][1] = $item->description;
}
}
foreach (array_keys($manager->getPermissions()) as $name) {
$available[$name] = $name[0] == '/' ? 'route' : 'permission';
foreach ($manager->getPermissions() as $item) {
$name = $item->name;
$available[$name][0] = $name[0] == '/' ? 'route' : 'permission';
$available[$name][1] = $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][0] = $item->type == 1 ? 'role' : ($item->name[0] == '/' ? 'route' : 'permission');
$assigned[$item->name][1] = $item->description;
unset($available[$item->name]);
}
unset($available[$this->name]);
Expand Down
12 changes: 10 additions & 2 deletions views/assignment/_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,16 @@ function search(target) {
};
$.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;
if(group[1] == null)
{
text = name;
}else {
text = group[1] + " ( " + name + ")";
}
$('<option>').text( text ).val(name).appendTo(groups[group[0]][0]);
groups[group[0]][1] = true;
}
});
$.each(groups, function () {
Expand Down
13 changes: 11 additions & 2 deletions views/item/_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ $('.search[data-target]').keyup(function () {

function search(target) {
var $list = $('select.list[data-target="' + target + '"]');

$list.html('');
var q = $('.search[data-target="' + target + '"]').val();

Expand All @@ -38,8 +39,16 @@ function search(target) {
};
$.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;
if(group[1] == null)
{
text = name;
}else {
text = group[1] + " ( " + name + ")";
}
$('<option>').text( text ).val(name).appendTo(groups[group[0]][0]);
groups[group[0]][1] = true;
}
});
$.each(groups, function () {
Expand Down
18 changes: 12 additions & 6 deletions views/layouts/left-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

/* @var $this \yii\web\View */
/* @var $content string */
/* @var $controller \yii\web\Controller string */


$controller = $this->context;
$menus = $controller->module->menus;
$route = $controller->route;
$user = Yii::$app->getUser();
foreach ($menus as $i => $menu) {
$menus[$i]['active'] = strpos($route, trim($menu['url'][0], '/')) === 0;
}
Expand All @@ -19,12 +22,15 @@
<div id="manager-menu" class="list-group">
<?php
foreach ($menus as $menu) {
$label = Html::tag('i', '', ['class' => 'glyphicon glyphicon-chevron-right pull-right']) .
Html::tag('span', Html::encode($menu['label']), []);
$active = $menu['active'] ? ' active' : '';
echo Html::a($label, $menu['url'], [
'class' => 'list-group-item' . $active,
]);
$r = \mdm\admin\components\Helper::checkRoute($menu['url'][0]);
if($r) {
$label = Html::tag('i', '', ['class' => 'glyphicon glyphicon-chevron-right pull-right']) .
Html::tag('span', Html::encode($menu['label']), []);
$active = $menu['active'] ? ' active' : '';
echo Html::a($label, $menu['url'], [
'class' => 'list-group-item' . $active,
]);
}
}
?>
</div>
Expand Down

0 comments on commit d47c0c8

Please sign in to comment.