Work on registration of existing systems to users

This commit is contained in:
Deon George
2022-03-14 22:28:54 +11:00
parent d68307461e
commit 8072f7c5a9
19 changed files with 553 additions and 56 deletions

View File

@@ -7,11 +7,13 @@ use Illuminate\Support\Collection;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\ViewErrorBag;
use App\Http\Requests\SystemRegister;
use App\Models\{Address,Echoarea,System,SystemZone,Zone};
use App\Models\{Address,Echoarea,Setup,System,SystemZone,Zone};
use App\Notifications\AddressLink;
use App\Rules\{FidoInteger,TwoByteInteger};
class SystemController extends Controller
@@ -258,6 +260,8 @@ class SystemController extends Controller
*/
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','mailer_type','mailer_address','mailer_port','zt_id'] as $key)
$o->{$key} = $request->post($key);
@@ -269,11 +273,9 @@ class SystemController extends Controller
$o->load(['addresses.zone.domain']);
return Gate::check('update',$o)
? view('system.addedit')
return view('system.addedit')
->with('action',$o->exists ? 'update' : 'create')
->with('o',$o)
: redirect()->to('user/system/register');
->with('o',$o);
}
public function api_address(Request $request,System $o): Collection
@@ -300,6 +302,42 @@ class SystemController extends Controller
->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
{
$result = collect();
list($zone_id,$host_id,$node_id,$point_id,$domain) = sscanf($request->query('term'),'%d:%d/%d.%d@%s');
# Look for Systems
foreach (Address::select(['addresses.id','systems.name',DB::raw('systems.id AS system_id'),'zones.zone_id','region_id','host_id','node_id','point_id','addresses.zone_id'])
->join('zones',['zones.id'=>'addresses.zone_id'])
->rightjoin('systems',['systems.id'=>'addresses.system_id'])
->when($zone_id || $host_id || $node_id,function($query) use ($zone_id,$host_id,$node_id) {
return $query
->when($zone_id,function($q,$zone_id) { return $q->where('zones.zone_id',$zone_id); })
->where(function($q) use ($host_id) {
return $q
->when($host_id,function($q,$host_id) { return $q->where('region_id',$host_id); })
->when($host_id,function($q,$host_id) { return $q->orWhere('host_id',$host_id); });
})
->when($node_id,function($q,$node_id) { return $q->where('node_id',$node_id); });
})
->orWhere('systems.name','ilike','%'.$request->query('term').'%')
->orderBy('systems.name')
->get() as $o)
{
$result->push(['id'=>$o->id,'name'=>sprintf('%s (%s)',$o->ftn3d,$o->name),'category'=>'Systems']);
}
return $result;
}
/**
* Delete address assigned to a host
*
@@ -450,8 +488,26 @@ class SystemController extends Controller
*/
public function system_register(SystemRegister $request)
{
// Step 1, show the user a form to select an existing defined system
if ($request->isMethod('GET'))
return view('user.system.register');
$o = System::findOrNew($request->system_id);
// If the system exists, and we are 'register', we'll start the address claim process
if ($o->exists && $request->action == 'register') {
$validate = Setup::findOrFail(config('app.id'))->system->inMyZones($o->addresses);
// If we have addresses, we'll trigger the routed netmail
if ($validate->count())
Notification::route('netmail',$x=$validate->first())->notify(new AddressLink($x,Auth::user()));
return view('user.system.widget.register_confirm')
->with('validate',$validate)
->with('o',$o);
}
// If the system doesnt exist, we'll create it
if (! $o->exist) {
$o->sysop = Auth::user()->name;
@@ -479,4 +535,4 @@ class SystemController extends Controller
->with('o',$o)
->with('errors',new ViewErrorBag);
}
}
}