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;
|
2021-08-07 20:25:06 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Transformer;
|
2017-04-02 04:49:53 +00:00
|
|
|
|
2021-08-07 20:25:06 +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.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
public function getResourceName(): string
|
|
|
|
{
|
|
|
|
return User::RESOURCE_NAME;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
'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-08-07 20:25:06 +00:00
|
|
|
'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
|
|
|
|
2021-07-25 21:51:39 +00:00
|
|
|
/**
|
|
|
|
* Return the role associated with this user.
|
|
|
|
*
|
|
|
|
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
|
|
|
|
*/
|
|
|
|
public function includeRole(User $user)
|
|
|
|
{
|
2021-09-13 06:58:39 +00:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_ROLES) || is_null($user->adminRole)) {
|
2021-07-25 21:51:39 +00:00
|
|
|
return $this->null();
|
|
|
|
}
|
|
|
|
|
2021-08-07 20:25:06 +00:00
|
|
|
return $this->item($user->adminRole, new AdminRoleTransformer());
|
2021-07-25 21:51:39 +00:00
|
|
|
}
|
|
|
|
|
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
|
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
|
|
|
}
|
|
|
|
|
2021-08-07 20:25:06 +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
|
|
|
}
|