clrghouz/app/Http/Controllers/ZoneController.php

79 lines
2.2 KiB
PHP
Raw Normal View History

2021-05-13 12:40:21 +00:00
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
2021-05-13 12:40:21 +00:00
use App\Models\Zone;
class ZoneController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
/**
* Add or edit a node
*/
public function add_edit(Request $request,Zone $o)
{
if ($request->post()) {
$this->authorize('admin',$o);
$request->validate([
'domain_id' => 'required|exists:domains,id',
'zone_id' => [
'required',
'digits_between:1,5',
Rule::unique('zones')->where(function ($query) use ($request,$o) {
return $query->where('domain_id',$request->post('domain_id'))
->when($o->exists,function($query) use ($o) {
return $query->where('id','<>',$o->id);
});
})
],
'system_id' => 'required|exists:systems,id',
'zt_id' => 'nullable|size:16|regex:/^([A-Fa-f0-9]){16}$/|unique:zones,zt_id,'.($o->exists ? $o->id : 0),
'zt_ipv4' => 'nullable|ipv4',
'zt_ipv4_mask' => [
'nullable','numeric','lte:31','required_with:zt_ipv4',
Rule::unique('zones')->where(function ($query) use ($request,$o) {
return $query->where('zt_ipv4',$request->post('zt_ipv4'))
->where('zt_ipv4_mask',$request->post('zt_ipv4_mask'))
->when($o->exists,function($query) use ($o) {
return $query->where('id','<>',$o->id);
});
})],
'zt_ipv6' => 'nullable|ipv6',
'zt_ipv6_mask' => [
'nullable','numeric','lte:112','required_with:zt_ipv6',
Rule::unique('zones')->where(function ($query) use ($request,$o) {
return $query->where('zt_ipv6',$request->post('zt_ipv6'))
->where('zt_ipv6_mask',$request->post('zt_ipv6_mask'))
->when($o->exists,function($query) use ($o) {
return $query->where('id','<>',$o->id);
});
})],
'active' => 'required|boolean',
]);
foreach (['zone_id','domain_id','system_id','active','notes','zt_id','zt_ipv4','zt_ipv4_mask','zt_ipv6','zt_ipv6_mask'] as $key)
2021-05-13 12:40:21 +00:00
$o->{$key} = $request->post($key);
$o->save();
return redirect()->action([self::class,'home']);
}
return view('zone.addedit')
->with('o',$o);
}
public function home()
{
return view('zone.home');
}
}