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;
|
2018-01-12 04:49:46 +00:00
|
|
|
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
2017-04-02 04:49:53 +00:00
|
|
|
|
2018-01-12 04:49:46 +00:00
|
|
|
class UserTransformer extends BaseTransformer
|
2017-04-02 04:49:53 +00:00
|
|
|
{
|
2017-04-09 19:31:10 +00:00
|
|
|
/**
|
|
|
|
* List of resources that can be included.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2021-07-25 21:51:39 +00:00
|
|
|
protected $availableIncludes = ['role', '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
|
|
|
*/
|
2021-01-04 19:44:44 +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 [
|
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,
|
2021-01-07 17:21:09 +00:00
|
|
|
'avatar_url' => $model->avatarURL(),
|
2021-07-25 21:51:39 +00:00
|
|
|
'admin_role_id' => $model->admin_role_id,
|
2021-01-20 01:51:29 +00:00
|
|
|
'role_name' => $model->adminRoleName(),
|
2021-01-04 19:44:44 +00:00
|
|
|
'created_at' => $this->formatTimestamp($model->created_at),
|
|
|
|
'updated_at' => $this->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
|
|
|
|
2021-07-25 21:51:39 +00:00
|
|
|
/**
|
|
|
|
* Return the role associated with this user.
|
|
|
|
*
|
|
|
|
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
|
|
|
|
*
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
|
|
|
*/
|
|
|
|
public function includeRole(User $user)
|
|
|
|
{
|
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_ROLES)) {
|
|
|
|
return $this->null();
|
|
|
|
}
|
|
|
|
|
|
|
|
$user->loadMissing('adminRole');
|
|
|
|
|
|
|
|
return $this->item($user->getRelation('adminRole'), $this->makeTransformer(AdminRoleTransformer::class), 'admin_role');
|
|
|
|
}
|
|
|
|
|
2017-04-09 19:31:10 +00:00
|
|
|
/**
|
|
|
|
* Return the servers associated with this user.
|
|
|
|
*
|
2018-01-12 04:49:46 +00:00
|
|
|
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
|
2018-03-05 04:35:57 +00:00
|
|
|
*
|
2021-01-04 19:44:44 +00:00
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2018-03-05 04:35:57 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
2017-04-09 19:31:10 +00:00
|
|
|
*/
|
|
|
|
public function includeServers(User $user)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2018-01-11 05:19:03 +00:00
|
|
|
$user->loadMissing('servers');
|
2017-04-09 19:31:10 +00:00
|
|
|
|
2018-01-12 04:49:46 +00:00
|
|
|
return $this->collection($user->getRelation('servers'), $this->makeTransformer(ServerTransformer::class), 'server');
|
2017-04-09 19:31:10 +00:00
|
|
|
}
|
2017-04-02 04:49:53 +00:00
|
|
|
}
|