2021-05-13 12:40:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
2021-06-18 13:01:41 +00:00
|
|
|
use Illuminate\Validation\Rule;
|
2021-05-13 12:40:21 +00:00
|
|
|
|
2021-07-26 11:21:58 +00:00
|
|
|
use App\Models\{Address,Zone};
|
2021-05-13 12:40:21 +00:00
|
|
|
|
|
|
|
class ZoneController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
2024-05-29 08:59:44 +00:00
|
|
|
* Add or edit a zone
|
2021-05-13 12:40:21 +00:00
|
|
|
*/
|
|
|
|
public function add_edit(Request $request,Zone $o)
|
|
|
|
{
|
|
|
|
if ($request->post()) {
|
2021-06-18 13:01:41 +00:00
|
|
|
$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
|
|
|
})
|
|
|
|
],
|
2021-06-18 13:01:41 +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',
|
2021-06-26 15:15:19 +00:00
|
|
|
'zt_id' => 'nullable|size:16|regex:/^([A-Fa-f0-9]){16}$/|unique:zones,zt_id,'.($o->exists ? $o->id : 0),
|
2021-06-24 12:28:06 +00:00
|
|
|
'zt_ipv4' => 'nullable|ipv4',
|
2021-06-26 15:15:19 +00:00
|
|
|
'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);
|
|
|
|
});
|
|
|
|
})],
|
2021-06-24 12:28:06 +00:00
|
|
|
'zt_ipv6' => 'nullable|ipv6',
|
2021-06-26 15:15:19 +00:00
|
|
|
'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);
|
|
|
|
});
|
|
|
|
})],
|
2021-06-18 13:01:41 +00:00
|
|
|
'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();
|
2021-08-14 06:14:22 +00:00
|
|
|
$zo = Zone::where('zone_id',$request->zone_id)
|
|
|
|
->where('domain_id',$request->domain_id)
|
2024-11-08 12:31:21 +00:00
|
|
|
->sole();
|
2021-05-13 12:40:21 +00:00
|
|
|
|
2021-07-26 11:21:58 +00:00
|
|
|
// Find the zones 0/0 address, and assign it to this host.
|
2021-08-14 06:14:22 +00:00
|
|
|
$ao = Address::where('zone_id',$zo->id)
|
2021-07-26 11:21:58 +00:00
|
|
|
->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([
|
2021-08-14 06:14:22 +00:00
|
|
|
'zone_id'=>$zo->id,
|
2021-07-26 11:21:58 +00:00
|
|
|
'region_id'=>0,
|
|
|
|
'host_id'=>0,
|
|
|
|
'node_id'=>0,
|
|
|
|
'point_id'=>0,
|
2022-01-24 11:56:13 +00:00
|
|
|
'role'=>Address::NODE_ZC,
|
2021-07-26 11:21:58 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$ao->system_id = $request->system_id;
|
|
|
|
$ao->active = TRUE;
|
|
|
|
$o->addresses()->save($ao);
|
|
|
|
|
2023-10-05 12:28:18 +00:00
|
|
|
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
|
2021-08-13 12:53:59 +00:00
|
|
|
* @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);
|
2021-08-13 12:53:59 +00:00
|
|
|
|
2022-01-24 11:56:13 +00:00
|
|
|
if ($default->count() && $default->first()->addresses->pluck('role')->search(Address::NODE_ZC) !== FALSE)
|
2021-08-13 12:53:59 +00:00
|
|
|
abort(412);
|
|
|
|
|
2021-08-09 13:35:22 +00:00
|
|
|
if ($default->count() && ($default->first()->id != $request->sid))
|
2021-08-13 12:53:59 +00:00
|
|
|
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
|
|
|
}
|