From 0ab1ac03e81fb59c95bf9094a4164a36608c714d Mon Sep 17 00:00:00 2001 From: Michal Dziekonski Date: Thu, 10 Dec 2020 20:57:22 +0100 Subject: [PATCH] GH-139 Create union join validator --- modules/flightControl/_includes.php | 1 + .../utils/validators/joinUnion.validator.php | 80 +++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 modules/flightControl/utils/validators/joinUnion.validator.php diff --git a/modules/flightControl/_includes.php b/modules/flightControl/_includes.php index aefe3773f..921afa83c 100644 --- a/modules/flightControl/_includes.php +++ b/modules/flightControl/_includes.php @@ -9,6 +9,7 @@ include($includePath . './utils/helpers/getAvailableSpeeds.helper.php'); include($includePath . './utils/helpers/getFleetUnionJoinData.helper.php'); include($includePath . './utils/validators/fleetArray.validator.php'); + include($includePath . './utils/validators/joinUnion.validator.php'); }); diff --git a/modules/flightControl/utils/validators/joinUnion.validator.php b/modules/flightControl/utils/validators/joinUnion.validator.php new file mode 100644 index 000000000..0723700b3 --- /dev/null +++ b/modules/flightControl/utils/validators/joinUnion.validator.php @@ -0,0 +1,80 @@ + true, + ]; + }; + $isInvalid = function ($errors) { + return [ + 'isValid' => false, + 'errors' => $errors, + ]; + }; + + $newFleet = $props['newFleet']; + $timestamp = $props['timestamp']; + $user = &$props['user']; + $destinationEntry = &$props['destinationEntry']; + + if (!($newFleet['ACS_ID'] > 0)) { + return $isInvalid([ + [ 'errorCode' => 'INVALID_UNION_ID', ], + ]); + } + + $unionJoinData = FlightControl\Utils\Helpers\getFleetUnionJoinData([ + 'newFleet' => $newFleet, + ]); + + if (!($unionJoinData)) { + return $isInvalid([ + [ 'errorCode' => 'UNION_NOT_FOUND', ], + ]); + } + + if ( + !( + $unionJoinData['owner_id'] == $user['id'] || + strstr($unionJoinData['users'], "|{$user['id']}|") !== false + ) + ) { + return $isInvalid([ + [ 'errorCode' => 'USER_CANT_JOIN', ], + ]); + } + + if (!($unionJoinData['end_target_id'] == $destinationEntry['id'])) { + return $isInvalid([ + [ 'errorCode' => 'INVALID_DESTINATION_COORDINATES', ], + ]); + } + + if (!($unionJoinData['fleets_count'] < ACS_MAX_JOINED_FLEETS)) { + return $isInvalid([ + [ 'errorCode' => 'UNION_JOINED_FLEETS_COUNT_EXCEEDED', ], + ]); + } + + if (!($unionJoinData['start_time'] > $timestamp)) { + return $isInvalid([ + [ 'errorCode' => 'UNION_JOIN_TIME_EXCEEDED', ], + ]); + } + + return $isValid(); +} + +?>