Skip to content

Commit

Permalink
Merge pull request #25 from dmstr/feature/fix-childs-with-rule
Browse files Browse the repository at this point in the history
fixed rule creation for childs
  • Loading branch information
schmunk42 authored Mar 3, 2020
2 parents 8d63cf5 + 9278bdd commit 21d2e33
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions db/RbacMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ protected function generatePrivileges($privileges = [])
if (isset($privilege['children']) && \is_array($privilege['children'])) {
foreach ($privilege['children'] as $child_privilege) {
$created_child_privilege = $this->createPrivilege($child_privilege['name'],
$child_privilege['type']);
$child_privilege['type'],
$child_privilege['rule'] ?? [] );

// check if parent already has child or if parent can have this as a child
if (!$this->authManager->hasChild($parent_privilege,
Expand Down Expand Up @@ -133,21 +134,25 @@ protected function generatePrivileges($privileges = [])
*/
protected function createPrivilege($name, $type, $rule_data = [])
{

$type_name = ($type === Item::TYPE_ROLE ? 'Role' : 'Permission');

$getter = 'get' . $type_name;

// check if permission or role exists and create it
if ($this->authManager->{$getter}($name) === null) {
echo "Creating $type_name: $name".PHP_EOL;
$privilege = $this->authManager->{'create' . $type_name}($name);

if (!empty($rule_data)) {
echo "Creating rule...".PHP_EOL;
$privilege->ruleName = $this->createRule($rule_data['name'], $rule_data['class'])->name;
}

if (!$this->authManager->add($privilege)) {
throw new ErrorException('Cannot create ' . mb_strtolower($type_name) . ' ' . $name);
}
} else {
echo "$name exists [skipping]".PHP_EOL;
}

return $this->authManager->{$getter}($name);
Expand All @@ -172,9 +177,12 @@ public function safeDown()
protected function createRule($name, $class)
{
if ($this->authManager->getRule($name) === null) {
$this->authManager->add(new $class([
$result = $this->authManager->add(new $class([
'name' => $name,
]));
if (!$result) {
throw new \Exception('Can not create rule');
}
}
return $this->authManager->getRule($name);
}
Expand Down

0 comments on commit 21d2e33

Please sign in to comment.