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
|
|
|
|
2018-01-14 18:06:15 +00:00
|
|
|
use Pterodactyl\Models\ApiKey;
|
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-27 18:38:56 +00:00
|
|
|
use Pterodactyl\Http\Middleware\Api\ApiSubstituteBindings;
|
2018-01-12 04:49:46 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2018-02-24 17:11:57 +00:00
|
|
|
use Symfony\Component\Routing\Exception\InvalidParameterException;
|
2018-01-12 04:49:46 +00:00
|
|
|
|
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.');
|
|
|
|
}
|
|
|
|
|
2020-12-28 19:47:08 +00:00
|
|
|
if (! is_null($this->user())) {
|
|
|
|
return $this->user()->root_admin;
|
|
|
|
}
|
|
|
|
|
2018-01-20 01:58:57 +00:00
|
|
|
return AdminAcl::check($this->key(), $this->resource, $this->permission);
|
2018-01-12 04:49:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the requested resource exists on the server.
|
|
|
|
*/
|
|
|
|
public function resourceExists(): bool
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default set of rules to apply to API requests.
|
|
|
|
*/
|
|
|
|
public function rules(): array
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the API key being used for the request.
|
|
|
|
*/
|
2018-01-14 18:06:15 +00:00
|
|
|
public function key(): ApiKey
|
2018-01-12 04:49:46 +00:00
|
|
|
{
|
|
|
|
return $this->attributes->get('api_key');
|
|
|
|
}
|
|
|
|
|
2018-01-27 18:38:56 +00:00
|
|
|
/**
|
2018-02-24 17:11:57 +00:00
|
|
|
* Grab a model from the route parameters. If no model is found in the
|
|
|
|
* binding mappings an exception will be thrown.
|
2018-01-27 18:38:56 +00:00
|
|
|
*
|
|
|
|
* @return mixed
|
2021-01-23 20:33:34 +00:00
|
|
|
*
|
2019-09-06 04:41:20 +00:00
|
|
|
* @deprecated
|
2018-02-24 17:11:57 +00:00
|
|
|
*
|
|
|
|
* @throws \Symfony\Component\Routing\Exception\InvalidParameterException
|
2018-01-27 18:38:56 +00:00
|
|
|
*/
|
2018-02-24 17:11:57 +00:00
|
|
|
public function getModel(string $model)
|
2018-01-27 18:38:56 +00:00
|
|
|
{
|
|
|
|
$parameterKey = array_get(array_flip(ApiSubstituteBindings::getMappings()), $model);
|
|
|
|
|
2018-02-24 17:11:57 +00:00
|
|
|
if (is_null($parameterKey)) {
|
2021-01-23 20:33:34 +00:00
|
|
|
throw new InvalidParameterException();
|
2018-01-27 18:38:56 +00:00
|
|
|
}
|
|
|
|
|
2018-02-24 17:11:57 +00:00
|
|
|
return $this->route()->parameter($parameterKey);
|
2018-01-27 18:38:56 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 02:00:14 +00:00
|
|
|
/**
|
|
|
|
* Validate that the resource exists and can be accessed prior to booting
|
|
|
|
* the validator and attempting to use the data.
|
|
|
|
*
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
|
|
|
protected function prepareForValidation()
|
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->passesAuthorization()) {
|
2018-03-02 02:00:14 +00:00
|
|
|
$this->failedAuthorization();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->hasValidated = true;
|
|
|
|
}
|
|
|
|
|
2018-01-26 04:34:53 +00:00
|
|
|
/*
|
2018-01-12 04:49:46 +00:00
|
|
|
* Determine if the request passes the authorization check as well
|
|
|
|
* as the exists check.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*
|
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
|
|
|
|
*/
|
|
|
|
protected function passesAuthorization()
|
|
|
|
{
|
2018-03-02 02:00:14 +00:00
|
|
|
// If we have already validated we do not need to call this function
|
|
|
|
// again. This is needed to work around Laravel's normal auth validation
|
|
|
|
// that occurs after validating the request params since we are doing auth
|
|
|
|
// validation in the prepareForValidation() function.
|
|
|
|
if ($this->hasValidated) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!parent::passesAuthorization()) {
|
2018-01-26 04:34:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-01-12 04:49:46 +00:00
|
|
|
|
|
|
|
// Only let the user know that a resource does not exist if they are
|
|
|
|
// authenticated to access the endpoint. This avoids exposing that
|
|
|
|
// an item exists (or does not exist) to the user until they can prove
|
|
|
|
// that they have permission to know about it.
|
2021-01-23 20:33:34 +00:00
|
|
|
if ($this->attributes->get('is_missing_model', false) || !$this->resourceExists()) {
|
2018-02-08 03:56:11 +00:00
|
|
|
throw new NotFoundHttpException(trans('exceptions.api.resource_not_found'));
|
2018-01-12 04:49:46 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 04:34:53 +00:00
|
|
|
return true;
|
2018-01-12 04:49:46 +00:00
|
|
|
}
|
|
|
|
}
|