Add cache policy for ServerPolicy

10 second cache, just long enough to handle the page load without
making more than one MySQL call.
This commit is contained in:
Dane Everitt 2017-02-18 22:26:07 -05:00
parent f91e4c511e
commit efdc3e6fd8
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
2 changed files with 13 additions and 1 deletions

View file

@ -7,6 +7,9 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
### Fixed ### Fixed
* `[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. * `[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.
### Added
* New cache policy for ServerPolicy to avoid making 15+ queries per page load when confirming if a user has permission to perform an action.
## v0.6.0-pre.3 (Courageous Carniadactylus) ## v0.6.0-pre.3 (Courageous Carniadactylus)
### Fixed ### Fixed
* `[pre.2]` — Fixes bug where servers could not be manually deployed to nodes due to a broken SQL call. * `[pre.2]` — Fixes bug where servers could not be manually deployed to nodes due to a broken SQL call.

View file

@ -24,11 +24,14 @@
namespace Pterodactyl\Policies; namespace Pterodactyl\Policies;
use Cache;
use Carbon;
use Pterodactyl\Models\User; use Pterodactyl\Models\User;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
class ServerPolicy class ServerPolicy
{ {
/** /**
* Create a new policy instance. * Create a new policy instance.
* *
@ -53,7 +56,13 @@ class ServerPolicy
return true; return true;
} }
return $user->permissions()->server($server)->permission($permission)->exists(); $permissions = Cache::remember('ServerPolicy.' . $user->uuid . $server->uuid, Carbon::now()->addSeconds(10), function () use ($user, $server) {
return $user->permissions()->server($server)->get()->transform(function ($item) {
return $item->permission;
})->values();
});
return ($permissions->search($permission, true) !== false);
} }
/** /**