From 61c689d80785fb2a92266c87bf7ef9f6c163b3ce Mon Sep 17 00:00:00 2001 From: Andrew Nagy <564256+tm1000@users.noreply.github.com> Date: Sat, 5 Apr 2025 04:58:15 -0700 Subject: [PATCH] fix(api): Add back validateDataApplications (#5539) --- .../Api/ApplicationsController.php | 134 +++++++++--------- 1 file changed, 67 insertions(+), 67 deletions(-) diff --git a/app/Http/Controllers/Api/ApplicationsController.php b/app/Http/Controllers/Api/ApplicationsController.php index 0044e86b4..45968b6c6 100644 --- a/app/Http/Controllers/Api/ApplicationsController.php +++ b/app/Http/Controllers/Api/ApplicationsController.php @@ -3006,73 +3006,73 @@ class ApplicationsController extends Controller // ]); // } - // private function validateDataApplications(Request $request, Server $server) - // { - // $teamId = getTeamIdFromToken(); + private function validateDataApplications(Request $request, Server $server) + { + $teamId = getTeamIdFromToken(); - // // Validate ports_mappings - // if ($request->has('ports_mappings')) { - // $ports = []; - // foreach (explode(',', $request->ports_mappings) as $portMapping) { - // $port = explode(':', $portMapping); - // if (in_array($port[0], $ports)) { - // return response()->json([ - // 'message' => 'Validation failed.', - // 'errors' => [ - // 'ports_mappings' => 'The first number before : should be unique between mappings.', - // ], - // ], 422); - // } - // $ports[] = $port[0]; - // } - // } - // // Validate custom_labels - // if ($request->has('custom_labels')) { - // if (! isBase64Encoded($request->custom_labels)) { - // return response()->json([ - // 'message' => 'Validation failed.', - // 'errors' => [ - // 'custom_labels' => 'The custom_labels should be base64 encoded.', - // ], - // ], 422); - // } - // $customLabels = base64_decode($request->custom_labels); - // if (mb_detect_encoding($customLabels, 'ASCII', true) === false) { - // return response()->json([ - // 'message' => 'Validation failed.', - // 'errors' => [ - // 'custom_labels' => 'The custom_labels should be base64 encoded.', - // ], - // ], 422); - // } - // } - // if ($request->has('domains') && $server->isProxyShouldRun()) { - // $uuid = $request->uuid; - // $fqdn = $request->domains; - // $fqdn = str($fqdn)->replaceEnd(',', '')->trim(); - // $fqdn = str($fqdn)->replaceStart(',', '')->trim(); - // $errors = []; - // $fqdn = str($fqdn)->trim()->explode(',')->map(function ($domain) use (&$errors) { - // if (filter_var($domain, FILTER_VALIDATE_URL) === false) { - // $errors[] = 'Invalid domain: '.$domain; - // } + // Validate ports_mappings + if ($request->has('ports_mappings')) { + $ports = []; + foreach (explode(',', $request->ports_mappings) as $portMapping) { + $port = explode(':', $portMapping); + if (in_array($port[0], $ports)) { + return response()->json([ + 'message' => 'Validation failed.', + 'errors' => [ + 'ports_mappings' => 'The first number before : should be unique between mappings.', + ], + ], 422); + } + $ports[] = $port[0]; + } + } + // Validate custom_labels + if ($request->has('custom_labels')) { + if (! isBase64Encoded($request->custom_labels)) { + return response()->json([ + 'message' => 'Validation failed.', + 'errors' => [ + 'custom_labels' => 'The custom_labels should be base64 encoded.', + ], + ], 422); + } + $customLabels = base64_decode($request->custom_labels); + if (mb_detect_encoding($customLabels, 'ASCII', true) === false) { + return response()->json([ + 'message' => 'Validation failed.', + 'errors' => [ + 'custom_labels' => 'The custom_labels should be base64 encoded.', + ], + ], 422); + } + } + if ($request->has('domains') && $server->isProxyShouldRun()) { + $uuid = $request->uuid; + $fqdn = $request->domains; + $fqdn = str($fqdn)->replaceEnd(',', '')->trim(); + $fqdn = str($fqdn)->replaceStart(',', '')->trim(); + $errors = []; + $fqdn = str($fqdn)->trim()->explode(',')->map(function ($domain) use (&$errors) { + if (filter_var($domain, FILTER_VALIDATE_URL) === false) { + $errors[] = 'Invalid domain: '.$domain; + } - // return str($domain)->trim()->lower(); - // }); - // if (count($errors) > 0) { - // return response()->json([ - // 'message' => 'Validation failed.', - // 'errors' => $errors, - // ], 422); - // } - // if (checkIfDomainIsAlreadyUsed($fqdn, $teamId, $uuid)) { - // return response()->json([ - // 'message' => 'Validation failed.', - // 'errors' => [ - // 'domains' => 'One of the domain is already used.', - // ], - // ], 422); - // } - // } - // } + return str($domain)->trim()->lower(); + }); + if (count($errors) > 0) { + return response()->json([ + 'message' => 'Validation failed.', + 'errors' => $errors, + ], 422); + } + if (checkIfDomainIsAlreadyUsed($fqdn, $teamId, $uuid)) { + return response()->json([ + 'message' => 'Validation failed.', + 'errors' => [ + 'domains' => 'One of the domain is already used.', + ], + ], 422); + } + } + } }