2018-01-26 03:26:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Transformers\Api\Application;
|
|
|
|
|
|
|
|
use Cake\Chronos\Chronos;
|
2021-01-15 16:20:15 +00:00
|
|
|
use Pterodactyl\Models\Node;
|
2018-01-26 03:26:06 +00:00
|
|
|
use Pterodactyl\Models\Database;
|
|
|
|
use Pterodactyl\Models\DatabaseHost;
|
|
|
|
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
|
|
|
|
|
|
|
class DatabaseHostTransformer extends BaseTransformer
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2021-01-15 16:20:15 +00:00
|
|
|
protected $availableIncludes = ['databases', 'nodes'];
|
2018-01-26 03:26:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the resource name for the JSONAPI output.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getResourceName(): string
|
|
|
|
{
|
|
|
|
return DatabaseHost::RESOURCE_NAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transform database host into a representation for the application API.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Models\DatabaseHost $model
|
2021-01-01 22:55:30 +00:00
|
|
|
*
|
2018-01-26 03:26:06 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function transform(DatabaseHost $model)
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'id' => $model->id,
|
|
|
|
'name' => $model->name,
|
|
|
|
'host' => $model->host,
|
|
|
|
'port' => $model->port,
|
|
|
|
'username' => $model->username,
|
|
|
|
'node' => $model->node_id,
|
|
|
|
'created_at' => Chronos::createFromFormat(Chronos::DEFAULT_TO_STRING_FORMAT, $model->created_at)
|
|
|
|
->setTimezone(config('app.timezone'))
|
|
|
|
->toIso8601String(),
|
|
|
|
'updated_at' => Chronos::createFromFormat(Chronos::DEFAULT_TO_STRING_FORMAT, $model->updated_at)
|
|
|
|
->setTimezone(config('app.timezone'))
|
|
|
|
->toIso8601String(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Include the databases associated with this host.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Models\DatabaseHost $model
|
2021-01-01 22:55:30 +00:00
|
|
|
*
|
2018-01-26 03:26:06 +00:00
|
|
|
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
|
2018-05-13 16:19:35 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
2021-01-05 21:52:49 +00:00
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2018-01-26 03:26:06 +00:00
|
|
|
*/
|
|
|
|
public function includeDatabases(DatabaseHost $model)
|
|
|
|
{
|
|
|
|
if (! $this->authorize(AdminAcl::RESOURCE_SERVER_DATABASES)) {
|
|
|
|
return $this->null();
|
|
|
|
}
|
|
|
|
|
|
|
|
$model->loadMissing('databases');
|
|
|
|
|
2021-01-15 16:20:15 +00:00
|
|
|
return $this->collection(
|
|
|
|
$model->getRelation('databases'),
|
|
|
|
$this->makeTransformer(ServerDatabaseTransformer::class),
|
|
|
|
Database::RESOURCE_NAME
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the nodes associated with this mount.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Models\Mount $mount
|
|
|
|
*
|
|
|
|
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
|
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
|
|
|
*/
|
|
|
|
public function includeNodes(DatabaseHost $model)
|
|
|
|
{
|
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_NODES)) {
|
|
|
|
return $this->null();
|
|
|
|
}
|
|
|
|
|
|
|
|
$model->loadMissing('nodes');
|
|
|
|
|
|
|
|
return $this->collection(
|
|
|
|
$model->getRelation('nodes'),
|
|
|
|
$this->makeTransformer(NodeTransformer::class),
|
|
|
|
Node::RESOURCE_NAME
|
|
|
|
);
|
2018-01-26 03:26:06 +00:00
|
|
|
}
|
|
|
|
}
|