|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Pterodactyl - Panel |
| 4 | + * Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +namespace Pterodactyl\Policies; |
| 26 | + |
| 27 | +use Cache; |
| 28 | +use Carbon; |
| 29 | +use Pterodactyl\Models\User; |
| 30 | +use Pterodactyl\Models\APIKey as Key; |
| 31 | +use Pterodactyl\Models\APIPermission as Permission; |
| 32 | + |
| 33 | +class APIKeyPolicy |
| 34 | +{ |
| 35 | + /** |
| 36 | + * Checks if the API key has permission to perform an action. |
| 37 | + * |
| 38 | + * @param \Pterodactyl\Models\User $user |
| 39 | + * @param \Pterodactyl\Models\APIKey $key |
| 40 | + * @param string $permission |
| 41 | + * @return bool |
| 42 | + */ |
| 43 | + private function checkPermission(User $user, Key $key, $permission) |
| 44 | + { |
| 45 | + $permissions = Cache::remember('APIKeyPolicy.' . $user->uuid . $key->public, Carbon::now()->addSeconds(5), function () use ($key) { |
| 46 | + return $key->permissions()->get()->transform(function ($item) { |
| 47 | + return $item->permission; |
| 48 | + })->values(); |
| 49 | + }); |
| 50 | + |
| 51 | + return $permissions->search($permission, true) !== false; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Determine if API key is allowed to view all nodes. |
| 56 | + * |
| 57 | + * @param \Pterodactyl\Models\User $user |
| 58 | + * @param \Pterodactyl\Models\APIKey $key |
| 59 | + * @return bool |
| 60 | + */ |
| 61 | + public function listNodes(User $user, Key $key) |
| 62 | + { |
| 63 | + return $this->checkPermission($user, $key, 'list-nodes'); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Determine if API key is allowed to view a specific node. |
| 68 | + * |
| 69 | + * @param \Pterodactyl\Models\User $user |
| 70 | + * @param \Pterodactyl\Models\APIKey $key |
| 71 | + * @return bool |
| 72 | + */ |
| 73 | + public function viewNode(User $user, Key $key) |
| 74 | + { |
| 75 | + return $this->checkPermission($user, $key, 'view-node'); |
| 76 | + } |
| 77 | +} |
0 commit comments