clrghouz/app/Http/Controllers/ZoneController.php

130 lines
3.6 KiB
PHP
Raw Permalink 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\{Address,Zone};
2021-05-13 12:40:21 +00:00
class ZoneController extends Controller
{
/**
* Add or edit a zone
2021-05-13 12:40:21 +00:00
*/
public function add_edit(Request $request,Zone $o)
{
if ($request->post()) {
$this->authorize('admin',$o);
$request->validate([
'domain_id' => 'required|exists:domains,id',
2021-08-16 12:26:33 +00:00
'default' => [
'required',
'boolean',
Rule::unique('zones')->where(function ($query) use ($request,$o) {
return $query->where('zone_id',$request->post('zone_id'))
->where('default',TRUE)
2023-06-27 07:39:11 +00:00
->where('id','<>',$o->id);
2021-08-16 12:26:33 +00:00
})
],
'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',
]);
2021-08-16 12:26:33 +00:00
foreach (['zone_id','default','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();
$zo = Zone::where('zone_id',$request->zone_id)
->where('domain_id',$request->domain_id)
->sole();
2021-05-13 12:40:21 +00:00
// Find the zones 0/0 address, and assign it to this host.
$ao = Address::where('zone_id',$zo->id)
->where('region_id',0)
->where('host_id',0)
->where('node_id',0)
->where('point_id',0)
->single();
// Its not defined, so we'll create it.
if (! $ao) {
$ao = new Address;
$ao->forceFill([
'zone_id'=>$zo->id,
'region_id'=>0,
'host_id'=>0,
'node_id'=>0,
'point_id'=>0,
'role'=>Address::NODE_ZC,
]);
}
$ao->system_id = $request->system_id;
$ao->active = TRUE;
$o->addresses()->save($ao);
return redirect()->to('zone');
2021-05-13 12:40:21 +00:00
}
return view('zone.addedit')
->with('o',$o);
}
2021-08-09 13:35:22 +00:00
/**
* Set address as default for zone
*
* @param Request $request
* @param Zone $o
* @throws \Illuminate\Auth\Access\AuthorizationException
2021-08-09 13:35:22 +00:00
*/
public function api_default(Request $request,Zone $o)
{
$this->authorize('admin',$o);
$default = $o->systems->where('pivot.default',TRUE);
if ($default->count() && $default->first()->addresses->pluck('role')->search(Address::NODE_ZC) !== FALSE)
abort(412);
2021-08-09 13:35:22 +00:00
if ($default->count() && ($default->first()->id != $request->sid))
abort(409);
2021-08-09 13:35:22 +00:00
$o->systems()->updateExistingPivot($request->sid,[
'default' => (bool)$request->set,
]);
}
2021-05-13 12:40:21 +00:00
}