Rework SystemController methods and paths, no functional changes

This commit is contained in:
2023-10-05 22:15:53 +11:00
parent c86d8d8952
commit fda68bba04
22 changed files with 380 additions and 403 deletions

View File

@@ -24,6 +24,42 @@ class SystemController extends Controller
{
private const LOGKEY = 'CSC';
/**
* Add or edit a node
*/
public function add_edit(SystemRegister $request,System $o)
{
$this->authorize('update',$o);
if ($request->post()) {
foreach (['name','location','sysop','hold','phone','address','port','active','method','notes','zt_id','pkt_type'] as $key)
$o->{$key} = $request->post($key);
switch ($request->post('pollmode')) {
case 1: $o->pollmode = FALSE; break;
case 2: $o->pollmode = TRUE; break;
default: $o->pollmode = NULL;
}
$o->autohold = FALSE;
$o->save();
$mailers = collect($request->post('mailer_details'))
->filter(function($item) { return $item['port']; })
->transform(function($item) { $item['active'] = Arr::get($item,'active',FALSE); return $item; });
$o->mailers()->sync($mailers);
return redirect()->action([self::class,'home']);
}
$o->load(['addresses.zone.domain','addresses.system','sessions.domain','sessions.systems']);
return view('system.addedit')
->with('action',$o->exists ? 'update' : 'create')
->with('o',$o);
}
/**
* Add an address to a system
*
@@ -32,7 +68,7 @@ class SystemController extends Controller
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function add_address(Request $request,System $o)
public function address_add(Request $request,System $o)
{
// @todo a point address is failing validation
// @todo This should be admin of the zone
@@ -59,12 +95,12 @@ class SystemController extends Controller
->where('point_id',0)
->where('role',Address::NODE_RC);
})
// Check that a host doesnt already exist
->orWhere(function($query) use ($value) {
return $query->where('host_id',$value)
->where('point_id',0)
->where('role',Address::NODE_NC);
});
// Check that a host doesnt already exist
->orWhere(function($query) use ($value) {
return $query->where('host_id',$value)
->where('point_id',0)
->where('role',Address::NODE_NC);
});
if ($o->count()) {
$fail('Region or host already exists');
@@ -98,15 +134,15 @@ class SystemController extends Controller
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);
});
// 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->post('zone_id'))
->where('point_id',0)
->where('active',TRUE);
->where('zone_id',$request->post('zone_id'))
->where('point_id',0)
->where('active',TRUE);
if ($o->count()) {
$fail('Region or host already exists');
@@ -242,78 +278,51 @@ class SystemController extends Controller
return redirect()->back()->withErrors(['action'=>'Unknown action: '.$request->post('action')]);
}
return redirect()->to(sprintf('ftn/system/addedit/%d',$o->id));
return redirect()->to(sprintf('system/addedit/%d',$o->id));
}
/**
* Add Session details
* Delete address assigned to a host
*
* @param Request $request
* @param System $o
* @param Address $o
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function add_session(Request $request,System $o)
public function address_del(Address $o)
{
// @todo This should be admin of the zone
$this->authorize('update',$o);
session()->flash('accordion','session');
$this->authorize('admin',$o);
session()->flash('accordion','address');
$validate = $request->validate([
'zone_id' => 'required|exists:zones,id',
'sespass' => 'required|string|min:4',
'pktpass' => 'required|string|min:4|max:8',
'ticpass' => 'required|string|min:4',
'fixpass' => 'required|string|min:4',
]);
$sid = $o->system_id;
$o->active = FALSE;
$o->save();
$o->delete();
$zo = Zone::findOrFail($validate['zone_id']);
// If this session is for the ZC, it now becomes the default.
if ($o->match($zo,Address::NODE_ZC)->count()) {
SystemZone::where('default',TRUE)->update(['default'=>FALSE]);
$validate['default'] = TRUE;
}
$o->sessions()->attach($zo,$validate);
return redirect()->to(sprintf('ftn/system/addedit/%d',$o->id));
return redirect()->to(sprintf('system/addedit/%d',$sid));
}
/**
* Add or edit a node
* Demote an address NC -> node
*
* @param Address $o
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function add_edit(SystemRegister $request,System $o)
public function address_dem(Address $o)
{
$this->authorize('update',$o);
// @todo This should be admin of the zone
$this->authorize('admin',$o);
session()->flash('accordion','address');
if ($request->post()) {
foreach (['name','location','sysop','hold','phone','address','port','active','method','notes','zt_id','pkt_type'] as $key)
$o->{$key} = $request->post($key);
// Make sure that no other system has this address active.
if ($o->role === Address::NODE_ACTIVE)
return redirect()->back()->withErrors(['demaddress'=>sprintf('%s cannot be demoted any more',$o->ftn3D)]);
switch ($request->post('pollmode')) {
case 1: $o->pollmode = FALSE; break;
case 2: $o->pollmode = TRUE; break;
default: $o->pollmode = NULL;
}
$o->role = ($o->role << 1);
$o->save();
$o->autohold = FALSE;
$o->save();
$mailers = collect($request->post('mailer_details'))
->filter(function($item) { return $item['port']; })
->transform(function($item) { $item['active'] = Arr::get($item,'active',FALSE); return $item; });
$o->mailers()->sync($mailers);
return redirect()->action([self::class,'home']);
}
$o->load(['addresses.zone.domain','addresses.system','sessions.domain','sessions.systems']);
return view('system.addedit')
->with('action',$o->exists ? 'update' : 'create')
->with('o',$o);
return redirect()->to(sprintf('system/addedit/%d',$o->system_id));
}
public function address_merge(AddressMerge $request,int $id)
@@ -400,6 +409,146 @@ class SystemController extends Controller
->with('oo',$oo);
}
/**
* Move address to another system
*
* @param Request $request
* @param System $so
* @param Address $o
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function address_mov(Request $request,System $so,Address $o)
{
session()->flash('accordion','address');
// Quick check that this address belongs to this system
if ($so->addresses->search(function($item) use ($o) { return $item->id === $o->id; }) === FALSE)
abort(404);
if ($request->post()) {
$this->authorize('admin',$o);
$validated = $request->validate([
'system_id' => 'required|exists:systems,id',
'remove' => 'nullable|boolean',
'remsess' => 'nullable|boolean|exclude_if:remove,1',
]);
$o->system_id = $validated['system_id'];
$o->save();
if (Arr::get($validated,'remove')) {
$so->sessions()->detach($o->zone);
$so->mailers()->detach();
$so->users()->detach();
$so->delete();
} elseif (Arr::get($validated,'remsess')) {
$so->sessions()->detach($o->zone);
}
return redirect()->to('system/addedit/'.$validated['system_id']);
}
return view('system.moveaddr')
->with('o',$o);
}
/**
* Promote an address node -> NC
*
* @param Address $o
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function address_pro(Address $o)
{
// @todo This should be admin of the zone
$this->authorize('admin',$o);
session()->flash('accordion','address');
// Make sure that no other system has this address active.
if ($o->role === Address::NODE_NC)
return redirect()->back()->withErrors(['proaddress'=>sprintf('%s cannot be promoted any more',$o->ftn3D)]);
$o->role = ($o->role >> 1);
$o->save();
return redirect()->to(sprintf('system/addedit/%d',$o->system_id));
}
/**
* Recover a deleted address
*
* @param int $id
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function address_pur(int $id)
{
$o = Address::onlyTrashed()->findOrFail($id);
// @todo This should be admin of the zone
$this->authorize('admin',$o);
session()->flash('accordion','address');
$o->forceDelete();
return redirect()->to(sprintf('system/addedit/%d',$o->system_id));
}
/**
* Recover a deleted address
*
* @param int $id
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function address_rec(int $id)
{
$o = Address::onlyTrashed()->findOrFail($id);
// @todo This should be admin of the zone
$this->authorize('admin',$o);
session()->flash('accordion','address');
$o->restore();
return redirect()->to(sprintf('system/addedit/%d',$o->system_id));
}
/**
* Suspend address assigned to a host
*
* @param Address $o
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function address_sus(Address $o)
{
// @todo This should be admin of the zone
$this->authorize('admin',$o);
session()->flash('accordion','address');
// Make sure that no other system has this address active.
if (! $o->active && ($x=Address::where([
'zone_id'=>$o->zone_id,
'host_id'=>$o->host_id,
'node_id'=>$o->node_id,
'point_id'=>$o->point_id,
'active'=>TRUE,
])->single())) {
return redirect()->back()->withErrors(['susaddress'=>sprintf('%s is already active on system [<a href="%s">%s</a>]',$o->ftn,url('system/addedit',$x->system_id),$x->system->name)]);
}
$o->active = (! $o->active);
$o->save();
return redirect()->to(sprintf('system/addedit/%d',$o->system_id));
}
public function api_address(Request $request,System $o): Collection
{
return Address::select(['addresses.id','addresses.zone_id','region_id','host_id','node_id','point_id'])
@@ -417,26 +566,13 @@ class SystemController extends Controller
return $o;
}
/**
* Systems with no owners
*/
public function api_orphan(Request $request): Collection
{
return System::select(['id','name'])
->leftjoin('system_user',['system_user.system_id'=>'systems.id'])
->whereNull('user_id')
->where('systems.name','ilike','%'.$request->term.'%')
->orderBy('name')
->get();
}
/**
* Identify all the addresses from systems that are not owned by a user
*
* @param Request $request
* @return Collection
*/
public function api_orphan_address(Request $request): Collection
public function api_address_orphan(Request $request): Collection
{
$result = collect();
@@ -497,67 +633,6 @@ class SystemController extends Controller
->with('setup',Setup::findOrFail(config('app.id')));
}
/**
* Delete address assigned to a host
*
* @param Address $o
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function del_address(Address $o)
{
// @todo This should be admin of the zone
$this->authorize('admin',$o);
session()->flash('accordion','address');
$sid = $o->system_id;
$o->active = FALSE;
$o->save();
$o->delete();
return redirect()->to(sprintf('ftn/system/addedit/%d',$sid));
}
/**
* Demo an address NC -> node
*
* @param Address $o
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function dem_address(Address $o)
{
// @todo This should be admin of the zone
$this->authorize('admin',$o);
session()->flash('accordion','address');
// Make sure that no other system has this address active.
if ($o->role === Address::NODE_ACTIVE)
return redirect()->back()->withErrors(['demaddress'=>sprintf('%s cannot be demoted any more',$o->ftn3D)]);
$o->role = ($o->role << 1);
$o->save();
return redirect()->to(sprintf('ftn/system/addedit/%d',$o->system_id));
}
/**
* Delete address assigned to a host
*
* @param Address $o
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function del_session(System $o,Zone $zo)
{
$this->authorize('admin',$zo);
session()->flash('accordion','session');
$o->sessions()->detach($zo);
return redirect()->to(sprintf('ftn/system/addedit/%d',$o->id));
}
/**
* Update the systems echoareas
*
@@ -641,186 +716,10 @@ class SystemController extends Controller
->with('fileareas',$fo);
}
public function home()
{
return view('system.home');
}
/**
* Move address to another system
*
* @param Request $request
* @param System $so
* @param Address $o
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function mov_address(Request $request,System $so,Address $o)
{
session()->flash('accordion','address');
// Quick check that this address belongs to this system
if ($so->addresses->search(function($item) use ($o) { return $item->id === $o->id; }) === FALSE)
abort(404);
if ($request->post()) {
$this->authorize('admin',$o);
$validated = $request->validate([
'system_id' => 'required|exists:systems,id',
'remove' => 'nullable|boolean',
'remsess' => 'nullable|boolean|exclude_if:remove,1',
]);
$o->system_id = $validated['system_id'];
$o->save();
if (Arr::get($validated,'remove')) {
$so->sessions()->detach($o->zone);
$so->mailers()->detach();
$so->users()->detach();
$so->delete();
} elseif (Arr::get($validated,'remsess')) {
$so->sessions()->detach($o->zone);
}
return redirect()->to('ftn/system/addedit/'.$validated['system_id']);
}
return view('system.moveaddr')
->with('o',$o);
}
public function ours()
{
return view('system.ours');
}
/**
* Promote an address node -> NC
*
* @param Address $o
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function pro_address(Address $o)
{
// @todo This should be admin of the zone
$this->authorize('admin',$o);
session()->flash('accordion','address');
// Make sure that no other system has this address active.
if ($o->role === Address::NODE_NC)
return redirect()->back()->withErrors(['proaddress'=>sprintf('%s cannot be promoted any more',$o->ftn3D)]);
$o->role = ($o->role >> 1);
$o->save();
return redirect()->to(sprintf('ftn/system/addedit/%d',$o->system_id));
}
/**
* Suspend address assigned to a host
*
* @param Address $o
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function sus_address(Address $o)
{
// @todo This should be admin of the zone
$this->authorize('admin',$o);
session()->flash('accordion','address');
// Make sure that no other system has this address active.
if (! $o->active && ($x=Address::where([
'zone_id'=>$o->zone_id,
'host_id'=>$o->host_id,
'node_id'=>$o->node_id,
'point_id'=>$o->point_id,
'active'=>TRUE,
])->single())) {
return redirect()->back()->withErrors(['susaddress'=>sprintf('%s is already active on system [<a href="%s">%s</a>]',$o->ftn,url('ftn/system/addedit',$x->system_id),$x->system->name)]);
}
$o->active = (! $o->active);
$o->save();
return redirect()->to(sprintf('ftn/system/addedit/%d',$o->system_id));
}
/**
* Recover a deleted address
*
* @param int $id
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function rec_address(int $id)
{
$o = Address::onlyTrashed()->findOrFail($id);
// @todo This should be admin of the zone
$this->authorize('admin',$o);
session()->flash('accordion','address');
$o->restore();
return redirect()->to(sprintf('ftn/system/addedit/%d',$o->system_id));
}
/**
* Recover a deleted address
*
* @param int $id
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function pur_address(int $id)
{
$o = Address::onlyTrashed()->findOrFail($id);
// @todo This should be admin of the zone
$this->authorize('admin',$o);
session()->flash('accordion','address');
$o->forceDelete();
return redirect()->to(sprintf('ftn/system/addedit/%d',$o->system_id));
}
// @todo Can this be consolidated with system_register()
public function system_link(Request $request)
{
if (! $request->system_id)
return redirect('user/system/register');
$s = Setup::findOrFail(config('app.id'))->system;
$so = System::findOrFail($request->system_id);
$ca = NULL;
$la = NULL;
foreach ($s->akas as $ao) {
if (($ca=$so->match($ao->zone))->count())
break;
}
if ($ca->count() && $la=$ca->pop()) {
Notification::route('netmail',$la)->notify(new AddressLink(Auth::user()));
AddressPoll::dispatch($la)->delay(15);
}
return view('user.system.register_send')
->with('la',$la)
->with('o',$so);
}
/**
* Register a system, or link to an existing system
*/
public function system_register(SystemRegister $request)
public function register(SystemRegister $request)
{
// Step 1, show the user a form to select an existing defined system
if ($request->isMethod('GET'))
@@ -863,7 +762,7 @@ class SystemController extends Controller
// @todo if the system already exists and part of one of our networks, we'll need to send the registration email to confirm the address.
// @todo mark the system (or addresses) as "pending" at this stage until it is confirmed
return redirect()->to(url('ftn/system/addedit',$o->id));
return redirect()->to(url('system/addedit',$o->id));
}
// Re-flash our previously input data
@@ -876,6 +775,84 @@ class SystemController extends Controller
->with('errors',new ViewErrorBag);
}
/**
* Add Session details
*
* @param Request $request
* @param System $o
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function session_add(Request $request,System $o)
{
// @todo This should be admin of the zone
$this->authorize('update',$o);
session()->flash('accordion','session');
$validate = $request->validate([
'zone_id' => 'required|exists:zones,id',
'sespass' => 'required|string|min:4',
'pktpass' => 'required|string|min:4|max:8',
'ticpass' => 'required|string|min:4',
'fixpass' => 'required|string|min:4',
]);
$zo = Zone::findOrFail($validate['zone_id']);
// If this session is for the ZC, it now becomes the default.
if ($o->match($zo,Address::NODE_ZC)->count()) {
SystemZone::where('default',TRUE)->update(['default'=>FALSE]);
$validate['default'] = TRUE;
}
$o->sessions()->attach($zo,$validate);
return redirect()->to(sprintf('system/addedit/%d',$o->id));
}
/**
* Delete address assigned to a host
*
* @param Address $o
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function session_del(System $o,Zone $zo)
{
$this->authorize('admin',$zo);
session()->flash('accordion','session');
$o->sessions()->detach($zo);
return redirect()->to(sprintf('system/addedit/%d',$o->id));
}
// @todo Can this be consolidated with system_register()
public function system_link(Request $request)
{
if (! $request->system_id)
return redirect('user/system/register');
$s = Setup::findOrFail(config('app.id'))->system;
$so = System::findOrFail($request->system_id);
$ca = NULL;
$la = NULL;
foreach ($s->akas as $ao) {
if (($ca=$so->match($ao->zone))->count())
break;
}
if ($ca->count() && $la=$ca->pop()) {
Notification::route('netmail',$la)->notify(new AddressLink(Auth::user()));
AddressPoll::dispatch($la)->delay(15);
}
return view('user.system.register_send')
->with('la',$la)
->with('o',$so);
}
public function view(System $o)
{
$o->load(['addresses']);