Skip to content

Commit efe59bb

Browse files
committed
Adjust the sync logic
1 parent 5a1ec73 commit efe59bb

File tree

5 files changed

+22
-31
lines changed

5 files changed

+22
-31
lines changed

src/Actions/SyncDefinedRole.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,27 @@
33
namespace BinaryCats\LaravelRbac\Actions;
44

55
use BackedEnum;
6+
use Illuminate\Support\Facades\Artisan;
67
use Lorisleiva\Actions\Action;
7-
use Spatie\Permission\Contracts\Role;
8+
use Spatie\Permission\Commands\CreateRole;
89

910
class SyncDefinedRole extends Action
1011
{
11-
public function __construct(
12-
protected readonly Role $role
13-
) {
14-
}
15-
1612
/**
1713
* Handle syncing a defined role.
1814
*/
1915
public function handle(string $name, string $guard, array $permissions): void
2016
{
2117
$permissions = collect($permissions)
22-
->map(fn ($permission) => match (true) {
18+
->map(fn ($permission): string => match (true) {
2319
$permission instanceof BackedEnum => $permission->value,
24-
default => (string) $permission
25-
});
26-
27-
$this->role::findOrCreate($name, $guard)
28-
->syncPermissions($permissions);
20+
default => (string) $permission
21+
})->implode('|');
22+
23+
Artisan::call(CreateRole::class, [
24+
'name' => $name,
25+
'guard' => $guard,
26+
'permissions' => $permissions
27+
]);
2928
}
3029
}

src/Commands/AbilityMakeCommand.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ protected function getStub()
4040
*/
4141
protected function getDefaultNamespace($rootNamespace)
4242
{
43-
return $rootNamespace.'\Abilities';
43+
$path = config('rbac.path', 'Abilities');
44+
45+
return str($path)
46+
->after(app()->path())
47+
->trim('/')
48+
->prepend($rootNamespace, '\\');
4449
}
4550

4651
/**

src/Commands/DefinedRoleMakeCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class DefinedRoleMakeCommand extends GeneratorCommand
2525
*/
2626
protected function getStub()
2727
{
28-
return file_exists($customPath = base_path('/stubs/defined-role.stub'))
28+
return file_exists($customPath = $this->laravel->basePath('/stubs/defined-role.stub'))
2929
? $customPath
3030
: __DIR__.'/../../stubs/defined-role.stub';
3131
}

src/Jobs/ResetPermissions.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,15 @@ class ResetPermissions
1818
use Queueable;
1919
use SerializesModels;
2020

21-
protected string $guard;
21+
protected readonly string $guard;
2222

23-
/**
24-
* @param string|null $guard
25-
*/
2623
public function __construct(?string $guard = null)
2724
{
2825
$this->guard = $guard ?? config('auth.defaults.guard');
2926
}
3027

3128
/**
32-
* @return void
29+
* Handle resetting permissions
3330
*/
3431
public function handle(): void
3532
{

tests/Actions/SyncDefinedRoleTest.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use BinaryCats\LaravelRbac\Tests\Fixtures\Abilities\FooAbility;
88
use BinaryCats\LaravelRbac\Tests\TestCase;
99
use PHPUnit\Framework\Attributes\Test;
10-
use Spatie\Permission\Exceptions\PermissionDoesNotExist;
1110

1211
class SyncDefinedRoleTest extends TestCase
1312
{
@@ -37,6 +36,7 @@ public function it_will_defer_syncing_defined_role_to_artisan_with_custom_guard(
3736
SyncDefinedRole::run('foo role', 'admin', [
3837
'bar',
3938
FooAbility::One,
39+
'this-permission-is-new-and-will-be-created',
4040
]);
4141

4242
$this->assertDatabaseHas(config('permission.table_names.roles'), [
@@ -51,16 +51,6 @@ public function it_will_defer_syncing_defined_role_to_artisan_with_custom_guard(
5151

5252
$this->assertTrue($role->hasPermissionTo('bar', 'admin'));
5353
$this->assertTrue($role->hasPermissionTo(FooAbility::One, 'admin'));
54-
}
55-
56-
#[Test]
57-
public function it_will_throw_an_exception_on_missing_permission(): void
58-
{
59-
$this->expectException(PermissionDoesNotExist::class);
60-
$this->expectExceptionMessage('There is no permission named `bar` for guard `web`');
61-
62-
SyncDefinedRole::run('foo role', 'web', [
63-
'bar',
64-
]);
54+
$this->assertTrue($role->hasPermissionTo('this-permission-is-new-and-will-be-created', 'admin'));
6555
}
6656
}

0 commit comments

Comments
 (0)