Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add once() Helper Function To Role and Permission Checking. #672

Closed
wants to merge 9 commits into from
13 changes: 13 additions & 0 deletions config/laratrust.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@
|
*/
'expiration_time' => 3600,

/*
|--------------------------------------------------------------------------
| Use the once() memoization function
|--------------------------------------------------------------------------
|
| Determines if the once() function should be used to cache the roles and permissions
| during the request. Only enable this if you are running Laravel 11 or above.
| If you are running Laravel 10 or below, you will need to require the
| spatie/once package via composer.
|
*/
'once' => false,
],

/*
Expand Down
20 changes: 20 additions & 0 deletions src/Traits/HasRolesAndPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@ public function hasRole(
mixed $team = null,
bool $requireAll = false
): bool {
if (Config::get('laratrust.cache.once') && function_exists('once')) {
return once(function () use ($name, $team, $requireAll) {
return $this->laratrustUserChecker()->currentUserHasRole(
$name,
$team,
$requireAll
);
});
}

return $this->laratrustUserChecker()->currentUserHasRole(
$name,
$team,
Expand All @@ -188,6 +198,16 @@ public function hasPermission(
mixed $team = null,
bool $requireAll = false
): bool {
if (Config::get('laratrust.cache.once') && function_exists('once')) {
return once(function () use ($permission, $team, $requireAll) {
return $this->laratrustUserChecker()->currentUserHasPermission(
$permission,
$team,
$requireAll
);
});
}

return $this->laratrustUserChecker()->currentUserHasPermission(
$permission,
$team,
Expand Down
23 changes: 21 additions & 2 deletions tests/Checkers/User/CheckerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,35 @@ public function userDisableTheRolesAndPermissionsCachingAssertions()
Permission::create(['name' => 'permission_e'])->id => ['team_id' => $team->id],
]);

// With cache
// With cache and without once
$this->app['config']->set('laratrust.cache.enabled', true);
$this->app['config']->set('laratrust.cache.once', false);
$this->user->hasRole('some_role');
$this->user->hasPermission('some_permission');
$this->assertTrue(Cache::has("laratrust_roles_for_users_{$this->user->id}"));
$this->assertTrue(Cache::has("laratrust_permissions_for_users_{$this->user->id}"));
$this->user->flushCache();

//With cache and once
$this->app['config']->set('laratrust.cache.enabled', true);
$this->app['config']->set('laratrust.cache.once', true);
$this->user->hasRole('some_role');
$this->user->hasPermission('some_permission');
$this->assertTrue(Cache::has("laratrust_roles_for_users_{$this->user->id}"));
$this->assertTrue(Cache::has("laratrust_permissions_for_users_{$this->user->id}"));
$this->user->flushCache();

// Without cache
// Without cache and with once
$this->app['config']->set('laratrust.cache.enabled', false);
$this->app['config']->set('laratrust.cache.once', true);
$this->user->hasRole('some_role');
$this->user->hasPermission('some_permission');
$this->assertFalse(Cache::has("laratrust_roles_for_users_{$this->user->id}"));
$this->assertFalse(Cache::has("laratrust_permissions_for_users_{$this->user->id}"));

//Without cache and without once
$this->app['config']->set('laratrust.cache.enabled', false);
$this->app['config']->set('laratrust.cache.once', false);
$this->user->hasRole('some_role');
$this->user->hasPermission('some_permission');
$this->assertFalse(Cache::has("laratrust_roles_for_users_{$this->user->id}"));
Expand Down