Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Http/Controllers/Api/Client/Servers/SubuserController.php
10280 views
1
<?php
2
3
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
4
5
use Illuminate\Http\Request;
6
use Pterodactyl\Models\Server;
7
use Illuminate\Http\JsonResponse;
8
use Pterodactyl\Facades\Activity;
9
use Pterodactyl\Models\Permission;
10
use Illuminate\Support\Facades\Log;
11
use Pterodactyl\Repositories\Eloquent\SubuserRepository;
12
use Pterodactyl\Services\Subusers\SubuserCreationService;
13
use Pterodactyl\Transformers\Api\Client\SubuserTransformer;
14
use Pterodactyl\Repositories\Wings\DaemonRevocationRepository;
15
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
16
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
17
use Pterodactyl\Http\Requests\Api\Client\Servers\Subusers\GetSubuserRequest;
18
use Pterodactyl\Http\Requests\Api\Client\Servers\Subusers\StoreSubuserRequest;
19
use Pterodactyl\Http\Requests\Api\Client\Servers\Subusers\DeleteSubuserRequest;
20
use Pterodactyl\Http\Requests\Api\Client\Servers\Subusers\UpdateSubuserRequest;
21
22
class SubuserController extends ClientApiController
23
{
24
/**
25
* SubuserController constructor.
26
*/
27
public function __construct(
28
private SubuserRepository $repository,
29
private SubuserCreationService $creationService,
30
private DaemonRevocationRepository $revocationRepository,
31
) {
32
parent::__construct();
33
}
34
35
/**
36
* Return the users associated with this server instance.
37
*/
38
public function index(GetSubuserRequest $request, Server $server): array
39
{
40
return $this->fractal->collection($server->subusers)
41
->transformWith($this->getTransformer(SubuserTransformer::class))
42
->toArray();
43
}
44
45
/**
46
* Returns a single subuser associated with this server instance.
47
*/
48
public function view(GetSubuserRequest $request): array
49
{
50
$subuser = $request->attributes->get('subuser');
51
52
return $this->fractal->item($subuser)
53
->transformWith($this->getTransformer(SubuserTransformer::class))
54
->toArray();
55
}
56
57
/**
58
* Create a new subuser for the given server.
59
*
60
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
61
* @throws \Pterodactyl\Exceptions\Service\Subuser\ServerSubuserExistsException
62
* @throws \Pterodactyl\Exceptions\Service\Subuser\UserIsServerOwnerException
63
* @throws \Throwable
64
*/
65
public function store(StoreSubuserRequest $request, Server $server): array
66
{
67
$response = $this->creationService->handle(
68
$server,
69
$request->input('email'),
70
$this->getDefaultPermissions($request)
71
);
72
73
Activity::event('server:subuser.create')
74
->subject($response->user)
75
->property(['email' => $request->input('email'), 'permissions' => $this->getDefaultPermissions($request)])
76
->log();
77
78
return $this->fractal->item($response)
79
->transformWith($this->getTransformer(SubuserTransformer::class))
80
->toArray();
81
}
82
83
/**
84
* Update a given subuser in the system for the server.
85
*
86
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
87
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
88
*/
89
public function update(UpdateSubuserRequest $request, Server $server): array
90
{
91
/** @var \Pterodactyl\Models\Subuser $subuser */
92
$subuser = $request->attributes->get('subuser');
93
94
$permissions = $this->getDefaultPermissions($request);
95
$current = $subuser->permissions;
96
97
sort($permissions);
98
sort($current);
99
100
$log = Activity::event('server:subuser.update')
101
->subject($subuser->user)
102
->property([
103
'email' => $subuser->user->email,
104
'old' => $current,
105
'new' => $permissions,
106
'revoked' => true,
107
]);
108
109
// Only update the database and hit up the Wings instance to invalidate JTI's if the permissions
110
// have actually changed for the user.
111
if ($permissions !== $current) {
112
$log->transaction(function ($instance) use ($request, $subuser, $server) {
113
$this->repository->update($subuser->id, [
114
'permissions' => $this->getDefaultPermissions($request),
115
]);
116
117
try {
118
$this->revocationRepository->setNode($server->node)->deauthorize(
119
$subuser->user->uuid,
120
[$server->uuid],
121
);
122
} catch (DaemonConnectionException $exception) {
123
// Don't block this request if we can't connect to the Wings instance. Chances are it is
124
// offline and the token will be invalid once Wings boots back.
125
Log::warning($exception, ['user_id' => $subuser->user_id, 'server_id' => $server->id]);
126
127
$instance->property('revoked', false);
128
}
129
});
130
}
131
132
$log->reset();
133
134
return $this->fractal->item($subuser->refresh())
135
->transformWith($this->getTransformer(SubuserTransformer::class))
136
->toArray();
137
}
138
139
/**
140
* Removes a subusers from a server's assignment.
141
*/
142
public function delete(DeleteSubuserRequest $request, Server $server): JsonResponse
143
{
144
/** @var \Pterodactyl\Models\Subuser $subuser */
145
$subuser = $request->attributes->get('subuser');
146
147
$log = Activity::event('server:subuser.delete')
148
->subject($subuser->user)
149
->property('email', $subuser->user->email)
150
->property('revoked', true);
151
152
$log->transaction(function ($instance) use ($server, $subuser) {
153
$subuser->delete();
154
155
try {
156
$this->revocationRepository->setNode($server->node)->deauthorize(
157
$subuser->user->uuid,
158
[$server->uuid],
159
);
160
} catch (DaemonConnectionException $exception) {
161
// Don't block this request if we can't connect to the Wings instance.
162
Log::warning($exception, ['user_id' => $subuser->user_id, 'server_id' => $server->id]);
163
164
$instance->property('revoked', false);
165
}
166
});
167
168
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
169
}
170
171
/**
172
* Returns the default permissions for subusers and parses out any permissions
173
* that were passed that do not also exist in the internally tracked list of
174
* permissions.
175
*/
176
protected function getDefaultPermissions(Request $request): array
177
{
178
$allowed = Permission::permissions()
179
->map(function ($value, $prefix) {
180
return array_map(function ($value) use ($prefix) {
181
return "$prefix.$value";
182
}, array_keys($value['keys']));
183
})
184
->flatten()
185
->all();
186
187
$cleaned = array_intersect($request->input('permissions') ?? [], $allowed);
188
189
return array_unique(array_merge($cleaned, [Permission::ACTION_WEBSOCKET_CONNECT]));
190
}
191
}
192
193