2017-04-02 04:49:53 +00:00
|
|
|
<?php
|
|
|
|
|
2018-01-20 03:47:06 +00:00
|
|
|
namespace Pterodactyl\Transformers\Api\Application;
|
2017-04-02 04:49:53 +00:00
|
|
|
|
2017-04-02 20:51:56 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2022-10-14 16:59:20 +00:00
|
|
|
use League\Fractal\Resource\Collection;
|
|
|
|
use League\Fractal\Resource\NullResource;
|
2018-01-12 04:49:46 +00:00
|
|
|
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
2022-12-15 00:05:46 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Transformer;
|
2017-04-02 04:49:53 +00:00
|
|
|
|
2022-12-15 00:05:46 +00:00
|
|
|
class UserTransformer extends Transformer
|
2017-04-02 04:49:53 +00:00
|
|
|
{
|
2017-04-09 19:31:10 +00:00
|
|
|
/**
|
|
|
|
* List of resources that can be included.
|
|
|
|
*/
|
2022-05-04 23:11:42 +00:00
|
|
|
protected array $availableIncludes = ['servers'];
|
2017-04-08 16:05:29 +00:00
|
|
|
|
2018-01-26 03:26:06 +00:00
|
|
|
/**
|
|
|
|
* Return the resource name for the JSONAPI output.
|
|
|
|
*/
|
|
|
|
public function getResourceName(): string
|
|
|
|
{
|
|
|
|
return User::RESOURCE_NAME;
|
|
|
|
}
|
|
|
|
|
2017-04-02 04:49:53 +00:00
|
|
|
/**
|
2018-02-04 21:38:38 +00:00
|
|
|
* Return a transformed User model that can be consumed by external services.
|
2017-04-02 04:49:53 +00:00
|
|
|
*/
|
2022-12-15 00:05:46 +00:00
|
|
|
public function transform(User $model): array
|
2017-04-02 04:49:53 +00:00
|
|
|
{
|
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,
|
|
|
|
'avatar_url' => $model->avatarURL(),
|
|
|
|
'admin_role_id' => $model->admin_role_id,
|
|
|
|
'role_name' => $model->adminRoleName(),
|
|
|
|
'created_at' => self::formatTimestamp($model->created_at),
|
|
|
|
'updated_at' => self::formatTimestamp($model->updated_at),
|
2018-02-04 21:38:38 +00:00
|
|
|
];
|
2017-04-02 04:49:53 +00:00
|
|
|
}
|
2017-04-09 19:31:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the servers associated with this user.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function includeServers(User $user): Collection|NullResource
|
2017-04-09 19:31:10 +00:00
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {
|
2018-01-12 04:49:46 +00:00
|
|
|
return $this->null();
|
2017-04-09 19:31:10 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 00:05:46 +00:00
|
|
|
return $this->collection($user->servers, new ServerTransformer());
|
2017-04-09 19:31:10 +00:00
|
|
|
}
|
2017-04-02 04:49:53 +00:00
|
|
|
}
|