68 lines
2.3 KiB
PHP
68 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace Pterodactyl\Services\Servers;
|
|
|
|
use Illuminate\Support\Arr;
|
|
use Pterodactyl\Models\Server;
|
|
use Illuminate\Database\ConnectionInterface;
|
|
use Pterodactyl\Traits\Services\ReturnsUpdatedModels;
|
|
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
|
|
|
class DetailsModificationService
|
|
{
|
|
use ReturnsUpdatedModels;
|
|
|
|
/**
|
|
* @var \Illuminate\Database\ConnectionInterface
|
|
*/
|
|
private $connection;
|
|
|
|
/**
|
|
* @var \Pterodactyl\Repositories\Wings\DaemonServerRepository
|
|
*/
|
|
private $serverRepository;
|
|
|
|
/**
|
|
* DetailsModificationService constructor.
|
|
*/
|
|
public function __construct(ConnectionInterface $connection, DaemonServerRepository $serverRepository)
|
|
{
|
|
$this->connection = $connection;
|
|
$this->serverRepository = $serverRepository;
|
|
}
|
|
|
|
/**
|
|
* Update the details for a single server instance.
|
|
*
|
|
* @throws \Throwable
|
|
*/
|
|
public function handle(Server $server, array $data): Server
|
|
{
|
|
return $this->connection->transaction(function () use ($data, $server) {
|
|
$owner = $server->owner_id;
|
|
|
|
$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();
|
|
|
|
// 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.
|
|
}
|
|
}
|
|
|
|
return $server;
|
|
});
|
|
}
|
|
}
|