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

60 lines
1.7 KiB
PHP
Raw Permalink Normal View History

<?php
namespace Pterodactyl\Transformers\Api\Application;
use Pterodactyl\Models\User;
use League\Fractal\Resource\Collection;
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 UserTransformer extends Transformer
{
/**
* List of resources that can be included.
*/
protected array $availableIncludes = ['servers'];
/**
* Return the resource name for the JSONAPI output.
*/
public function getResourceName(): string
{
return User::RESOURCE_NAME;
}
/**
2018-02-04 21:38:38 +00:00
* Return a transformed User model that can be consumed by external services.
*/
2022-12-15 00:05:46 +00:00
public function transform(User $model): array
{
2018-02-04 21:38:38 +00:00
return [
2022-12-15 00:05:46 +00:00
'id' => $model->id,
'external_id' => $model->external_id,
'uuid' => $model->uuid,
'username' => $model->username,
'email' => $model->email,
'language' => $model->language,
'root_admin' => (bool) $model->root_admin,
'2fa' => (bool) $model->use_totp,
2022-12-15 19:26:34 +00:00
'avatar_url' => $model->avatar_url,
2022-12-15 00:05:46 +00:00
'admin_role_id' => $model->admin_role_id,
2022-12-15 19:26:34 +00:00
'role_name' => $model->admin_role_name,
2022-12-15 00:05:46 +00:00
'created_at' => self::formatTimestamp($model->created_at),
'updated_at' => self::formatTimestamp($model->updated_at),
2018-02-04 21:38:38 +00:00
];
}
/**
* Return the servers associated with this user.
*/
public function includeServers(User $user): Collection|NullResource
{
2021-01-23 20:33:34 +00:00
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {
return $this->null();
}
2022-12-15 00:05:46 +00:00
return $this->collection($user->servers, new ServerTransformer());
}
}