Skip to content

Commit

Permalink
feat: cover permission instance verification based on its own guard (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreBellas authored Apr 19, 2024
1 parent 96c2761 commit 7a39f04
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Traits/HasPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ protected function hasWildcardPermission($permission, $guardName = null): bool
}

if ($permission instanceof Permission) {
$guardName = $permission->guard_name ?? $guardName;
$permission = $permission->name;
}

Expand Down
14 changes: 14 additions & 0 deletions tests/HasPermissionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ public function it_can_assign_a_permission_to_a_user()
$this->assertTrue($this->testUser->hasPermissionTo($this->testUserPermission));
}


/** @test */
public function it_can_assign_a_permission_to_a_user_with_a_non_default_guard()
{
$testUserPermission = app(Permission::class)->create([
'name' => 'edit-articles',
'guard_name' => 'api',
]);

$this->testUser->givePermissionTo($testUserPermission);

$this->assertTrue($this->testUser->hasPermissionTo($testUserPermission));
}

/** @test */
public function it_throws_an_exception_when_assigning_a_permission_that_does_not_exist()
{
Expand Down
41 changes: 41 additions & 0 deletions tests/WildcardHasPermissionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,47 @@ public function it_can_check_wildcard_permission()
$this->assertFalse($user1->hasPermissionTo('projects.view'));
}

/** @test */
public function it_can_check_wildcard_permission_for_a_non_default_guard()
{
app('config')->set('permission.enable_wildcard_permission', true);

$user1 = User::create(['email' => '[email protected]']);

$permission1 = Permission::create(['name' => 'articles.edit,view,create', 'guard_name' => 'api']);
$permission2 = Permission::create(['name' => 'news.*', 'guard_name' => 'api']);
$permission3 = Permission::create(['name' => 'posts.*', 'guard_name' => 'api']);

$user1->givePermissionTo([$permission1, $permission2, $permission3]);

$this->assertTrue($user1->hasPermissionTo('posts.create', 'api'));
$this->assertTrue($user1->hasPermissionTo('posts.create.123', 'api'));
$this->assertTrue($user1->hasPermissionTo('posts.*', 'api'));
$this->assertTrue($user1->hasPermissionTo('articles.view', 'api'));
$this->assertFalse($user1->hasPermissionTo('projects.view', 'api'));
}

/** @test */
public function it_can_check_wildcard_permission_from_instance_without_explicit_guard_argument()
{
app('config')->set('permission.enable_wildcard_permission', true);

$user1 = User::create(['email' => '[email protected]']);

$permission2 = Permission::create(['name' => 'articles.view']);
$permission1 = Permission::create(['name' => 'articles.edit', 'guard_name' => 'api']);
$permission3 = Permission::create(['name' => 'news.*', 'guard_name' => 'api']);
$permission4 = Permission::create(['name' => 'posts.*', 'guard_name' => 'api']);

$user1->givePermissionTo([$permission1, $permission2, $permission3]);

$this->assertTrue($user1->hasPermissionTo($permission1));
$this->assertTrue($user1->hasPermissionTo($permission2));
$this->assertTrue($user1->hasPermissionTo($permission3));
$this->assertFalse($user1->hasPermissionTo($permission4));
$this->assertFalse($user1->hasPermissionTo('articles.edit'));
}

/**
* @test
*
Expand Down

0 comments on commit 7a39f04

Please sign in to comment.