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

fix & enhencement #400

Open
wants to merge 8 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
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
10 changes: 9 additions & 1 deletion components/AccessControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,15 @@ public function beforeAction($action)
{
$actionId = $action->getUniqueId();
$user = $this->getUser();
if (Helper::checkRoute('/' . $actionId, Yii::$app->getRequest()->get(), $user)) {

$request = Yii::$app->getRequest();
if($request->getIsPost()){
$params = $request->post();
}else{
$params = $request->get();
}

if (Helper::checkRoute('/' . $actionId, $params, $user)) {
return true;
}
$this->denyAccess($user);
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "mdmsoft/yii2-admin",
"name": "noname007/yii2-admin",
"description": "RBAC Auth manager for Yii2 ",
"keywords": ["yii", "admin", "auth", "rbac"],
"type": "yii2-extension",
Expand Down
3 changes: 2 additions & 1 deletion controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ public function actionActivate($id)
*/
protected function findModel($id)
{
if (($model = User::findOne($id)) !== null) {
$class = Yii::$app->getUser()->identityClass ? : mdm\admin\models\User::class;
if (($model = $class::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
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,29 +103,50 @@ 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]);
}
}

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

Expand Down
15 changes: 10 additions & 5 deletions models/AuthItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,18 +254,23 @@ public function getItems()
$advanced = Configs::instance()->advanced;
$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] == '/' || $advanced && $name[0] == '@' ? 'route' : 'permission';
foreach ($manager->getPermissions() as $item) {
$name = $item->name;
$available[$name][0] = $name[0] == '/' || $advanced && $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] == '/' || $advanced && $item->name[0] == '@'
$assigned[$item->name][0] = $item->type == 1 ? 'role' : ($item->name[0] == '/' || $advanced && $item->name[0] == '@'
? 'route' : 'permission');
$assigned[$item->name][1] = $item->description;
unset($available[$item->name]);
}
unset($available[$this->name]);
Expand Down
19 changes: 15 additions & 4 deletions views/assignment/_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,25 @@ function search(target) {
$list.html('');
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;
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
6 changes: 6 additions & 0 deletions views/assignment/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@
$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>';
?>
Expand Down
20 changes: 15 additions & 5 deletions views/item/_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,28 @@ $('.search[data-target]').keyup(function () {

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

$list.html('');
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;
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
8 changes: 8 additions & 0 deletions views/item/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
'getUserUrl' => Url::to(['get-users', 'id' => $model->name])
]);
$this->registerJs("var _opts = {$opts};");

$labels = Json::htmlEncode([
'Roles' => Yii::t('rbac-admin', 'Roles'),
'Permissions' => Yii::t('rbac-admin', 'Permissions'),
'Routes' => Yii::t('rbac-admin', 'Routes'),
]);
$this->registerJs("var _labels = {$labels};");

$this->registerJs($this->render('_script.js'));
$animateIcon = ' <i class="glyphicon glyphicon-refresh glyphicon-refresh-animate"></i>';
?>
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