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-22 23:03:51 +00:00
|
|
|
use Pterodactyl\Models\ApiKey;
|
2022-05-22 19:37:39 +00:00
|
|
|
use Laravel\Sanctum\TransientToken;
|
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
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The resource that should be checked when performing the authorization
|
|
|
|
* function for this request.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
protected ?string $resource;
|
2018-01-12 04:49:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The permission level that a given API key should have for accessing
|
|
|
|
* the defined $resource during the request cycle.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
protected int $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
|
|
|
*
|
2022-11-28 16:56:03 +00:00
|
|
|
* @throws PterodactylException
|
2018-01-12 04:49:46 +00:00
|
|
|
*/
|
|
|
|
public function authorize(): bool
|
|
|
|
{
|
|
|
|
if (is_null($this->resource)) {
|
|
|
|
throw new PterodactylException('An ACL resource must be defined on API requests.');
|
|
|
|
}
|
|
|
|
|
2022-05-22 19:37:39 +00:00
|
|
|
$token = $this->user()->currentAccessToken();
|
|
|
|
if ($token instanceof TransientToken) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-28 16:56:03 +00:00
|
|
|
/** @var ApiKey $token */
|
2022-05-22 23:03:51 +00:00
|
|
|
if ($token->key_type === ApiKey::TYPE_ACCOUNT) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-05-22 19:37:39 +00:00
|
|
|
return AdminAcl::check($token, $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
|
2022-10-14 16:59:20 +00:00
|
|
|
*
|
2022-05-22 18:10:01 +00:00
|
|
|
* @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-11-28 16:56:03 +00:00
|
|
|
/** @var ApiKey $value */
|
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
|
|
|
}
|
|
|
|
}
|