Updates to service updating - broadband

This commit is contained in:
Deon George
2022-08-01 20:34:10 +10:00
parent de3f1a534b
commit 7feec266b8
11 changed files with 234 additions and 73 deletions

View File

@@ -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;

View File

@@ -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 */
/**

View File

@@ -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
*

43
app/Rules/IPv4_CIDR.php Normal file
View File

@@ -0,0 +1,43 @@
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class IPv4_CIDR implements Rule
{
private const MAX=32;
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
if (str_contains($value,'/')) {
list($value,$mask) = explode('/',$value);
$m = filter_var($mask,\FILTER_VALIDATE_INT,[
'options' => [
'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.';
}
}

43
app/Rules/IPv6_CIDR.php Normal file
View File

@@ -0,0 +1,43 @@
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class IPv6_CIDR implements Rule
{
private const MAX=128;
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
if (str_contains($value,':') && str_contains($value,'/')) {
list($value,$mask) = explode('/',$value);
$m = filter_var($mask,\FILTER_VALIDATE_INT,[
'options' => [
'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.';
}
}