2018-01-12 04:49:46 +00:00
|
|
|
<?php
|
|
|
|
|
2018-01-20 01:58:57 +00:00
|
|
|
namespace Pterodactyl\Http\Requests\Api\Application;
|
2018-01-12 04:49:46 +00:00
|
|
|
|
2022-05-22 18:10:01 +00:00
|
|
|
use Webmozart\Assert\Assert;
|
2022-05-14 21:31:53 +00:00
|
|
|
use Illuminate\Validation\Validator;
|
2022-05-22 18:10:01 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2018-01-20 01:58:57 +00:00
|
|
|
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
2018-01-12 04:49:46 +00:00
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
use Pterodactyl\Exceptions\PterodactylException;
|
|
|
|
|
2018-01-20 03:47:06 +00:00
|
|
|
abstract class ApplicationApiRequest extends FormRequest
|
2018-01-12 04:49:46 +00:00
|
|
|
{
|
2018-03-02 02:00:14 +00:00
|
|
|
/**
|
|
|
|
* Tracks if the request has been validated internally or not to avoid
|
|
|
|
* making duplicate validation calls.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $hasValidated = false;
|
|
|
|
|
2018-01-12 04:49:46 +00:00
|
|
|
/**
|
|
|
|
* The resource that should be checked when performing the authorization
|
|
|
|
* function for this request.
|
|
|
|
*
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected $resource;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The permission level that a given API key should have for accessing
|
|
|
|
* the defined $resource during the request cycle.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2018-01-20 01:58:57 +00:00
|
|
|
protected $permission = AdminAcl::NONE;
|
2018-01-12 04:49:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the current user is authorized to perform
|
2018-05-13 14:50:56 +00:00
|
|
|
* the requested action against the API.
|
2018-01-12 04:49:46 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\PterodactylException
|
|
|
|
*/
|
|
|
|
public function authorize(): bool
|
|
|
|
{
|
|
|
|
if (is_null($this->resource)) {
|
|
|
|
throw new PterodactylException('An ACL resource must be defined on API requests.');
|
|
|
|
}
|
|
|
|
|
2022-05-22 18:10:01 +00:00
|
|
|
return AdminAcl::check($this->attributes->get('api_key'), $this->resource, $this->permission);
|
2018-01-12 04:49:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default set of rules to apply to API requests.
|
|
|
|
*/
|
|
|
|
public function rules(): array
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2022-05-14 21:31:53 +00:00
|
|
|
/**
|
|
|
|
* Helper method allowing a developer to easily hook into this logic without having
|
|
|
|
* to remember what the method name is called or where to use it. By default this is
|
|
|
|
* a no-op.
|
|
|
|
*/
|
|
|
|
public function withValidator(Validator $validator): void
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2018-03-02 02:00:14 +00:00
|
|
|
/**
|
2022-05-22 18:10:01 +00:00
|
|
|
* Returns the named route parameter and asserts that it is a real model that
|
|
|
|
* exists in the database.
|
2018-03-02 02:00:14 +00:00
|
|
|
*
|
2022-05-22 18:10:01 +00:00
|
|
|
* @template T of \Illuminate\Database\Eloquent\Model
|
2018-01-12 04:49:46 +00:00
|
|
|
*
|
2022-05-22 18:10:01 +00:00
|
|
|
* @param class-string<T> $expect
|
2018-01-12 04:49:46 +00:00
|
|
|
*
|
2022-05-22 18:10:01 +00:00
|
|
|
* @return T
|
|
|
|
* @noinspection PhpUndefinedClassInspection
|
|
|
|
* @noinspection PhpDocSignatureInspection
|
2018-01-12 04:49:46 +00:00
|
|
|
*/
|
2022-05-22 18:10:01 +00:00
|
|
|
public function parameter(string $key, string $expect)
|
2018-01-12 04:49:46 +00:00
|
|
|
{
|
2022-05-22 18:10:01 +00:00
|
|
|
$value = $this->route()->parameter($key);
|
2018-03-02 02:00:14 +00:00
|
|
|
|
2022-05-22 18:10:01 +00:00
|
|
|
Assert::isInstanceOf($value, $expect);
|
|
|
|
Assert::isInstanceOf($value, Model::class);
|
|
|
|
Assert::true($value->exists);
|
2018-01-12 04:49:46 +00:00
|
|
|
|
2022-05-22 18:10:01 +00:00
|
|
|
/* @var T $value */
|
|
|
|
return $value;
|
2018-01-12 04:49:46 +00:00
|
|
|
}
|
|
|
|
}
|