1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gameap\Http\Controllers\API; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Gameap\Exceptions\Repositories\GdaemonTaskRepository\EmptyServerStartCommandException; |
7
|
|
|
use Gameap\Exceptions\Repositories\GdaemonTaskRepository\GdaemonTaskRepositoryException; |
8
|
|
|
use Gameap\Exceptions\Repositories\RecordExistExceptions; |
9
|
|
|
use Gameap\Helpers\PermissionHelper; |
10
|
|
|
use Gameap\Helpers\ServerPermissionHelper; |
11
|
|
|
use Gameap\Http\Controllers\AuthController; |
12
|
|
|
use Gameap\Http\Requests\Admin\ServerDestroyRequest; |
13
|
|
|
use Gameap\Http\Requests\API\SaveServerRequest; |
14
|
|
|
use Gameap\Http\Requests\API\ServerConsoleCommandRequest; |
15
|
|
|
use Gameap\Models\GdaemonTask; |
16
|
|
|
use Gameap\Models\Server; |
17
|
|
|
use Gameap\Models\User; |
18
|
|
|
use Gameap\Repositories\GdaemonTaskRepository; |
19
|
|
|
use Gameap\Repositories\ServerRepository; |
20
|
|
|
use Gameap\Services\ServerControlService; |
21
|
|
|
use Gameap\Services\ServerService; |
22
|
|
|
use Gameap\UseCases\Commands\CreateGameServerCommand; |
23
|
|
|
use Gameap\UseCases\Commands\EditGameServerCommand; |
24
|
|
|
use Gameap\UseCases\CreateGameServer; |
25
|
|
|
use Gameap\UseCases\EditGameServer; |
26
|
|
|
use Illuminate\Contracts\Auth\Factory as AuthFactory; |
27
|
|
|
use Illuminate\Http\Request; |
28
|
|
|
use Illuminate\Http\Response; |
29
|
|
|
use Illuminate\Support\Facades\Auth; |
30
|
|
|
use Spatie\QueryBuilder\QueryBuilder; |
31
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
32
|
|
|
|
33
|
|
|
class ServersController extends AuthController |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* The ServerRepository instance. |
37
|
|
|
* |
38
|
|
|
* @var \Gameap\Repositories\ServerRepository |
39
|
|
|
*/ |
40
|
|
|
public $repository; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The GdaemonTaskRepository instance. |
44
|
|
|
* |
45
|
|
|
* @var GdaemonTaskRepository |
46
|
|
|
*/ |
47
|
|
|
public $gdaemonTaskRepository; |
48
|
9 |
|
|
49
|
|
|
/** @var \Gameap\Services\ServerService */ |
50
|
|
|
public $serverService; |
51
|
|
|
|
52
|
|
|
/** @var \Gameap\Services\ServerControlService */ |
53
|
|
|
public $serverControlService; |
54
|
9 |
|
|
55
|
|
|
/** @var SerializerInterface */ |
56
|
9 |
|
protected $serializer; |
57
|
9 |
|
|
58
|
9 |
|
/** @var AuthFactory */ |
59
|
9 |
|
protected $authFactory; |
60
|
9 |
|
|
61
|
|
|
/** |
62
|
|
|
* ServersController constructor. |
63
|
|
|
* @param ServerRepository $repository |
64
|
|
|
*/ |
65
|
|
|
public function __construct( |
66
|
|
|
ServerRepository $repository, |
67
|
|
|
GdaemonTaskRepository $gdaemonTaskRepository, |
68
|
|
|
ServerService $serverService, |
69
|
9 |
|
ServerControlService $serverControlService, |
70
|
|
|
SerializerInterface $serializer, |
71
|
9 |
|
AuthFactory $authFactory |
72
|
9 |
|
) { |
73
|
|
|
parent::__construct(); |
74
|
|
|
|
75
|
9 |
|
$this->repository = $repository; |
76
|
6 |
|
$this->gdaemonTaskRepository = $gdaemonTaskRepository; |
77
|
3 |
|
$this->serverService = $serverService; |
78
|
3 |
|
$this->serverControlService = $serverControlService; |
79
|
3 |
|
$this->serializer = $serializer; |
80
|
3 |
|
$this->authFactory = $authFactory; |
81
|
3 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
3 |
|
* @param Server $server |
85
|
|
|
* |
86
|
|
|
* @return array|\Illuminate\Http\JsonResponse |
87
|
|
|
* |
88
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
89
|
|
|
*/ |
90
|
6 |
|
public function start(Server $server) |
91
|
|
|
{ |
92
|
|
|
$this->authorize(ServerPermissionHelper::CONTROL_ABILITY, $server); |
93
|
|
|
$this->authorize(ServerPermissionHelper::START_ABILITY, $server); |
94
|
|
|
|
95
|
|
|
try { |
96
|
|
|
$gdaemonTaskId = $this->serverControlService->start($server); |
97
|
|
|
} catch (GdaemonTaskRepositoryException $exception) { |
98
|
|
|
return $this->handleException($exception); |
99
|
|
|
} catch (RecordExistExceptions $exception) { |
100
|
|
|
$gdaemonTaskId = $this->gdaemonTaskRepository->getOneWorkingTaskId( |
101
|
|
|
$server->id, |
102
|
|
|
GdaemonTask::TASK_SERVER_START |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
if (!$gdaemonTaskId) { |
106
|
|
|
return $this->makeErrorResponse($exception->getMessage()); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return [ |
111
|
|
|
'gdaemonTaskId' => $gdaemonTaskId, |
112
|
|
|
]; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param Server $server |
117
|
|
|
* |
118
|
|
|
* @return array|\Illuminate\Http\JsonResponse |
119
|
|
|
* |
120
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
121
|
|
|
*/ |
122
|
|
|
public function stop(Server $server) |
123
|
|
|
{ |
124
|
|
|
$this->authorize(ServerPermissionHelper::CONTROL_ABILITY, $server); |
125
|
|
|
$this->authorize(ServerPermissionHelper::STOP_ABILITY, $server); |
126
|
|
|
|
127
|
|
|
try { |
128
|
|
|
$gdaemonTaskId = $this->serverControlService->stop($server); |
129
|
|
|
} catch (RecordExistExceptions $exception) { |
130
|
|
|
$gdaemonTaskId = $this->gdaemonTaskRepository->getOneWorkingTaskId( |
131
|
|
|
$server->id, |
132
|
|
|
GdaemonTask::TASK_SERVER_STOP |
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
if (!$gdaemonTaskId) { |
136
|
|
|
return $this->makeErrorResponse($exception->getMessage()); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return [ |
141
|
|
|
'gdaemonTaskId' => $gdaemonTaskId, |
142
|
|
|
]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param Server $server |
147
|
|
|
* @return array|\Illuminate\Http\JsonResponse |
148
|
|
|
* |
149
|
|
|
* @throws \Gameap\Exceptions\Repositories\RecordExistExceptions |
150
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
151
|
|
|
*/ |
152
|
|
|
public function restart(Server $server) |
153
|
|
|
{ |
154
|
|
|
$this->authorize(ServerPermissionHelper::CONTROL_ABILITY, $server); |
155
|
|
|
$this->authorize(ServerPermissionHelper::RESTART_ABILITY, $server); |
156
|
|
|
|
157
|
|
|
try { |
158
|
|
|
$gdaemonTaskId = $this->serverControlService->restart($server); |
159
|
|
|
} catch (GdaemonTaskRepositoryException $exception) { |
160
|
|
|
return $this->handleException($exception); |
161
|
|
|
} catch (RecordExistExceptions $exception) { |
162
|
|
|
$gdaemonTaskId = $this->gdaemonTaskRepository->getOneWorkingTaskId( |
163
|
|
|
$server->id, |
164
|
|
|
GdaemonTask::TASK_SERVER_RESTART |
165
|
|
|
); |
166
|
|
|
|
167
|
|
|
if (!$gdaemonTaskId) { |
168
|
|
|
return $this->makeErrorResponse($exception->getMessage()); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return [ |
173
|
|
|
'gdaemonTaskId' => $gdaemonTaskId, |
174
|
|
|
]; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param Server $server |
179
|
|
|
* @return array|\Illuminate\Http\JsonResponse |
180
|
|
|
* |
181
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
182
|
|
|
*/ |
183
|
|
|
public function update(Server $server) |
184
|
|
|
{ |
185
|
|
|
$this->authorize(ServerPermissionHelper::CONTROL_ABILITY, $server); |
186
|
|
|
$this->authorize(ServerPermissionHelper::UPDATE_ABILITY, $server); |
187
|
|
|
|
188
|
|
|
try { |
189
|
|
|
$gdaemonTaskId = $this->serverControlService->update($server); |
190
|
|
|
} catch (RecordExistExceptions $exception) { |
191
|
|
|
$gdaemonTaskId = $this->gdaemonTaskRepository->getOneWorkingTaskId( |
192
|
|
|
$server->id, |
193
|
|
|
GdaemonTask::TASK_SERVER_UPDATE |
194
|
|
|
); |
195
|
|
|
|
196
|
|
|
if (!$gdaemonTaskId) { |
197
|
|
|
return $this->makeErrorResponse($exception->getMessage()); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return [ |
202
|
|
|
'gdaemonTaskId' => $gdaemonTaskId, |
203
|
|
|
]; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param Server $server |
208
|
|
|
* @return array|\Illuminate\Http\JsonResponse |
209
|
|
|
* |
210
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
211
|
|
|
*/ |
212
|
|
|
public function install(Server $server) |
213
|
|
|
{ |
214
|
|
|
$this->authorize(ServerPermissionHelper::CONTROL_ABILITY, $server); |
215
|
|
|
$this->authorize(ServerPermissionHelper::UPDATE_ABILITY, $server); |
216
|
|
|
|
217
|
|
|
try { |
218
|
|
|
$gdaemonTaskId = $this->serverControlService->install($server); |
219
|
|
|
} catch (RecordExistExceptions $exception) { |
220
|
|
|
$gdaemonTaskId = $this->gdaemonTaskRepository->getOneWorkingTaskId( |
221
|
|
|
$server->id, |
222
|
|
|
GdaemonTask::TASK_SERVER_INSTALL |
223
|
|
|
); |
224
|
|
|
|
225
|
|
|
if (!$gdaemonTaskId) { |
226
|
|
|
return $this->makeErrorResponse($exception->getMessage()); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return [ |
231
|
|
|
'gdaemonTaskId' => $gdaemonTaskId, |
232
|
|
|
]; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @param Server $server |
237
|
|
|
* @return array|\Illuminate\Http\JsonResponse |
238
|
|
|
* |
239
|
|
|
* @throws \Gameap\Exceptions\Repositories\RecordExistExceptions |
240
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
241
|
|
|
*/ |
242
|
|
|
public function reinstall(Server $server) |
243
|
|
|
{ |
244
|
|
|
$this->authorize(ServerPermissionHelper::CONTROL_ABILITY, $server); |
245
|
|
|
$this->authorize(ServerPermissionHelper::UPDATE_ABILITY, $server); |
246
|
|
|
|
247
|
|
|
try { |
248
|
|
|
$deleteTaskId = $this->gdaemonTaskRepository->addServerDelete($server); |
249
|
|
|
$gdaemonTaskId = $this->gdaemonTaskRepository->addServerInstall($server, $deleteTaskId); |
250
|
|
|
} catch (RecordExistExceptions $exception) { |
251
|
|
|
return $this->makeErrorResponse($exception->getMessage()); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
return [ |
255
|
|
|
'gdaemonTaskId' => $gdaemonTaskId, |
256
|
|
|
]; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Get server status |
261
|
|
|
* @param Server $server |
262
|
|
|
* @return array |
263
|
|
|
* |
264
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
265
|
|
|
*/ |
266
|
|
|
public function getStatus(Server $server) |
267
|
|
|
{ |
268
|
|
|
$this->authorize(ServerPermissionHelper::CONTROL_ABILITY, $server); |
269
|
|
|
|
270
|
|
|
return [ |
271
|
|
|
'processActive' => $server->processActive(), |
272
|
|
|
]; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @param Server $server |
277
|
|
|
* @return array |
278
|
|
|
* |
279
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
280
|
|
|
*/ |
281
|
|
|
public function query(Server $server) |
282
|
|
|
{ |
283
|
|
|
$this->authorize(ServerPermissionHelper::CONTROL_ABILITY, $server); |
284
|
|
|
|
285
|
|
|
$query = $this->serverService->query($server); |
286
|
|
|
|
287
|
|
|
return $query; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
3 |
|
* @param Server $server |
292
|
|
|
* @return array |
293
|
3 |
|
* |
294
|
3 |
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
295
|
|
|
*/ |
296
|
|
|
public function consoleLog(Server $server) |
297
|
|
|
{ |
298
|
|
|
$this->authorize(ServerPermissionHelper::CONTROL_ABILITY, $server); |
299
|
3 |
|
$this->authorize(ServerPermissionHelper::CONSOLE_VIEW_ABILITY, $server); |
300
|
|
|
|
301
|
|
|
return [ |
302
|
|
|
'console' => $this->serverService->getConsoleLog($server), |
303
|
|
|
]; |
304
|
|
|
} |
305
|
|
|
|
306
|
3 |
|
/** |
307
|
|
|
* @param ServerConsoleCommandRequest $request |
308
|
3 |
|
* @param Server $server |
309
|
|
|
* @return array |
310
|
3 |
|
* |
311
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
312
|
|
|
*/ |
313
|
|
|
public function sendCommand(ServerConsoleCommandRequest $request, Server $server) |
314
|
3 |
|
{ |
315
|
|
|
$this->authorize(ServerPermissionHelper::CONTROL_ABILITY, $server); |
316
|
|
|
$this->authorize(ServerPermissionHelper::CONSOLE_SEND_ABILITY, $server); |
317
|
|
|
|
318
|
|
|
$command = $request->input('command'); |
319
|
|
|
$this->serverService->sendConsoleCommand($server, $command); |
320
|
|
|
|
321
|
|
|
return ['message' => 'success']; |
322
|
3 |
|
} |
323
|
|
|
|
324
|
3 |
|
public function search(Request $request) |
325
|
3 |
|
{ |
326
|
3 |
|
$query = $request->input('q'); |
327
|
1 |
|
return $this->repository->search($query); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
public function getList() |
331
|
|
|
{ |
332
|
|
|
/** @var User $currentUser */ |
333
|
|
|
$currentUser = $this->authFactory->guard()->user(); |
334
|
|
|
|
335
|
|
|
if ($currentUser->can(PermissionHelper::ADMIN_PERMISSIONS)) { |
336
|
|
|
return $this->repository->getAllServers(); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
return $this->repository->getServersForUser($currentUser->id); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
public function store(SaveServerRequest $request, CreateGameServer $createGameServer): array |
343
|
|
|
{ |
344
|
|
|
/** @var CreateGameServerCommand $command */ |
345
|
|
|
$command = $this->serializer->denormalize( |
346
|
|
|
$request->all(), |
347
|
|
|
CreateGameServerCommand::class, |
348
|
|
|
); |
349
|
|
|
|
350
|
|
|
$result = $createGameServer($command); |
351
|
|
|
|
352
|
|
|
return ['message' => 'success', 'result' => $result]; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
public function save(int $id, SaveServerRequest $request, EditGameServer $saveGameServer): array |
356
|
|
|
{ |
357
|
|
|
/** @var EditGameServerCommand $command */ |
358
|
|
|
$command = $this->serializer->denormalize( |
359
|
|
|
$request->all(), |
360
|
|
|
EditGameServerCommand::class, |
361
|
|
|
); |
362
|
|
|
|
363
|
|
|
$command->id = $id; |
364
|
|
|
|
365
|
|
|
$result = $saveGameServer($command); |
366
|
|
|
|
367
|
|
|
return ['message' => 'success', 'result' => $result->id]; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
public function destroy(ServerDestroyRequest $request, Server $server) |
371
|
|
|
{ |
372
|
|
|
if ($request->input('delete_files')) { |
373
|
|
|
try { |
374
|
|
|
$this->gdaemonTaskRepository->addServerDelete($server); |
375
|
|
|
} catch (RecordExistExceptions $e) { |
376
|
|
|
// Nothing |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
$server->delete(); |
380
|
|
|
} else { |
381
|
|
|
$server->forceDelete(); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
return ['message' => 'success']; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* @param Exception $exception |
389
|
|
|
* |
390
|
|
|
* @return \Illuminate\Http\JsonResponse |
391
|
|
|
*/ |
392
|
|
|
private function handleException(\Throwable $exception) |
393
|
|
|
{ |
394
|
|
|
if (Auth::user()->can(PermissionHelper::ADMIN_PERMISSIONS)) { |
395
|
|
|
$extraMessage = $this->getDocMessage($exception); |
396
|
|
|
} else { |
397
|
|
|
$extraMessage = (string)__('main.common_admin_error'); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
return $this->makeErrorResponse($exception->getMessage() . $extraMessage); |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* @param Exception $exception |
405
|
|
|
* @return string |
406
|
|
|
*/ |
407
|
|
|
private function getDocMessage(\Throwable $exception) |
408
|
|
|
{ |
409
|
|
|
$msg = ''; |
410
|
|
|
|
411
|
|
|
if ($exception instanceof EmptyServerStartCommandException) { |
412
|
|
|
$msg = __('gdaemon_tasks.empty_server_start_command_doc'); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
return is_string($msg) ? $msg : ''; |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* @param $message |
420
|
|
|
* @param int $code |
421
|
|
|
* @return \Illuminate\Http\JsonResponse |
422
|
|
|
*/ |
423
|
|
|
private function makeErrorResponse($message, $code = Response::HTTP_UNPROCESSABLE_ENTITY) |
424
|
|
|
{ |
425
|
|
|
return response()->json([ |
426
|
|
|
'message' => $message, |
427
|
|
|
'http_code' => $code, |
428
|
|
|
], $code); |
429
|
|
|
} |
430
|
|
|
} |
431
|
|
|
|