Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Transformers/Api/Application/UserTransformer.php
10280 views
1
<?php
2
3
namespace Pterodactyl\Transformers\Api\Application;
4
5
use Pterodactyl\Models\User;
6
use League\Fractal\Resource\Collection;
7
use League\Fractal\Resource\NullResource;
8
use Pterodactyl\Services\Acl\Api\AdminAcl;
9
10
class UserTransformer extends BaseTransformer
11
{
12
/**
13
* List of resources that can be included.
14
*/
15
protected array $availableIncludes = ['servers'];
16
17
/**
18
* Return the resource name for the JSONAPI output.
19
*/
20
public function getResourceName(): string
21
{
22
return User::RESOURCE_NAME;
23
}
24
25
/**
26
* Return a transformed User model that can be consumed by external services.
27
*/
28
public function transform(User $user): array
29
{
30
return [
31
'id' => $user->id,
32
'external_id' => $user->external_id,
33
'uuid' => $user->uuid,
34
'username' => $user->username,
35
'email' => $user->email,
36
'first_name' => $user->name_first,
37
'last_name' => $user->name_last,
38
'language' => $user->language,
39
'root_admin' => (bool) $user->root_admin,
40
'2fa' => (bool) $user->use_totp,
41
'created_at' => $this->formatTimestamp($user->created_at),
42
'updated_at' => $this->formatTimestamp($user->updated_at),
43
];
44
}
45
46
/**
47
* Return the servers associated with this user.
48
*
49
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
50
*/
51
public function includeServers(User $user): Collection|NullResource
52
{
53
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {
54
return $this->null();
55
}
56
57
$user->loadMissing('servers');
58
59
return $this->collection($user->getRelation('servers'), $this->makeTransformer(ServerTransformer::class), 'server');
60
}
61
}
62
63