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

Add support for multi level permissions in can() #1227

Closed
wants to merge 5 commits into from
Closed
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
10 changes: 8 additions & 2 deletions src/Authorization/Traits/Authorizable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 9 additions & 2 deletions src/Entities/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,16 @@
}

// Check wildcard match
$check = substr($permission, 0, strpos($permission, '.')) . '.*';
$checks = [];
$parts = explode('.', $permission);
for ($i = count($parts); $i > 0; $i--) {
$check = implode('.', array_slice($parts, 0, $i)) . '.*';
$checks[] = $check;
}

return $this->permissions !== null && $this->permissions !== [] && in_array($check, $this->permissions, true);
return $this->permissions !== null &&

Check failure on line 95 in src/Entities/Group.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.2 Static Analysis

Only booleans are allowed in &&, array<int<0, max>, string> given on the right side.

Check failure on line 95 in src/Entities/Group.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.0 Static Analysis

Only booleans are allowed in &&, array<int<0, max>, string> given on the right side.

Check failure on line 95 in src/Entities/Group.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.3 Static Analysis

Only booleans are allowed in &&, array<int<0, max>, string> given on the right side.

Check failure on line 95 in src/Entities/Group.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 7.4 Static Analysis

Only booleans are allowed in &&, array<int<0, max>, string> given on the right side.

Check failure on line 95 in src/Entities/Group.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.1 Static Analysis

Only booleans are allowed in &&, array<int<0, max>, string> given on the right side.
$this->permissions !== [] &&
array_intersect($checks, $this->permissions);
}

/**
Expand Down
21 changes: 21 additions & 0 deletions tests/Authorization/GroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,25 @@ 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->assertFalse($group->can('foo.bar.buz.biz'));
$this->assertTrue($group->can('foo.biz.buz'));
$this->assertTrue($group->can('foo.biz.buz.*'));
$this->assertFalse($group->can('foo.biz'));
$this->assertFalse($group->can('foo.biz.*'));
$this->assertTrue($group->can('foo.biz.buz.bar'));
$this->assertFalse($group->can('foo.biz.bar.buz'));
$this->assertFalse($group->can('foo.biz.buz.bar.boz'));
}
}
Loading