Skip to content

Commit efdc3e6

Browse files
committed
Add cache policy for ServerPolicy
10 second cache, just long enough to handle the page load without making more than one MySQL call.
1 parent f91e4c5 commit efdc3e6

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
77
### Fixed
88
* `[pre.3]` — Fixes bug in cache handler that doesn't cache against the user making the request. Would have allowed for users to access servers not belonging to themselves in production.
99

10+
### Added
11+
* New cache policy for ServerPolicy to avoid making 15+ queries per page load when confirming if a user has permission to perform an action.
12+
1013
## v0.6.0-pre.3 (Courageous Carniadactylus)
1114
### Fixed
1215
* `[pre.2]` — Fixes bug where servers could not be manually deployed to nodes due to a broken SQL call.

app/Policies/ServerPolicy.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@
2424

2525
namespace Pterodactyl\Policies;
2626

27+
use Cache;
28+
use Carbon;
2729
use Pterodactyl\Models\User;
2830
use Pterodactyl\Models\Server;
2931

3032
class ServerPolicy
3133
{
34+
3235
/**
3336
* Create a new policy instance.
3437
*
@@ -53,7 +56,13 @@ private function checkPermission(User $user, Server $server, $permission)
5356
return true;
5457
}
5558

56-
return $user->permissions()->server($server)->permission($permission)->exists();
59+
$permissions = Cache::remember('ServerPolicy.' . $user->uuid . $server->uuid, Carbon::now()->addSeconds(10), function () use ($user, $server) {
60+
return $user->permissions()->server($server)->get()->transform(function ($item) {
61+
return $item->permission;
62+
})->values();
63+
});
64+
65+
return ($permissions->search($permission, true) !== false);
5766
}
5867

5968
/**

0 commit comments

Comments
 (0)