misc_pterodactyl-panel/app/Transformers/Api/Application/SubuserTransformer.php

65 lines
1.7 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Transformers\Api\Application;
use Pterodactyl\Models\Subuser;
use League\Fractal\Resource\Item;
use League\Fractal\Resource\NullResource;
use Pterodactyl\Services\Acl\Api\AdminAcl;
2022-12-15 00:05:46 +00:00
use Pterodactyl\Transformers\Api\Transformer;
2022-12-15 00:05:46 +00:00
class SubuserTransformer extends Transformer
{
/**
* List of resources that can be included.
*/
2022-12-15 00:05:46 +00:00
protected array $availableIncludes = ['server', 'user'];
/**
* Return the resource name for the JSONAPI output.
*/
public function getResourceName(): string
{
return Subuser::RESOURCE_NAME;
}
/**
* Return a transformed Subuser model that can be consumed by external services.
*/
2022-12-15 00:05:46 +00:00
public function transform(Subuser $model): array
{
return [
2022-12-15 00:05:46 +00:00
'id' => $model->id,
'user_id' => $model->user_id,
'server_id' => $model->server_id,
'permissions' => $model->permissions,
'created_at' => self::formatTimestamp($model->created_at),
'updated_at' => self::formatTimestamp($model->updated_at),
];
}
/**
2022-12-15 00:05:46 +00:00
* Return a generic item of server for this subuser.
*/
2022-12-15 00:05:46 +00:00
public function includeServer(Subuser $subuser): Item|NullResource
{
2022-12-15 00:05:46 +00:00
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {
return $this->null();
}
2022-12-15 00:05:46 +00:00
return $this->item($subuser->server, new ServerTransformer());
}
/**
2022-12-15 00:05:46 +00:00
* Return a generic item of user for this subuser.
*/
2022-12-15 00:05:46 +00:00
public function includeUser(Subuser $subuser): Item|NullResource
{
2022-12-15 00:05:46 +00:00
if (!$this->authorize(AdminAcl::RESOURCE_USERS)) {
return $this->null();
}
2022-12-15 00:05:46 +00:00
return $this->item($subuser->user, new UserTransformer());
}
}