Update transformers and controllers to no longer pull an API key attribute
This commit is contained in:
parent
bd37978a98
commit
e9c633fd03
9 changed files with 91 additions and 173 deletions
|
@ -3,12 +3,13 @@
|
|||
namespace Pterodactyl\Transformers\Api\Application;
|
||||
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Http\Request;
|
||||
use Webmozart\Assert\Assert;
|
||||
use Pterodactyl\Models\ApiKey;
|
||||
use Illuminate\Container\Container;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
||||
use Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException;
|
||||
|
||||
/**
|
||||
* @method array transform(Model $model)
|
||||
|
@ -17,15 +18,7 @@ abstract class BaseTransformer extends TransformerAbstract
|
|||
{
|
||||
public const RESPONSE_TIMEZONE = 'UTC';
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Models\ApiKey
|
||||
*/
|
||||
private $key;
|
||||
|
||||
/**
|
||||
* Return the resource name for the JSONAPI output.
|
||||
*/
|
||||
abstract public function getResourceName(): string;
|
||||
protected Request $request;
|
||||
|
||||
/**
|
||||
* BaseTransformer constructor.
|
||||
|
@ -39,54 +32,69 @@ abstract class BaseTransformer extends TransformerAbstract
|
|||
}
|
||||
|
||||
/**
|
||||
* Set the HTTP request class being used for this request.
|
||||
*
|
||||
* @return $this
|
||||
* Return the resource name for the JSONAPI output.
|
||||
*/
|
||||
public function setKey(ApiKey $key)
|
||||
abstract public function getResourceName(): string;
|
||||
|
||||
/**
|
||||
* Sets the request on the instance.
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setRequest(Request $request): self
|
||||
{
|
||||
$this->key = $key;
|
||||
$this->request = $request;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the request instance being used for this transformer.
|
||||
* Returns a new transformer instance with the request set on the instance.
|
||||
*
|
||||
* @return \Pterodactyl\Transformers\Api\Application\BaseTransformer
|
||||
*/
|
||||
public function getKey(): ApiKey
|
||||
public static function fromRequest(Request $request)
|
||||
{
|
||||
return $this->key;
|
||||
return app(static::class)->setRequest($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the API key loaded onto the transformer has permission
|
||||
* to access a different resource. This is used when including other
|
||||
* models on a transformation request.
|
||||
*
|
||||
* @deprecated — prefer $user->can/cannot methods
|
||||
*/
|
||||
protected function authorize(string $resource): bool
|
||||
{
|
||||
return AdminAcl::check($this->getKey(), $resource, AdminAcl::READ);
|
||||
$token = $this->request->user()->currentAccessToken();
|
||||
if (!$token instanceof ApiKey || $token->key_type !== ApiKey::TYPE_APPLICATION) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return AdminAcl::check($token, $resource, AdminAcl::READ);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance of the transformer and pass along the currently
|
||||
* set API key.
|
||||
*
|
||||
* @return \Pterodactyl\Transformers\Api\Application\BaseTransformer
|
||||
* @template T of \Pterodactyl\Transformers\Api\Application\BaseTransformer
|
||||
*
|
||||
* @param class-string<T> $abstract
|
||||
*
|
||||
* @return T
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
||||
*
|
||||
* @noinspection PhpUndefinedClassInspection
|
||||
* @noinspection PhpDocSignatureInspection
|
||||
*/
|
||||
protected function makeTransformer(string $abstract, array $parameters = [])
|
||||
protected function makeTransformer(string $abstract)
|
||||
{
|
||||
/** @var \Pterodactyl\Transformers\Api\Application\BaseTransformer $transformer */
|
||||
$transformer = Container::getInstance()->makeWith($abstract, $parameters);
|
||||
$transformer->setKey($this->getKey());
|
||||
Assert::subclassOf($abstract, self::class);
|
||||
|
||||
if (!$transformer instanceof self) {
|
||||
throw new InvalidTransformerLevelException('Calls to ' . __METHOD__ . ' must return a transformer that is an instance of ' . __CLASS__);
|
||||
}
|
||||
|
||||
return $transformer;
|
||||
return $abstract::fromRequest($this->request);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Reference in a new issue