Skip to content
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
5 changes: 3 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
name: run-tests

on:
push:
pull_request:
types: [ready_for_review, synchronize]
paths:
- '**.php'
- '.github/workflows/run-tests.yml'
Expand All @@ -17,7 +18,7 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest]
php: [8.4, 8.3, 8.2]
php: [8.5, 8.4, 8.3, 8.2]
laravel: ["^12.0", "^11.0"]
stability: [prefer-lowest, prefer-stable]
include:
Expand Down
19 changes: 9 additions & 10 deletions src/Actions/SyncDefinedRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,27 @@
namespace BinaryCats\LaravelRbac\Actions;

use BackedEnum;
use Illuminate\Support\Facades\Artisan;
use Lorisleiva\Actions\Action;
use Spatie\Permission\Contracts\Role;
use Spatie\Permission\Commands\CreateRole;

class SyncDefinedRole extends Action
{
public function __construct(
protected readonly Role $role
) {
}

/**
* Handle syncing a defined role.
*/
public function handle(string $name, string $guard, array $permissions): void
{
$permissions = collect($permissions)
->map(fn ($permission) => match (true) {
->map(fn ($permission): string => match (true) {
$permission instanceof BackedEnum => $permission->value,
default => (string) $permission
});
})->implode('|');

$this->role::findOrCreate($name, $guard)
->syncPermissions($permissions);
Artisan::call(CreateRole::class, [
'name' => $name,
'guard' => $guard,
'permissions' => $permissions,
]);
}
}
7 changes: 6 additions & 1 deletion src/Commands/AbilityMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ protected function getStub()
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\Abilities';
$path = config('rbac.path', 'Abilities');

return str($path)
->after(app()->path())
->trim('/')
->prepend($rootNamespace, '\\');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/DefinedRoleMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class DefinedRoleMakeCommand extends GeneratorCommand
*/
protected function getStub()
{
return file_exists($customPath = base_path('/stubs/defined-role.stub'))
return file_exists($customPath = $this->laravel->basePath('/stubs/defined-role.stub'))
? $customPath
: __DIR__.'/../../stubs/defined-role.stub';
}
Expand Down
7 changes: 2 additions & 5 deletions src/Jobs/ResetPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,15 @@ class ResetPermissions
use Queueable;
use SerializesModels;

protected string $guard;
protected readonly string $guard;

/**
* @param string|null $guard
*/
public function __construct(?string $guard = null)
{
$this->guard = $guard ?? config('auth.defaults.guard');
}

/**
* @return void
* Handle resetting permissions.
*/
public function handle(): void
{
Expand Down
25 changes: 20 additions & 5 deletions tests/Actions/SyncDefinedRoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use BinaryCats\LaravelRbac\Tests\Fixtures\Abilities\FooAbility;
use BinaryCats\LaravelRbac\Tests\TestCase;
use PHPUnit\Framework\Attributes\Test;
use Spatie\Permission\Exceptions\PermissionDoesNotExist;

class SyncDefinedRoleTest extends TestCase
{
Expand All @@ -29,13 +28,29 @@ public function it_will_defer_syncing_defined_role_to_artisan(): void
}

#[Test]
public function it_will_throw_an_exception_on_missing_permission(): void
public function it_will_defer_syncing_defined_role_to_artisan_with_custom_guard(): void
{
$this->expectException(PermissionDoesNotExist::class);
$this->expectExceptionMessage('There is no permission named `bar` for guard `web`');
StorePermission::run('bar', 'admin');
StorePermission::run(FooAbility::One, 'admin');

SyncDefinedRole::run('foo role', 'web', [
SyncDefinedRole::run('foo role', 'admin', [
'bar',
FooAbility::One,
'this-permission-is-new-and-will-be-created',
]);

$this->assertDatabaseHas(config('permission.table_names.roles'), [
'name' => 'foo role',
'guard_name' => 'admin',
]);

$role = app(config('permission.models.role'))->where([
'name' => 'foo role',
'guard_name' => 'admin',
])->firstOrFail();

$this->assertTrue($role->hasPermissionTo('bar', 'admin'));
$this->assertTrue($role->hasPermissionTo(FooAbility::One, 'admin'));
$this->assertTrue($role->hasPermissionTo('this-permission-is-new-and-will-be-created', 'admin'));
}
}
Loading