Path: blob/1.0-develop/app/Http/Requests/Api/Application/ApplicationApiRequest.php
10266 views
<?php12namespace Pterodactyl\Http\Requests\Api\Application;34use Webmozart\Assert\Assert;5use Pterodactyl\Models\ApiKey;6use Laravel\Sanctum\TransientToken;7use Illuminate\Validation\Validator;8use Illuminate\Database\Eloquent\Model;9use Pterodactyl\Services\Acl\Api\AdminAcl;10use Illuminate\Foundation\Http\FormRequest;11use Pterodactyl\Exceptions\PterodactylException;1213abstract class ApplicationApiRequest extends FormRequest14{15/**16* The resource that should be checked when performing the authorization17* function for this request.18*/19protected ?string $resource;2021/**22* The permission level that a given API key should have for accessing23* the defined $resource during the request cycle.24*/25protected int $permission = AdminAcl::NONE;2627/**28* Determine if the current user is authorized to perform29* the requested action against the API.30*31* @throws PterodactylException32*/33public function authorize(): bool34{35if (is_null($this->resource)) {36throw new PterodactylException('An ACL resource must be defined on API requests.');37}3839$token = $this->user()->currentAccessToken();40if ($token instanceof TransientToken) { // @phpstan-ignore instanceof.alwaysFalse41return true;42}4344if ($token->key_type === ApiKey::TYPE_ACCOUNT) {45return true;46}4748return AdminAcl::check($token, $this->resource, $this->permission);49}5051/**52* Default set of rules to apply to API requests.53*/54public function rules(): array55{56return [];57}5859/**60* Helper method allowing a developer to easily hook into this logic without having61* to remember what the method name is called or where to use it. By default this is62* a no-op.63*/64public function withValidator(Validator $validator): void65{66// do nothing67}6869/**70* Returns the named route parameter and asserts that it is a real model that71* exists in the database.72*73* @template T of \Illuminate\Database\Eloquent\Model74*75* @param class-string<T> $expect76*77* @return T78*79* @noinspection PhpDocSignatureInspection80*/81public function parameter(string $key, string $expect)82{83$value = $this->route()->parameter($key);8485Assert::isInstanceOf($value, $expect);86Assert::isInstanceOf($value, Model::class); // @phpstan-ignore staticMethod.alreadyNarrowedType87Assert::true($value->exists);8889/* @var T $value */90return $value;91}92}939495