Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #5 from justcoded/permissions_tree_selector
Browse files Browse the repository at this point in the history
Role/Permission unique name fix
  • Loading branch information
aprokopenko authored Oct 25, 2017
2 parents f7625c7 + 61521ba commit f2539aa
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ v1.1
---------------------
* Role permission selector now have better UI with real trees.
* Bugfix: Item name validation not allow '*'.
* Bugfix: Fatal error on creating Role/Permission with existed name.

v1.0.1
---------------------
Expand Down
8 changes: 6 additions & 2 deletions src/forms/PermissionForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ public function attributeHints()
*/
public function uniqueItemName($attribute, $params, $validator)
{
$permission = Permission::getList();
return ! isset($permission[$this->$attribute]);
$name = $this->$attribute;
if ($item = Permission::find($name)) {
$this->addError($attribute, 'Permission with the same name is already exists.');
return false;
}
return true;
}

/**
Expand Down
28 changes: 17 additions & 11 deletions src/forms/RoleForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,12 @@ public function attributeHints()
*/
public function uniqueItemName($attribute, $params, $validator)
{
$permission = Role::getList();
return ! isset($permission[$this->$attribute]);
$name = $this->$attribute;
if ($item = Role::find($name)) {
$this->addError($attribute, 'Role with the same name is already exists.');
return false;
}
return true;
}

/**
Expand Down Expand Up @@ -138,16 +142,18 @@ public function save()
public function getInheritPermissions()
{
$herited = [];
foreach ($this->childRoles as $roleName) {
$permissions = Yii::$app->authManager->getPermissionsByRole($roleName);
$herited = array_merge(
$herited,
array_keys($permissions)
);
}
if (!empty($this->childRoles)) {
foreach ($this->childRoles as $roleName) {
$permissions = Yii::$app->authManager->getPermissionsByRole($roleName);
$herited = array_merge(
$herited,
array_keys($permissions)
);
}

$herited = array_unique($herited);
$herited = array_combine($herited, $herited);
$herited = array_unique($herited);
$herited = array_combine($herited, $herited);
}
return $herited;
}

Expand Down
12 changes: 7 additions & 5 deletions src/views/roles/_permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
$this->registerJs('window.inheritPermissions = ' . Json::encode(array_values($inheritTree)), View::POS_BEGIN);
?>
<div class="row js-tree-box">
<div class="col-md-3">
<h4>Inherit Permissions</h4>
<input type="text" class="simple-input" id="inheritSearch" placeholder="Search..." />
<div id="inheritPermissions"></div>
</div>
<?php if ($model::SCENARIO_CREATE !== $model->scenario) : ?>
<div class="col-md-3">
<h4>Inherit Permissions</h4>
<input type="text" class="simple-input" id="inheritSearch" placeholder="Search..." />
<div id="inheritPermissions"></div>
</div>
<?php endif; ?>
<div class="col-md-4">
<h4>Allowed Permissions</h4>
<input type="text" class="simple-input" id="allowSearch" placeholder="Search..." />
Expand Down

0 comments on commit f2539aa

Please sign in to comment.