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

73 lines
2.1 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Transformers\Api\Application;
use Pterodactyl\Models\User;
use Pterodactyl\Services\Acl\Api\AdminAcl;
class UserTransformer extends BaseTransformer
{
/**
* List of resources that can be included.
*
* @var array
*/
2017-11-19 22:30:00 +00:00
protected $availableIncludes = ['servers'];
/**
* Return the resource name for the JSONAPI output.
*
* @return string
*/
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.
*
2021-01-04 19:44:44 +00:00
* @param \Pterodactyl\Models\User $model
* @return array
*/
2021-01-04 19:44:44 +00:00
public function transform(User $model): array
{
2018-02-04 21:38:38 +00:00
return [
2021-01-04 19:44:44 +00:00
'id' => $model->id,
'external_id' => $model->external_id,
'uuid' => $model->uuid,
'username' => $model->username,
'email' => $model->email,
'first_name' => $model->name_first,
'last_name' => $model->name_last,
'language' => $model->language,
'root_admin' => (bool) $model->root_admin,
'2fa' => (bool) $model->use_totp,
'role_name' => $model->root_admin ? 'Super Administrator' : 'None',
'created_at' => $this->formatTimestamp($model->created_at),
'updated_at' => $this->formatTimestamp($model->updated_at),
2018-02-04 21:38:38 +00:00
];
}
/**
* Return the servers associated with this user.
*
2017-11-19 22:30:00 +00:00
* @param \Pterodactyl\Models\User $user
2021-01-04 19:44:44 +00:00
*
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
*
2021-01-04 19:44:44 +00:00
* @throws \Illuminate\Contracts\Container\BindingResolutionException
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeServers(User $user)
{
if (! $this->authorize(AdminAcl::RESOURCE_SERVERS)) {
return $this->null();
}
$user->loadMissing('servers');
return $this->collection($user->getRelation('servers'), $this->makeTransformer(ServerTransformer::class), 'server');
}
}