Fix/optimise address creation/editing via System AKAs
This commit is contained in:
@@ -16,11 +16,10 @@ use Illuminate\Support\Facades\Notification;
|
||||
use Illuminate\Support\ViewErrorBag;
|
||||
|
||||
use App\Classes\FTN\Message;
|
||||
use App\Http\Requests\{AddressMerge,AreafixRequest,SystemEchoareaRequest,SystemRegisterRequest,SystemSessionRequest};
|
||||
use App\Http\Requests\{AddressAdd,AddressMerge,AreafixRequest,SystemEchoareaRequest,SystemRegisterRequest,SystemSessionRequest};
|
||||
use App\Jobs\AddressPoll;
|
||||
use App\Models\{Address,Echoarea,Echomail,Filearea,Netmail,Setup,System,Zone};
|
||||
use App\Notifications\Netmails\AddressLink;
|
||||
use App\Rules\{FidoInteger,TwoByteInteger};
|
||||
|
||||
class SystemController extends Controller
|
||||
{
|
||||
@@ -74,223 +73,51 @@ class SystemController extends Controller
|
||||
/**
|
||||
* Add an address to a system
|
||||
*
|
||||
* @param Request $request
|
||||
* @param AddressAdd $request
|
||||
* @param System $o
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
*/
|
||||
public function address_add(Request $request,System $o)
|
||||
public function address_add(AddressAdd $request,System $o)
|
||||
{
|
||||
// @todo This should be admin of the zone
|
||||
$this->authorize('admin',$o);
|
||||
session()->flash('accordion','address');
|
||||
$oo = Address::findOrNew($request->validated('submit'));
|
||||
$oo->zone_id = $request->validated('zone_id');
|
||||
$oo->security = $request->validated('security');
|
||||
$oo->active = TRUE;
|
||||
|
||||
$request->validate([
|
||||
'action' => 'required|in:region,host,node,update',
|
||||
'zone_id' => 'required|exists:zones,id',
|
||||
]);
|
||||
|
||||
switch ($request->action) {
|
||||
switch ($request->validated('action')) {
|
||||
case 'region':
|
||||
$request->validate([
|
||||
'region_id_new' => [
|
||||
'required',
|
||||
new TwoByteInteger,
|
||||
function ($attribute,$value,$fail) use ($request) {
|
||||
// Check that the region doesnt already exist
|
||||
$o = Address::where(function($query) use ($value) {
|
||||
return $query->where(function($query) use ($value) {
|
||||
return $query
|
||||
->where('region_id',$value)
|
||||
->where('role',Address::NODE_RC);
|
||||
})
|
||||
// Check that a host doesnt already exist
|
||||
->orWhere(function($query) use ($value) {
|
||||
return $query
|
||||
->where('host_id',$value)
|
||||
->where('role',Address::NODE_NC);
|
||||
});
|
||||
})
|
||||
->where('zone_id',$request->zone_id)
|
||||
->where('node_id',0)
|
||||
->where('point_id',0);
|
||||
|
||||
if ($o->count()) {
|
||||
$fail('Region or host already exists');
|
||||
}
|
||||
},
|
||||
],
|
||||
]);
|
||||
|
||||
$oo = new Address;
|
||||
$oo->zone_id = $request->zone_id;
|
||||
$oo->region_id = $request->region_id_new;
|
||||
$oo->host_id = $request->region_id_new;
|
||||
$oo->region_id = $request->validated('region_id_new');
|
||||
$oo->host_id = $request->validated('region_id_new');
|
||||
$oo->node_id = 0;
|
||||
$oo->point_id = 0;
|
||||
$oo->active = TRUE;
|
||||
|
||||
$o->addresses()->save($oo);
|
||||
break;
|
||||
|
||||
case 'host':
|
||||
$request->validate([
|
||||
'region_id' => ['required',new FidoInteger],
|
||||
'host_id_new' => [
|
||||
'required',
|
||||
new TwoByteInteger,
|
||||
function ($attribute,$value,$fail) use ($request) {
|
||||
// Check that the region doesnt already exist
|
||||
$o = Address::where(function($query) use ($value) {
|
||||
return $query->where(function($query) use ($value) {
|
||||
return $query
|
||||
->where('region_id',$value)
|
||||
->where('role',Address::NODE_RC);
|
||||
})
|
||||
// Check that a host doesnt already exist
|
||||
->orWhere(function($query) use ($value) {
|
||||
return $query
|
||||
->where('host_id',$value)
|
||||
->where('role',Address::NODE_NC);
|
||||
});
|
||||
})
|
||||
->where('zone_id',$request->zone_id)
|
||||
->where('node_id',$request->node_id_new)
|
||||
->where('point_id',0);
|
||||
|
||||
if ($o->count()) {
|
||||
$fail('Region or host already exists');
|
||||
}
|
||||
},
|
||||
],
|
||||
'node_id_new' => [
|
||||
'required',
|
||||
new TwoByteInteger,
|
||||
function ($attribute,$value,$fail) use ($request) {
|
||||
// Check that the region doesnt already exist
|
||||
$o = Address::where(function($query) use ($request,$value) {
|
||||
return $query
|
||||
->where('zone_id',$request->zone_id)
|
||||
->where('host_id',$request->host_id_new)
|
||||
->where('node_id',$value)
|
||||
->where('point_id',0)
|
||||
->where('role',Address::NODE_RC);
|
||||
});
|
||||
|
||||
if ($o->count()) {
|
||||
$fail('Host already exists');
|
||||
}
|
||||
},
|
||||
]
|
||||
]);
|
||||
|
||||
// Find the Hub address
|
||||
// Find the zones <HOST>/0 address, and assign it to this host.
|
||||
$oo = Address::where('zone_id',$request->zone_id)
|
||||
->where('region_id',$request->region_id)
|
||||
->where('host_id',$request->host_id_new)
|
||||
->where('node_id',0)
|
||||
->where('point_id',0)
|
||||
->single();
|
||||
|
||||
// Its not defined, so we'll create it.
|
||||
if (! $oo) {
|
||||
$oo = new Address;
|
||||
$oo->forceFill([
|
||||
'zone_id'=>$request->zone_id,
|
||||
'region_id'=>$request->region_id,
|
||||
'host_id'=>$request->host_id_new,
|
||||
'node_id'=>0,
|
||||
'point_id'=>0,
|
||||
'role'=>Address::NODE_NC,
|
||||
]);
|
||||
}
|
||||
|
||||
$oo->system_id = $request->system_id;
|
||||
$oo->active = TRUE;
|
||||
$o->addresses()->save($oo);
|
||||
|
||||
$oo = new Address;
|
||||
$oo->zone_id = $request->zone_id;
|
||||
$oo->region_id = $request->region_id;
|
||||
$oo->host_id = $request->host_id_new;
|
||||
$oo->node_id = $request->node_id_new;
|
||||
$oo->region_id = $request->validated('region_id');
|
||||
$oo->host_id = $request->validated('host_id_new');
|
||||
$oo->node_id = 0;
|
||||
$oo->point_id = 0;
|
||||
$oo->active = TRUE;
|
||||
|
||||
$o->addresses()->save($oo);
|
||||
break;
|
||||
|
||||
case 'update':
|
||||
case 'node':
|
||||
$request->validate([
|
||||
'region_id' => ['required',new FidoInteger],
|
||||
'host_id' => ['required',new FidoInteger],
|
||||
'node_id' => [
|
||||
'required',
|
||||
new TwoByteInteger,
|
||||
function ($attribute,$value,$fail) use ($request) {
|
||||
if ($request->point_id === 0) {
|
||||
// Check that the host doesnt already exist
|
||||
$o = Address::where(function($query) use ($request,$value) {
|
||||
return $query
|
||||
->where('zone_id',$request->zone_id)
|
||||
->where('host_id',$request->host_id)
|
||||
->where('node_id',$value)
|
||||
->where('point_id',0)
|
||||
->where('id','<>',$request->submit);
|
||||
});
|
||||
$oo->region_id = $request->validated('region_id');
|
||||
$oo->host_id = $request->validated('host_id');
|
||||
$oo->node_id = $request->validated('node_id');
|
||||
$oo->point_id = $request->validated('point_id');
|
||||
$oo->hub_id = $request->validated('hub_id') ? $request->validated('hub_id') : NULL;
|
||||
|
||||
if ($o->count()) {
|
||||
$fail(sprintf('Host already exists: %s',$o->get()->pluck('ftn')->join(',')));
|
||||
}
|
||||
}
|
||||
},
|
||||
],
|
||||
'point_id' => [
|
||||
'required',
|
||||
function($attribute,$value,$fail) use ($request) {
|
||||
if (! is_numeric($value) || $value > Address::ADDRESS_FIELD_MAX)
|
||||
$fail(sprintf('Point numbers must be between 0 and %d',Address::ADDRESS_FIELD_MAX));
|
||||
|
||||
// Check that the host doesnt already exist
|
||||
$o = Address::where(function($query) use ($request,$value) {
|
||||
return $query
|
||||
->where('zone_id',$request->zone_id)
|
||||
->where('host_id',$request->host_id)
|
||||
->where('node_id',$request->node_id)
|
||||
->where('point_id',$value)
|
||||
->where('id','<>',$request->submit);
|
||||
});
|
||||
|
||||
if ($o->count()) {
|
||||
$fail(sprintf('Point already exists: %s',$o->get()->pluck('ftn')->join(',')));
|
||||
}
|
||||
}
|
||||
],
|
||||
'hub' => 'required|boolean',
|
||||
'hub_id' => 'nullable|exists:addresses,id',
|
||||
'security' => 'required|integer|min:0|max:7',
|
||||
]);
|
||||
|
||||
$oo = Address::findOrNew($request->submit);
|
||||
$oo->zone_id = $request->zone_id;
|
||||
$oo->region_id = $request->region_id;
|
||||
$oo->host_id = $request->host_id;
|
||||
$oo->node_id = $request->node_id;
|
||||
$oo->point_id = $request->point_id;
|
||||
// @todo Validation should check that the hub is in the right region and net
|
||||
$oo->hub_id = $request->hub_id > 0 ? $request->hub_id : NULL;
|
||||
$oo->security = $request->security;
|
||||
$oo->active = TRUE;
|
||||
|
||||
$o->addresses()->save($oo);
|
||||
break;
|
||||
|
||||
default:
|
||||
return redirect()->back()->withErrors(['action'=>'Unknown action: '.$request->action]);
|
||||
}
|
||||
|
||||
$o->addresses()->save($oo);
|
||||
|
||||
return redirect()->to(sprintf('system/addedit/%d',$o->id));
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user