From 7d81fa624757f6e36c6556ced98535c14441bb5b Mon Sep 17 00:00:00 2001 From: IceToast <> Date: Thu, 19 Jan 2023 11:18:51 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20Added=20Server=20owner=20tr?= =?UTF-8?q?ansfer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Classes/Pterodactyl.php | 54 +++++++++------ .../Controllers/Admin/ServerController.php | 68 +++++++++++++------ .../views/admin/servers/edit.blade.php | 46 +++++++++---- 3 files changed, 114 insertions(+), 54 deletions(-) diff --git a/app/Classes/Pterodactyl.php b/app/Classes/Pterodactyl.php index 528a2c4b..89ae028c 100644 --- a/app/Classes/Pterodactyl.php +++ b/app/Classes/Pterodactyl.php @@ -7,6 +7,7 @@ use App\Models\Nest; use App\Models\Node; use App\Models\Product; use App\Models\Server; +use App\Models\User; use Exception; use Illuminate\Http\Client\PendingRequest; use Illuminate\Http\Client\Response; @@ -22,19 +23,19 @@ class Pterodactyl public static function client() { return Http::withHeaders([ - 'Authorization' => 'Bearer '.config('SETTINGS::SYSTEM:PTERODACTYL:TOKEN'), + 'Authorization' => 'Bearer ' . config('SETTINGS::SYSTEM:PTERODACTYL:TOKEN'), 'Content-type' => 'application/json', 'Accept' => 'Application/vnd.pterodactyl.v1+json', - ])->baseUrl(config('SETTINGS::SYSTEM:PTERODACTYL:URL').'/api'); + ])->baseUrl(config('SETTINGS::SYSTEM:PTERODACTYL:URL') . '/api'); } public static function clientAdmin() { return Http::withHeaders([ - 'Authorization' => 'Bearer '.config('SETTINGS::SYSTEM:PTERODACTYL:ADMIN_USER_TOKEN'), + 'Authorization' => 'Bearer ' . config('SETTINGS::SYSTEM:PTERODACTYL:ADMIN_USER_TOKEN'), 'Content-type' => 'application/json', 'Accept' => 'Application/vnd.pterodactyl.v1+json', - ])->baseUrl(config('SETTINGS::SYSTEM:PTERODACTYL:URL').'/api'); + ])->baseUrl(config('SETTINGS::SYSTEM:PTERODACTYL:URL') . '/api'); } /** @@ -43,22 +44,22 @@ class Pterodactyl private static function getException(string $message = '', int $status = 0): Exception { if ($status == 404) { - return new Exception('Ressource does not exist on pterodactyl - '.$message, 404); + return new Exception('Ressource does not exist on pterodactyl - ' . $message, 404); } if ($status == 403) { - return new Exception('No permission on pterodactyl, check pterodactyl token and permissions - '.$message, 403); + return new Exception('No permission on pterodactyl, check pterodactyl token and permissions - ' . $message, 403); } if ($status == 401) { - return new Exception('No pterodactyl token set - '.$message, 401); + return new Exception('No pterodactyl token set - ' . $message, 401); } if ($status == 500) { - return new Exception('Pterodactyl server error - '.$message, 500); + return new Exception('Pterodactyl server error - ' . $message, 500); } - return new Exception('Request Failed, is pterodactyl set-up correctly? - '.$message); + return new Exception('Request Failed, is pterodactyl set-up correctly? - ' . $message); } /** @@ -70,7 +71,7 @@ class Pterodactyl public static function getEggs(Nest $nest) { try { - $response = self::client()->get("/application/nests/{$nest->id}/eggs?include=nest,variables&per_page=".config('SETTINGS::SYSTEM:PTERODACTYL:PER_PAGE_LIMIT')); + $response = self::client()->get("/application/nests/{$nest->id}/eggs?include=nest,variables&per_page=" . config('SETTINGS::SYSTEM:PTERODACTYL:PER_PAGE_LIMIT')); } catch (Exception $e) { throw self::getException($e->getMessage()); } @@ -89,7 +90,7 @@ class Pterodactyl public static function getNodes() { try { - $response = self::client()->get('/application/nodes?per_page='.config('SETTINGS::SYSTEM:PTERODACTYL:PER_PAGE_LIMIT')); + $response = self::client()->get('/application/nodes?per_page=' . config('SETTINGS::SYSTEM:PTERODACTYL:PER_PAGE_LIMIT')); } catch (Exception $e) { throw self::getException($e->getMessage()); } @@ -109,12 +110,12 @@ class Pterodactyl public static function getNode($id) { try { - $response = self::client()->get('/application/nodes/'.$id); + $response = self::client()->get('/application/nodes/' . $id); } catch (Exception $e) { throw self::getException($e->getMessage()); } if ($response->failed()) { - throw self::getException('Failed to get node id '.$id.' - '.$response->status()); + throw self::getException('Failed to get node id ' . $id . ' - ' . $response->status()); } return $response->json()['attributes']; @@ -123,7 +124,7 @@ class Pterodactyl public static function getServers() { try { - $response = self::client()->get('/application/servers?per_page='.config('SETTINGS::SYSTEM:PTERODACTYL:PER_PAGE_LIMIT')); + $response = self::client()->get('/application/servers?per_page=' . config('SETTINGS::SYSTEM:PTERODACTYL:PER_PAGE_LIMIT')); } catch (Exception $e) { throw self::getException($e->getMessage()); } @@ -142,7 +143,7 @@ class Pterodactyl public static function getNests() { try { - $response = self::client()->get('/application/nests?per_page='.config('SETTINGS::SYSTEM:PTERODACTYL:PER_PAGE_LIMIT')); + $response = self::client()->get('/application/nests?per_page=' . config('SETTINGS::SYSTEM:PTERODACTYL:PER_PAGE_LIMIT')); } catch (Exception $e) { throw self::getException($e->getMessage()); } @@ -161,7 +162,7 @@ class Pterodactyl public static function getLocations() { try { - $response = self::client()->get('/application/locations?per_page='.config('SETTINGS::SYSTEM:PTERODACTYL:PER_PAGE_LIMIT')); + $response = self::client()->get('/application/locations?per_page=' . config('SETTINGS::SYSTEM:PTERODACTYL:PER_PAGE_LIMIT')); } catch (Exception $e) { throw self::getException($e->getMessage()); } @@ -195,9 +196,9 @@ class Pterodactyl $freeAllocations = []; if (isset($response['data'])) { - if (! empty($response['data'])) { + if (!empty($response['data'])) { foreach ($response['data'] as $allocation) { - if (! $allocation['attributes']['assigned']) { + if (!$allocation['attributes']['assigned']) { array_push($freeAllocations, $allocation); } } @@ -234,7 +235,7 @@ class Pterodactyl */ public static function url(string $route): string { - return config('SETTINGS::SYSTEM:PTERODACTYL:URL').$route; + return config('SETTINGS::SYSTEM:PTERODACTYL:URL') . $route; } /** @@ -373,6 +374,21 @@ class Pterodactyl ]); } + /** + * Update the owner of a server + * + * @param int $userId + * @param Server $server + * @return mixed + */ + public static function updateServerOwner(Server $server, int $userId) + { + return self::client()->patch("/application/servers/{$server->pterodactyl_id}/details", [ + 'name' => $server->name, + 'user' => $userId, + ]); + } + /** * Power Action Specific Server * diff --git a/app/Http/Controllers/Admin/ServerController.php b/app/Http/Controllers/Admin/ServerController.php index 2be3b51d..06d2cc22 100644 --- a/app/Http/Controllers/Admin/ServerController.php +++ b/app/Http/Controllers/Admin/ServerController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers\Admin; use App\Classes\Pterodactyl; use App\Http\Controllers\Controller; use App\Models\Server; +use App\Models\User; use Exception; use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\View\Factory; @@ -66,8 +67,12 @@ class ServerController extends Controller */ public function edit(Server $server) { + // get all users from the database + $users = User::all(); + return view('admin.servers.edit')->with([ 'server' => $server, + 'users' => $users, ]); } @@ -76,15 +81,36 @@ class ServerController extends Controller * * @param Request $request * @param Server $server - * @return Response */ public function update(Request $request, Server $server) { $request->validate([ 'identifier' => 'required|string', + 'user_id' => 'required|integer', ]); - $server->update($request->all()); + + if ($request->get('user_id') != $server->user_id) { + // find the user + $user = User::findOrFail($request->get('user_id')); + + // try to update the owner on pterodactyl + try { + $response = Pterodactyl::updateServerOwner($server, $user->pterodactyl_id); + if ($response->getStatusCode() != 200) { + return redirect()->back()->with('error', 'Failed to update server owner on pterodactyl'); + } + + // update the owner on the database + $server->user_id = $user->id; + } catch (Exception $e) { + return redirect()->back()->with('error', 'Internal Server Error'); + } + } + + // update the identifier + $server->identifier = $request->get('identifier'); + $server->save(); return redirect()->route('admin.servers.index')->with('success', 'Server updated!'); } @@ -102,7 +128,7 @@ class ServerController extends Controller return redirect()->route('admin.servers.index')->with('success', __('Server removed')); } catch (Exception $e) { - return redirect()->route('admin.servers.index')->with('error', __('An exception has occurred while trying to remove a resource "').$e->getMessage().'"'); + return redirect()->route('admin.servers.index')->with('error', __('An exception has occurred while trying to remove a resource "') . $e->getMessage() . '"'); } } @@ -128,17 +154,17 @@ class ServerController extends Controller $CPIDArray = []; $renameCount = 0; - foreach ($CPServers as $CPServer) {//go thru all CP servers and make array with IDs as keys. All values are false. + foreach ($CPServers as $CPServer) { //go thru all CP servers and make array with IDs as keys. All values are false. if ($CPServer->pterodactyl_id) { $CPIDArray[$CPServer->pterodactyl_id] = false; } } - foreach ($pteroServers as $server) {//go thru all ptero servers, if server exists, change value to true in array. + foreach ($pteroServers as $server) { //go thru all ptero servers, if server exists, change value to true in array. if (isset($CPIDArray[$server['attributes']['id']])) { $CPIDArray[$server['attributes']['id']] = true; - if (isset($server['attributes']['name'])) {//failsafe + if (isset($server['attributes']['name'])) { //failsafe //Check if a server got renamed $savedServer = Server::query()->where('pterodactyl_id', $server['attributes']['id'])->first(); if ($savedServer->name != $server['attributes']['name']) { @@ -150,16 +176,16 @@ class ServerController extends Controller } } $filteredArray = array_filter($CPIDArray, function ($v, $k) { - return $v == false; + return $v == false; }, ARRAY_FILTER_USE_BOTH); //Array of servers, that dont exist on ptero (value == false) $deleteCount = 0; - foreach ($filteredArray as $key => $CPID) {//delete servers that dont exist on ptero anymore - if (! Pterodactyl::getServerAttributes($key, true)) { + foreach ($filteredArray as $key => $CPID) { //delete servers that dont exist on ptero anymore + if (!Pterodactyl::getServerAttributes($key, true)) { $deleteCount++; } } - return redirect()->back()->with('success', __('Servers synced successfully'.(($renameCount) ? (',\n'.__('renamed').' '.$renameCount.' '.__('servers')) : '').((count($filteredArray)) ? (',\n'.__('deleted').' '.$deleteCount.'/'.count($filteredArray).' '.__('old servers')) : ''))).'.'; + return redirect()->back()->with('success', __('Servers synced successfully' . (($renameCount) ? (',\n' . __('renamed') . ' ' . $renameCount . ' ' . __('servers')) : '') . ((count($filteredArray)) ? (',\n' . __('deleted') . ' ' . $deleteCount . '/' . count($filteredArray) . ' ' . __('old servers')) : ''))) . '.'; } /** @@ -180,7 +206,7 @@ class ServerController extends Controller return datatables($query) ->addColumn('user', function (Server $server) { - return ''.$server->user->name.''; + return '' . $server->user->name . ''; }) ->addColumn('resources', function (Server $server) { return $server->product->description; @@ -191,16 +217,16 @@ class ServerController extends Controller $suspendText = $server->isSuspended() ? __('Unsuspend') : __('Suspend'); return ' - -
- '; @@ -208,7 +234,7 @@ class ServerController extends Controller ->addColumn('status', function (Server $server) { $labelColor = $server->isSuspended() ? 'text-danger' : 'text-success'; - return ''; + return ''; }) ->editColumn('created_at', function (Server $server) { return $server->created_at ? $server->created_at->diffForHumans() : ''; @@ -217,7 +243,7 @@ class ServerController extends Controller return $server->suspended ? $server->suspended->diffForHumans() : ''; }) ->editColumn('name', function (Server $server) { - return ''.strip_tags($server->name).''; + return '' . strip_tags($server->name) . ''; }) ->rawColumns(['user', 'actions', 'status', 'name']) ->make(); diff --git a/themes/default/views/admin/servers/edit.blade.php b/themes/default/views/admin/servers/edit.blade.php index 86d51f47..d14474e9 100644 --- a/themes/default/views/admin/servers/edit.blade.php +++ b/themes/default/views/admin/servers/edit.blade.php @@ -6,21 +6,22 @@