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: multilevel permissions in can() method #1229

Open
wants to merge 4 commits into
base: develop
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
12 changes: 9 additions & 3 deletions src/Authorization/Traits/Authorizable.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public function can(string ...$permissions): bool
if (strpos($permission, '.') === false) {
throw new LogicException(
'A permission must be a string consisting of a scope and action, like `users.create`.'
. ' Invalid permission: ' . $permission
. ' Invalid permission: ' . $permission
);
}

Expand All @@ -280,8 +280,14 @@ public function can(string ...$permissions): bool
}

// Check wildcard match
$check = substr($permission, 0, strpos($permission, '.')) . '.*';
if (isset($matrix[$group]) && in_array($check, $matrix[$group], true)) {
$checks = [];
$parts = explode('.', $permission);

for ($i = count($parts); $i > 0; $i--) {
$check = implode('.', array_slice($parts, 0, $i)) . '.*';
$checks[] = $check;
}
if (isset($matrix[$group]) && array_intersect($checks, $matrix[$group]) !== []) {
return true;
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/Entities/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,17 @@ public function can(string $permission): bool
}

// Check wildcard match
$check = substr($permission, 0, strpos($permission, '.')) . '.*';
$checks = [];
$parts = explode('.', $permission);

return $this->permissions !== null && $this->permissions !== [] && in_array($check, $this->permissions, true);
for ($i = count($parts); $i > 0; $i--) {
$check = implode('.', array_slice($parts, 0, $i)) . '.*';
$checks[] = $check;
}

return $this->permissions !== null
&& $this->permissions !== []
&& array_intersect($checks, $this->permissions) !== [];
}

/**
Expand Down
24 changes: 24 additions & 0 deletions tests/Authorization/GroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,28 @@ public function testCan(): void
$this->assertTrue($group2->can('users.edit'));
$this->assertFalse($group2->can('foo.bar'));
}

public function testCanNestedPerms(): void
{
$group = $this->groups->info('user');

$group->addPermission('foo.bar.*');
$group->addPermission('foo.biz.buz.*');

$this->assertTrue($group->can('foo.bar'));
$this->assertTrue($group->can('foo.bar.*'));
$this->assertTrue($group->can('foo.bar.baz'));
$this->assertTrue($group->can('foo.bar.buz'));
$this->assertTrue($group->can('foo.bar.buz.biz'));
$this->assertTrue($group->can('foo.biz.buz'));
$this->assertTrue($group->can('foo.biz.buz.*'));
$this->assertTrue($group->can('foo.biz.buz.bar'));
$this->assertFalse($group->can('foo'));
$this->assertFalse($group->can('foo.*'));
$this->assertFalse($group->can('foo.biz'));
$this->assertFalse($group->can('foo.buz'));
$this->assertFalse($group->can('foo.biz.*'));
$this->assertFalse($group->can('foo.biz.bar'));
$this->assertFalse($group->can('foo.biz.bar.buz'));
}
}
Loading