Skip to content

Commit 3b55e9a

Browse files
committed
Add Gate integration for roles and permissions
Introduces a Gate::before handler to support role and permission checks using Laravel's authorization system. Updates the README with usage examples and adds a test to verify that roles and permissions are respected by the new Gate integration.
1 parent 9f80bc8 commit 3b55e9a

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,26 @@ You no longer need to stack `jwt.auth` + `load.access` manually—just use `micr
251251

252252
---
253253

254+
### Authorization
255+
256+
This package hooks into Laravel's Gate so Blade directives work with your roles and permissions. Any ability is treated as a permission by default; prefix an ability with `role:` or `permission:` to be explicit.
257+
258+
```blade
259+
@can('posts.create')
260+
<!-- user can create posts -->
261+
@endcan
262+
263+
@cannot('permission:posts.delete')
264+
<!-- no delete rights -->
265+
@endcannot
266+
267+
@canany(['role:admin', 'permission:posts.update'])
268+
<!-- admin or user with update permission -->
269+
@endcanany
270+
```
271+
272+
---
273+
254274
## Endpoints
255275

256276
### Health Check Endpoint

src/Providers/MicroserviceServiceProvider.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
use Illuminate\Foundation\Http\Kernel;
66
use Illuminate\Routing\Router;
7+
use Illuminate\Support\Facades\Gate;
78
use Illuminate\Support\Facades\Http;
89
use Illuminate\Support\ServiceProvider;
10+
use Kroderdev\LaravelMicroserviceCore\Contracts\AccessUserInterface;
911
use Kroderdev\LaravelMicroserviceCore\Contracts\ApiGatewayClientInterface;
1012
use Kroderdev\LaravelMicroserviceCore\Http\HealthCheckController;
1113
use Kroderdev\LaravelMicroserviceCore\Http\Middleware\LoadAccess;
@@ -47,6 +49,24 @@ public function boot(Router $router): void
4749

4850
$aliases = config('microservice.middleware_aliases', []);
4951

52+
// Authorization gates
53+
Gate::before(function ($user, string $ability) {
54+
if (! $user instanceof AccessUserInterface) {
55+
return null;
56+
}
57+
58+
if (str_starts_with($ability, 'role:')) {
59+
$role = substr($ability, 5);
60+
return $user->hasRole($role);
61+
}
62+
63+
if (str_starts_with($ability, 'permission:')) {
64+
$ability = substr($ability, 11);
65+
}
66+
67+
return $user->hasPermissionTo($ability);
68+
});
69+
5070
// JWT Middleware alias
5171
if (!empty($aliases['jwt_auth'])) {
5272
$router->aliasMiddleware($aliases['jwt_auth'], ValidateJwt::class);

tests/Auth/GateIntegrationTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Tests\Auth;
4+
5+
use Illuminate\Support\Facades\Gate;
6+
use Orchestra\Testbench\TestCase;
7+
use Kroderdev\LaravelMicroserviceCore\Providers\MicroserviceServiceProvider;
8+
use Kroderdev\LaravelMicroserviceCore\Auth\ExternalUser;
9+
10+
class GateIntegrationTest extends TestCase
11+
{
12+
protected function getPackageProviders($app)
13+
{
14+
return [MicroserviceServiceProvider::class];
15+
}
16+
17+
/** @test */
18+
public function gates_respect_roles_and_permissions()
19+
{
20+
$user = new ExternalUser(['id' => '1']);
21+
$user->loadAccess(['admin'], ['posts.view']);
22+
$this->be($user);
23+
24+
$this->assertTrue(Gate::allows('posts.view'));
25+
$this->assertTrue(Gate::allows('permission:posts.view'));
26+
$this->assertTrue(Gate::allows('role:admin'));
27+
$this->assertFalse(Gate::allows('role:guest'));
28+
$this->assertFalse(Gate::allows('posts.edit'));
29+
}
30+
}

0 commit comments

Comments
 (0)