Skip to content

fix: TypeError when checking invalid permission #510

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

Merged
merged 1 commit into from
Nov 5, 2022
Merged
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: 10 additions & 0 deletions src/Authorization/Traits/Authorizable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use CodeIgniter\I18n\Time;
use CodeIgniter\Shield\Authorization\AuthorizationException;
use CodeIgniter\Shield\Exceptions\LogicException;
use CodeIgniter\Shield\Models\GroupModel;
use CodeIgniter\Shield\Models\PermissionModel;

Expand Down Expand Up @@ -226,9 +227,18 @@ public function hasPermission(string $permission): bool
/**
* Checks user permissions and their group permissions
* to see if the user has a specific permission.
*
* @param string $permission string consisting of a scope and action, like `users.create`
*/
public function can(string $permission): 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
);
}

$this->populatePermissions();

$permission = strtolower($permission);
Expand Down
11 changes: 11 additions & 0 deletions tests/Authorization/AuthorizableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use CodeIgniter\I18n\Time;
use CodeIgniter\Shield\Authorization\AuthorizationException;
use CodeIgniter\Shield\Exceptions\LogicException;
use CodeIgniter\Shield\Models\UserModel;
use CodeIgniter\Test\DatabaseTestTrait;
use Locale;
Expand Down Expand Up @@ -299,6 +300,16 @@ public function testCanCascadesToGroupsWithWildcards(): void
$this->assertTrue($this->user->can('admin.access'));
}

public function testCanGetsInvalidPermission(): void
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('Invalid permission: developer');

$this->user->addGroup('superadmin');

$this->assertTrue($this->user->can('developer'));
}

/**
* @see https://github.com/codeigniter4/shield/pull/238
*/
Expand Down