Files

72 lines
2.1 KiB
PHP
Raw Permalink Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
2018-01-19 19:58:57 -06:00
namespace Pterodactyl\Http\Requests\Api\Application;
use Laravel\Sanctum\TransientToken;
use Pterodactyl\Exceptions\PterodactylException;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Http\Requests\Api\ApiRequest;
use Pterodactyl\Models\ApiKey;
use Pterodactyl\Services\Acl\Api\AdminAcl;
2026-10-05 18:10:18 -04:00
abstract class ApplicationApiRequest extends ApiRequest
{
/**
* The resource that should be checked when performing the authorization
* function for this request.
*/
2026-10-05 18:10:18 -04:00
protected ?string $resource = null;
/**
* The permission level that a given API key should have for accessing
* the defined $resource during the request cycle.
*/
2022-10-14 10:59:20 -06:00
protected int $permission = AdminAcl::NONE;
/**
* Determine if the current user is authorized to perform
2018-05-13 10:50:56 -04:00
* the requested action against the API.
*
2024-10-21 19:18:20 -06:00
* @throws PterodactylException
*/
public function authorize(): bool
{
2026-10-05 18:10:18 -04:00
throw_if(($this->resource) === null, PterodactylException::class, 'An ACL resource must be defined on API requests.');
$user = $this->user();
throw_if($user === null, PterodactylException::class, 'Application API requests require an authenticated user.');
2026-10-05 18:10:18 -04:00
$token = $user->currentAccessToken();
if ($token === null || $token instanceof TransientToken) {
return true;
}
if ($token->key_type === ApiKey::TYPE_ACCOUNT) {
return true;
}
return AdminAcl::check($token, $this->resource, $this->permission);
}
2026-10-12 15:27:35 +05:45
/**
* Whether the request may act on root administrators. Sessions and account keys already
* require one; application keys need write access to every resource.
*/
protected function canManageAdministrators(): bool
{
$token = $this->user()?->currentAccessToken();
if (! $token instanceof ApiKey || $token->key_type !== ApiKey::TYPE_APPLICATION) {
return true;
}
foreach (AdminAcl::getResourceList() as $resource) {
if (! AdminAcl::check($token, $resource, AdminAcl::WRITE)) {
return false;
}
}
return true;
}
}