From 7feec266b8cb0bd0215a2fbbb78f8ee1e1345a83 Mon Sep 17 00:00:00 2001 From: Deon George Date: Mon, 1 Aug 2022 20:34:10 +1000 Subject: [PATCH] Updates to service updating - broadband --- app/Http/Controllers/ServiceController.php | 22 ++- app/Models/Service/Broadband.php | 22 +++ app/Models/Service/Type.php | 14 +- app/Rules/IPv4_CIDR.php | 43 ++++++ app/Rules/IPv6_CIDR.php | 43 ++++++ composer.json | 2 +- composer.lock | 8 +- .../backend/adminlte/service/home.blade.php | 5 +- .../service/widget/broadband/update.blade.php | 142 +++++++++++------- .../adminlte/service/widget/update.blade.php | 4 +- routes/web.php | 2 +- 11 files changed, 234 insertions(+), 73 deletions(-) create mode 100644 app/Rules/IPv4_CIDR.php create mode 100644 app/Rules/IPv6_CIDR.php diff --git a/app/Http/Controllers/ServiceController.php b/app/Http/Controllers/ServiceController.php index dda90a7..a38152d 100644 --- a/app/Http/Controllers/ServiceController.php +++ b/app/Http/Controllers/ServiceController.php @@ -10,6 +10,7 @@ use Illuminate\Support\Collection; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Mail; +use Illuminate\Support\Facades\Validator; use Illuminate\View\View; use Symfony\Component\HttpKernel\Exception\HttpException; @@ -409,15 +410,28 @@ class ServiceController extends Controller * @param Request $request * @param Service $o * @return RedirectResponse - * @todo This needs to be reworked, to take into account our different service types - * @todo Add Validation */ public function update(Request $request,Service $o) { - if ($request->post($o->product->category)) { - $o->type->forceFill($request->post($o->product->category))->save(); + if ($o->type->validation()) { + $validator = Validator::make($x=$request->post($o->category),$o->type->validation()); + + if ($validator->fails()) { + return redirect() + ->back() + ->withErrors($validator) + ->withInput(); + } + + $o->type->forceFill(Arr::except($x,['start_at'])); + + } elseif ($request->post($o->product->category)) { + $o->type->forceFill($request->post($o->product->category)); } + $o->type->save(); + + // Also update our service start_at date. if ($request->post('start_at')) $o->start_at = $request->start_at; diff --git a/app/Models/Service/Broadband.php b/app/Models/Service/Broadband.php index 2931d1f..75df9b7 100644 --- a/app/Models/Service/Broadband.php +++ b/app/Models/Service/Broadband.php @@ -10,6 +10,7 @@ use App\Interfaces\ServiceUsage; use App\Models\Supplier\Broadband as SupplierBroadband; use App\Models\Supplier\Type as SupplierType; use App\Models\Usage\Broadband as UsageBroadband; +use App\Rules\IPv6_CIDR; /** * Class Broadband (Service) @@ -81,6 +82,27 @@ class Broadband extends Type implements ServiceUsage return $value ?: $this->supplied()->technology; } + /* OVERRIDES */ + + /** + * Service update validation + * + * @return array + */ + public function validation(): array + { + return [ + 'service_number' => 'nullable|string|min:10|max:10', + 'service_username' => 'nullable|string', + 'service_password' => 'nullable|string', + 'connect_at' => 'nullable|date', + 'start_at' => 'nullable|date', + 'expire_at' => 'nullable|date|after:start_at', + 'ipaddress' => 'nullable|ipv4', + 'ip6address' => ['nullable',new IPv6_CIDR], + ]; + } + /* METHODS */ /** diff --git a/app/Models/Service/Type.php b/app/Models/Service/Type.php index 7e70bef..5b8c847 100644 --- a/app/Models/Service/Type.php +++ b/app/Models/Service/Type.php @@ -69,13 +69,23 @@ abstract class Type extends Model implements ServiceItem * @param $value * @return LeenooksCarbon */ - public function getExpireAtAttribute($value): LeenooksCarbon + public function getExpireAtAttribute($value): ?LeenooksCarbon { - return LeenooksCarbon::create($value); + return $value ? LeenooksCarbon::create($value) : NULL; } /* METHODS */ + /** + * Validation used to accept form input + * + * @return mixed + */ + public function validation(): array + { + return []; + } + /** * The supplier's service that we provide * diff --git a/app/Rules/IPv4_CIDR.php b/app/Rules/IPv4_CIDR.php new file mode 100644 index 0000000..8d2107a --- /dev/null +++ b/app/Rules/IPv4_CIDR.php @@ -0,0 +1,43 @@ + [ + 'min_range' => 0, + "max_range" => self::MAX, + ]] + ); + + return $m && filter_var($value,\FILTER_VALIDATE_IP,\FILTER_FLAG_IPV4); + } + } + + /** + * Get the validation error message. + * + * @return string + */ + public function message() + { + return 'The :attribute must be a valid IPv4 CIDR range.'; + } +} diff --git a/app/Rules/IPv6_CIDR.php b/app/Rules/IPv6_CIDR.php new file mode 100644 index 0000000..2f1160d --- /dev/null +++ b/app/Rules/IPv6_CIDR.php @@ -0,0 +1,43 @@ + [ + 'min_range' => 0, + "max_range" => self::MAX, + ] + ]); + + return $m && filter_var($value,\FILTER_VALIDATE_IP,\FILTER_FLAG_IPV6); + } + } + + /** + * Get the validation error message. + * + * @return string + */ + public function message() + { + return 'The :attribute must be a valid IPv6 CIDR range.'; + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index 4b3689b..e9cc5ac 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "laravel/passport": "^10.1", "laravel/socialite": "^5.2", "laravel/ui": "^3.2", - "leenooks/laravel": "^9.2.0", + "leenooks/laravel": "^9.2.2", "leenooks/laravel-theme": "^v2.0.18", "nunomaduro/laravel-console-summary": "^1.8", "paypal/paypal-checkout-sdk": "^1.0", diff --git a/composer.lock b/composer.lock index 87d1d3e..5b2275f 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "c41aad3374358724adae9239e88f7a4f", + "content-hash": "3306c0a2c799a142669bda57f369316a", "packages": [ { "name": "asm89/stack-cors", @@ -3295,11 +3295,11 @@ }, { "name": "leenooks/laravel", - "version": "9.2.1", + "version": "9.2.2", "source": { "type": "git", "url": "https://dev.leenooks.net/leenooks/laravel", - "reference": "9ef7e8e626a9c84bc385350b7261368abcabe16b" + "reference": "2e512cc3edc913e59726a93c248e76705ff44daa" }, "require": { "creativeorange/gravatar": "^1.0", @@ -3338,7 +3338,7 @@ "laravel", "leenooks" ], - "time": "2022-08-01T03:59:01+00:00" + "time": "2022-08-01T10:29:57+00:00" }, { "name": "leenooks/laravel-theme", diff --git a/resources/views/theme/backend/adminlte/service/home.blade.php b/resources/views/theme/backend/adminlte/service/home.blade.php index d0d8f40..ba87fc7 100644 --- a/resources/views/theme/backend/adminlte/service/home.blade.php +++ b/resources/views/theme/backend/adminlte/service/home.blade.php @@ -16,14 +16,13 @@ @section('main-content')
- -
+
@includeIf('service.widget.'.$o->product->category.'.details',['o'=>$o->type]) @include('service.widget.information')
-
+