22
33namespace Pterodactyl \Services \Servers ;
44
5+ use Illuminate \Support \Arr ;
56use Pterodactyl \Models \Server ;
67use Illuminate \Database \ConnectionInterface ;
78use Pterodactyl \Traits \Services \ReturnsUpdatedModels ;
8- use Pterodactyl \Repositories \Eloquent \ServerRepository ;
9+ use Pterodactyl \Repositories \Wings \DaemonServerRepository ;
10+ use Pterodactyl \Exceptions \Http \Connection \DaemonConnectionException ;
911
1012class DetailsModificationService
1113{
@@ -17,47 +19,57 @@ class DetailsModificationService
1719 private $ connection ;
1820
1921 /**
20- * @var \Pterodactyl\Repositories\Eloquent\ServerRepository
22+ * @var \Pterodactyl\Repositories\Wings\DaemonServerRepository
2123 */
22- private $ repository ;
24+ private $ serverRepository ;
2325
2426 /**
2527 * DetailsModificationService constructor.
2628 *
2729 * @param \Illuminate\Database\ConnectionInterface $connection
28- * @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
30+ * @param \Pterodactyl\Repositories\Wings\DaemonServerRepository $serverRepository
2931 */
30- public function __construct (
31- ConnectionInterface $ connection ,
32- ServerRepository $ repository
33- ) {
32+ public function __construct (ConnectionInterface $ connection , DaemonServerRepository $ serverRepository )
33+ {
3434 $ this ->connection = $ connection ;
35- $ this ->repository = $ repository ;
35+ $ this ->serverRepository = $ serverRepository ;
3636 }
3737
3838 /**
3939 * Update the details for a single server instance.
4040 *
4141 * @param \Pterodactyl\Models\Server $server
4242 * @param array $data
43- * @return bool| \Pterodactyl\Models\Server
43+ * @return \Pterodactyl\Models\Server
4444 *
45- * @throws \Pterodactyl\Exceptions\Model\DataValidationException
46- * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
45+ * @throws \Throwable
4746 */
48- public function handle (Server $ server , array $ data )
47+ public function handle (Server $ server , array $ data ): Server
4948 {
50- $ this ->connection ->beginTransaction ();
49+ return $ this ->connection ->transaction (function () use ($ data , $ server ) {
50+ $ owner = $ server ->owner_id ;
5151
52- $ response = $ this -> repository -> setFreshModel ( $ this -> getUpdatedModel ())-> update ( $ server ->id , [
53- 'external_id ' => array_get ($ data , 'external_id ' ),
54- 'owner_id ' => array_get ($ data , 'owner_id ' ),
55- 'name ' => array_get ($ data , 'name ' ),
56- 'description ' => array_get ($ data , 'description ' ) ?? '' ,
57- ], true , true );
52+ $ server ->forceFill ( [
53+ 'external_id ' => Arr:: get ($ data , 'external_id ' ),
54+ 'owner_id ' => Arr:: get ($ data , 'owner_id ' ),
55+ 'name ' => Arr:: get ($ data , 'name ' ),
56+ 'description ' => Arr:: get ($ data , 'description ' ) ?? '' ,
57+ ])-> saveOrFail ( );
5858
59- $ this ->connection ->commit ();
59+ // If the owner_id value is changed we need to revoke any tokens that exist for the server
60+ // on the Wings instance so that the old owner no longer has any permission to access the
61+ // websockets.
62+ if ($ server ->owner_id !== $ owner ) {
63+ try {
64+ $ this ->serverRepository ->setServer ($ server )->revokeUserJTI ($ owner );
65+ } catch (DaemonConnectionException $ exception ) {
66+ // Do nothing. A failure here is not ideal, but it is likely to be caused by Wings
67+ // being offline, or in an entirely broken state. Remeber, these tokens reset every
68+ // few minutes by default, we're just trying to help it along a little quicker.
69+ }
70+ }
6071
61- return $ response ;
72+ return $ server ;
73+ });
6274 }
6375}
0 commit comments