Skip to content

Commit 21d2e33

Browse files
authored
Merge pull request #25 from dmstr/feature/fix-childs-with-rule
fixed rule creation for childs
2 parents 8d63cf5 + 9278bdd commit 21d2e33

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

db/RbacMigration.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ protected function generatePrivileges($privileges = [])
105105
if (isset($privilege['children']) && \is_array($privilege['children'])) {
106106
foreach ($privilege['children'] as $child_privilege) {
107107
$created_child_privilege = $this->createPrivilege($child_privilege['name'],
108-
$child_privilege['type']);
108+
$child_privilege['type'],
109+
$child_privilege['rule'] ?? [] );
109110

110111
// check if parent already has child or if parent can have this as a child
111112
if (!$this->authManager->hasChild($parent_privilege,
@@ -133,21 +134,25 @@ protected function generatePrivileges($privileges = [])
133134
*/
134135
protected function createPrivilege($name, $type, $rule_data = [])
135136
{
136-
137137
$type_name = ($type === Item::TYPE_ROLE ? 'Role' : 'Permission');
138138

139139
$getter = 'get' . $type_name;
140+
140141
// check if permission or role exists and create it
141142
if ($this->authManager->{$getter}($name) === null) {
143+
echo "Creating $type_name: $name".PHP_EOL;
142144
$privilege = $this->authManager->{'create' . $type_name}($name);
143145

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

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

153158
return $this->authManager->{$getter}($name);
@@ -172,9 +177,12 @@ public function safeDown()
172177
protected function createRule($name, $class)
173178
{
174179
if ($this->authManager->getRule($name) === null) {
175-
$this->authManager->add(new $class([
180+
$result = $this->authManager->add(new $class([
176181
'name' => $name,
177182
]));
183+
if (!$result) {
184+
throw new \Exception('Can not create rule');
185+
}
178186
}
179187
return $this->authManager->getRule($name);
180188
}

0 commit comments

Comments
 (0)