2018-02-25 21:30:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Transformers\Api\Client;
|
|
|
|
|
2018-02-28 03:28:43 +00:00
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
use Webmozart\Assert\Assert;
|
|
|
|
use Pterodactyl\Models\Server;
|
2019-09-22 22:30:53 +00:00
|
|
|
use Illuminate\Container\Container;
|
2018-02-25 21:30:56 +00:00
|
|
|
use Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException;
|
|
|
|
use Pterodactyl\Transformers\Api\Application\BaseTransformer as BaseApplicationTransformer;
|
|
|
|
|
|
|
|
abstract class BaseClientTransformer extends BaseApplicationTransformer
|
|
|
|
{
|
2018-02-28 03:28:43 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Models\User
|
|
|
|
*/
|
|
|
|
private $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the user model of the user requesting this transformation.
|
|
|
|
*/
|
|
|
|
public function getUser(): User
|
|
|
|
{
|
|
|
|
return $this->user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the user model of the user requesting this transformation.
|
|
|
|
*/
|
|
|
|
public function setUser(User $user)
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
|
2018-02-25 21:30:56 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2018-02-28 03:28:43 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
2018-02-25 21:30:56 +00:00
|
|
|
*/
|
2018-02-28 03:28:43 +00:00
|
|
|
protected function authorize(string $ability, Server $server = null): bool
|
2018-02-25 21:30:56 +00:00
|
|
|
{
|
2018-02-28 03:28:43 +00:00
|
|
|
Assert::isInstanceOf($server, Server::class);
|
|
|
|
|
|
|
|
return $this->getUser()->can($ability, [$server]);
|
2018-02-25 21:30:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new instance of the transformer and pass along the currently
|
|
|
|
* set API key.
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
|
|
|
*/
|
|
|
|
protected function makeTransformer(string $abstract, array $parameters = [])
|
|
|
|
{
|
2019-09-22 22:30:53 +00:00
|
|
|
/** @var \Pterodactyl\Transformers\Api\Application\BaseTransformer $transformer */
|
|
|
|
$transformer = Container::getInstance()->makeWith($abstract, $parameters);
|
2021-07-28 04:23:11 +00:00
|
|
|
// $transformer->setKey($this->getKey());
|
2018-02-25 21:30:56 +00:00
|
|
|
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$transformer instanceof self) {
|
2018-02-25 21:30:56 +00:00
|
|
|
throw new InvalidTransformerLevelException('Calls to ' . __METHOD__ . ' must return a transformer that is an instance of ' . __CLASS__);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $transformer;
|
|
|
|
}
|
|
|
|
}
|