Skip to content

Commit bbd9b63

Browse files
committed
Add permissions_cache_ttl option
1 parent 920aed3 commit bbd9b63

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ Loads the authenticated user's roles and permissions, typically from a centraliz
125125
By default, the `ValidateJwt` middleware will automatically load `roles` and `permissions` from the JWT payload if they are present.
126126
However, if you have a centralized permission service, you can use `LoadAccess` to fetch and hydrate the latest roles and permissions for the user, ensuring up-to-date authorization data.
127127

128+
#### Configuration (`config/microservice.php`)
129+
130+
```php
131+
'permissions_cache_ttl' => env('PERMISSIONS_CACHE_TTL', 60),
132+
```
133+
128134
#### Usage
129135

130136
Apply after JWT authentication, or use the `microservice.auth` group for both:

src/Services/PermissionsClient.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ public function __construct(ApiGatewayClientInterface $gateway)
1818
public function getAccessFor(ExternalUser $user): array
1919
{
2020
$cacheKey = "user_access:{$user->getAuthIdentifier()}";
21+
$ttl = config('microservice.permissions_cache_ttl', 60);
2122

22-
return Cache::remember($cacheKey, 60, function () use ($user) {
23+
return Cache::remember($cacheKey, $ttl, function () use ($user) {
2324
$response = $this->gateway->get('/auth/permissions/' . $user->getAuthIdentifier());
2425

2526
if ($response->failed()) {

src/config/microservice.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
'role' => 'role',
1919
'permission' => 'permission',
2020
],
21-
22-
2321

2422
/*
2523
|--------------------------------------------------------------------------
@@ -118,4 +116,15 @@
118116
*/
119117
'url' => env('API_GATEWAY_URL', 'http://gateway.local'),
120118
],
119+
120+
/*
121+
|--------------------------------------------------------------------------
122+
| Permissions Cache
123+
|--------------------------------------------------------------------------
124+
|
125+
| Defines how long (in seconds) fetched roles and permissions are cached
126+
| for an authenticated user. Adjust via the PERMISSIONS_CACHE_TTL
127+
| environment variable to control cache duration.
128+
*/
129+
'permissions_cache_ttl' => env('PERMISSIONS_CACHE_TTL', 60),
121130
];

0 commit comments

Comments
 (0)