2017-07-23 01:15:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Servers;
|
|
|
|
|
2020-12-06 20:16:12 +00:00
|
|
|
use Illuminate\Support\Arr;
|
2017-07-23 01:15:01 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2017-09-25 02:12:30 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2018-01-20 22:03:23 +00:00
|
|
|
use Pterodactyl\Traits\Services\ReturnsUpdatedModels;
|
2020-12-06 20:16:12 +00:00
|
|
|
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
|
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
2017-07-23 01:15:01 +00:00
|
|
|
|
|
|
|
class DetailsModificationService
|
|
|
|
{
|
2018-01-20 22:03:23 +00:00
|
|
|
use ReturnsUpdatedModels;
|
2017-07-23 01:15:01 +00:00
|
|
|
|
|
|
|
/**
|
2018-01-20 22:03:23 +00:00
|
|
|
* @var \Illuminate\Database\ConnectionInterface
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
2018-01-20 22:03:23 +00:00
|
|
|
private $connection;
|
2017-07-23 01:15:01 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-06 20:16:12 +00:00
|
|
|
* @var \Pterodactyl\Repositories\Wings\DaemonServerRepository
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
2020-12-06 20:16:12 +00:00
|
|
|
private $serverRepository;
|
2017-07-24 00:57:43 +00:00
|
|
|
|
2017-07-23 01:15:01 +00:00
|
|
|
/**
|
|
|
|
* DetailsModificationService constructor.
|
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Illuminate\Database\ConnectionInterface $connection
|
2020-12-06 20:16:12 +00:00
|
|
|
* @param \Pterodactyl\Repositories\Wings\DaemonServerRepository $serverRepository
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
2020-12-06 20:16:12 +00:00
|
|
|
public function __construct(ConnectionInterface $connection, DaemonServerRepository $serverRepository)
|
|
|
|
{
|
2017-09-25 02:12:30 +00:00
|
|
|
$this->connection = $connection;
|
2020-12-06 20:16:12 +00:00
|
|
|
$this->serverRepository = $serverRepository;
|
2017-07-23 01:15:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the details for a single server instance.
|
|
|
|
*
|
2018-01-20 22:03:23 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param array $data
|
2020-12-06 20:16:12 +00:00
|
|
|
* @return \Pterodactyl\Models\Server
|
2017-07-23 01:15:01 +00:00
|
|
|
*
|
2020-12-06 20:16:12 +00:00
|
|
|
* @throws \Throwable
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
2020-12-06 20:16:12 +00:00
|
|
|
public function handle(Server $server, array $data): Server
|
2017-07-23 01:15:01 +00:00
|
|
|
{
|
2020-12-06 20:16:12 +00:00
|
|
|
return $this->connection->transaction(function () use ($data, $server) {
|
|
|
|
$owner = $server->owner_id;
|
2018-01-20 22:03:23 +00:00
|
|
|
|
2020-12-06 20:16:12 +00:00
|
|
|
$server->forceFill([
|
|
|
|
'external_id' => Arr::get($data, 'external_id'),
|
|
|
|
'owner_id' => Arr::get($data, 'owner_id'),
|
|
|
|
'name' => Arr::get($data, 'name'),
|
|
|
|
'description' => Arr::get($data, 'description') ?? '',
|
|
|
|
])->saveOrFail();
|
2017-07-23 01:15:01 +00:00
|
|
|
|
2020-12-06 20:16:12 +00:00
|
|
|
// If the owner_id value is changed we need to revoke any tokens that exist for the server
|
|
|
|
// on the Wings instance so that the old owner no longer has any permission to access the
|
|
|
|
// websockets.
|
|
|
|
if ($server->owner_id !== $owner) {
|
|
|
|
try {
|
|
|
|
$this->serverRepository->setServer($server)->revokeUserJTI($owner);
|
|
|
|
} catch (DaemonConnectionException $exception) {
|
|
|
|
// Do nothing. A failure here is not ideal, but it is likely to be caused by Wings
|
|
|
|
// being offline, or in an entirely broken state. Remeber, these tokens reset every
|
|
|
|
// few minutes by default, we're just trying to help it along a little quicker.
|
|
|
|
}
|
|
|
|
}
|
2017-07-23 01:15:01 +00:00
|
|
|
|
2020-12-06 20:16:12 +00:00
|
|
|
return $server;
|
|
|
|
});
|
2017-07-23 01:15:01 +00:00
|
|
|
}
|
|
|
|
}
|