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