2018-01-11 22:49:46 -06:00
|
|
|
<?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;
|
2018-01-11 22:49:46 -06:00
|
|
|
|
2022-05-22 15:37:39 -04:00
|
|
|
use Laravel\Sanctum\TransientToken;
|
2018-01-11 22:49:46 -06:00
|
|
|
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;
|
2018-01-11 22:49:46 -06:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
abstract class ApplicationApiRequest extends ApiRequest
|
2018-01-11 22:49:46 -06:00
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* 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;
|
2018-01-11 22:49:46 -06:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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;
|
2018-01-11 22:49:46 -06:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine if the current user is authorized to perform
|
2018-05-13 10:50:56 -04:00
|
|
|
* the requested action against the API.
|
2018-01-11 22:49:46 -06:00
|
|
|
*
|
2024-10-21 19:18:20 -06:00
|
|
|
* @throws PterodactylException
|
2018-01-11 22:49:46 -06:00
|
|
|
*/
|
|
|
|
|
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.');
|
2018-01-11 22:49:46 -06:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
$token = $user->currentAccessToken();
|
|
|
|
|
if ($token === null || $token instanceof TransientToken) {
|
2022-05-22 15:37:39 -04:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-22 19:03:51 -04:00
|
|
|
if ($token->key_type === ApiKey::TYPE_ACCOUNT) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-22 15:37:39 -04:00
|
|
|
return AdminAcl::check($token, $this->resource, $this->permission);
|
2018-01-11 22:49:46 -06:00
|
|
|
}
|
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;
|
|
|
|
|
}
|
2018-01-11 22:49:46 -06:00
|
|
|
}
|