2017-04-02 06:49:53 +02:00
|
|
|
<?php
|
|
|
|
|
2018-01-20 04:47:06 +01:00
|
|
|
namespace Pterodactyl\Transformers\Api\Application;
|
2017-04-02 06:49:53 +02:00
|
|
|
|
2017-04-02 22:51:56 +02:00
|
|
|
use Pterodactyl\Models\User;
|
2022-10-14 18:59:20 +02:00
|
|
|
use League\Fractal\Resource\Collection;
|
|
|
|
use League\Fractal\Resource\NullResource;
|
2018-01-12 05:49:46 +01:00
|
|
|
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
2017-04-02 06:49:53 +02:00
|
|
|
|
2018-01-12 05:49:46 +01:00
|
|
|
class UserTransformer extends BaseTransformer
|
2017-04-02 06:49:53 +02:00
|
|
|
{
|
2017-04-09 21:31:10 +02:00
|
|
|
/**
|
|
|
|
* List of resources that can be included.
|
|
|
|
*/
|
2022-05-05 01:11:42 +02:00
|
|
|
protected array $availableIncludes = ['servers'];
|
2017-04-08 18:05:29 +02:00
|
|
|
|
2018-01-26 04:26:06 +01:00
|
|
|
/**
|
|
|
|
* Return the resource name for the JSONAPI output.
|
|
|
|
*/
|
|
|
|
public function getResourceName(): string
|
|
|
|
{
|
|
|
|
return User::RESOURCE_NAME;
|
|
|
|
}
|
|
|
|
|
2017-04-02 06:49:53 +02:00
|
|
|
/**
|
2018-02-04 22:38:38 +01:00
|
|
|
* Return a transformed User model that can be consumed by external services.
|
2017-04-02 06:49:53 +02:00
|
|
|
*/
|
2017-11-19 23:30:00 +01:00
|
|
|
public function transform(User $user): array
|
2017-04-02 06:49:53 +02:00
|
|
|
{
|
2018-02-04 22:38:38 +01:00
|
|
|
return [
|
|
|
|
'id' => $user->id,
|
|
|
|
'external_id' => $user->external_id,
|
|
|
|
'uuid' => $user->uuid,
|
|
|
|
'username' => $user->username,
|
|
|
|
'email' => $user->email,
|
|
|
|
'first_name' => $user->name_first,
|
|
|
|
'last_name' => $user->name_last,
|
|
|
|
'language' => $user->language,
|
|
|
|
'root_admin' => (bool) $user->root_admin,
|
|
|
|
'2fa' => (bool) $user->use_totp,
|
|
|
|
'created_at' => $this->formatTimestamp($user->created_at),
|
|
|
|
'updated_at' => $this->formatTimestamp($user->updated_at),
|
|
|
|
];
|
2017-04-02 06:49:53 +02:00
|
|
|
}
|
2017-04-09 21:31:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the servers associated with this user.
|
|
|
|
*
|
2018-03-05 05:35:57 +01:00
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
2017-04-09 21:31:10 +02:00
|
|
|
*/
|
2022-10-14 18:59:20 +02:00
|
|
|
public function includeServers(User $user): Collection|NullResource
|
2017-04-09 21:31:10 +02:00
|
|
|
{
|
2021-01-23 21:33:34 +01:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {
|
2018-01-12 05:49:46 +01:00
|
|
|
return $this->null();
|
2017-04-09 21:31:10 +02:00
|
|
|
}
|
|
|
|
|
2018-01-11 06:19:03 +01:00
|
|
|
$user->loadMissing('servers');
|
2017-04-09 21:31:10 +02:00
|
|
|
|
2018-01-12 05:49:46 +01:00
|
|
|
return $this->collection($user->getRelation('servers'), $this->makeTransformer(ServerTransformer::class), 'server');
|
2017-04-09 21:31:10 +02:00
|
|
|
}
|
2017-04-02 06:49:53 +02:00
|
|
|
}
|