From c7388c2db6f970966e63904bff4cdbfb431fbc4d Mon Sep 17 00:00:00 2001 From: Deon George Date: Mon, 9 Aug 2021 23:35:22 +1000 Subject: [PATCH] Enabled default routing --- app/Http/Controllers/ZoneController.php | 19 ++++++ app/Models/Address.php | 67 +++++++++++-------- app/Models/System.php | 2 +- app/Models/Zone.php | 9 +++ .../2021_08_08_125817_default_route.php | 36 ++++++++++ resources/views/domain/view.blade.php | 4 +- resources/views/system/addedit.blade.php | 48 ++++++++++++- routes/api.php | 4 +- 8 files changed, 154 insertions(+), 35 deletions(-) create mode 100644 database/migrations/2021_08_08_125817_default_route.php diff --git a/app/Http/Controllers/ZoneController.php b/app/Http/Controllers/ZoneController.php index 87c9744..b301412 100644 --- a/app/Http/Controllers/ZoneController.php +++ b/app/Http/Controllers/ZoneController.php @@ -95,6 +95,25 @@ class ZoneController extends Controller ->with('o',$o); } + /** + * Set address as default for zone + * + * @param Request $request + * @param Address $o + */ + public function api_default(Request $request,Zone $o) + { + $this->authorize('admin',$o); + + $default = $o->systems->where('pivot.default',TRUE); + if ($default->count() && ($default->first()->id != $request->sid)) + abort(404); + + $o->systems()->updateExistingPivot($request->sid,[ + 'default' => (bool)$request->set, + ]); + } + public function home() { return view('zone.home'); diff --git a/app/Models/Address.php b/app/Models/Address.php index 5d74813..707ea26 100644 --- a/app/Models/Address.php +++ b/app/Models/Address.php @@ -38,42 +38,48 @@ class Address extends Model if (! $this->session('sespass')) return $this->hasMany(self::class,'id','void'); - switch ($this->role) { - case DomainController::NODE_ZC: - $children = self::select('addresses.*') - ->where('zone_id',$this->zone_id); + if (! $this->session('default')) { + switch ($this->role) { + case DomainController::NODE_ZC: + $children = self::select('addresses.*') + ->where('zone_id',$this->zone_id); - break; + break; - case DomainController::NODE_RC: - $children = self::select('addresses.*') - ->where('zone_id',$this->zone_id) - ->where('region_id',$this->region_id); + case DomainController::NODE_RC: + $children = self::select('addresses.*') + ->where('zone_id',$this->zone_id) + ->where('region_id',$this->region_id); - break; + break; - case DomainController::NODE_NC: - $children = self::select('addresses.*') - ->where('zone_id',$this->zone_id) - ->where('region_id',$this->region_id) - ->where('host_id',$this->host_id); + case DomainController::NODE_NC: + $children = self::select('addresses.*') + ->where('zone_id',$this->zone_id) + ->where('region_id',$this->region_id) + ->where('host_id',$this->host_id); - break; + break; - case DomainController::NODE_HC: - // Identify our children. - $children = self::select('addresses.*') - ->where('hub_id',$this->id); + case DomainController::NODE_HC: + // Identify our children. + $children = self::select('addresses.*') + ->where('hub_id',$this->id); - break; + break; - case DomainController::NODE_ACTIVE: - case DomainController::NODE_POINT: - // Nodes dont have children, but must return a relationship instance - return $this->hasOne(self::class,NULL,'void'); + case DomainController::NODE_ACTIVE: + case DomainController::NODE_POINT: + // Nodes dont have children, but must return a relationship instance + return $this->hasOne(self::class,NULL,'void'); - default: - throw new Exception('Unknown role: '.serialize($this->role)); + default: + throw new Exception('Unknown role: '.serialize($this->role)); + } + + } else { + $children = self::select('addresses.*') + ->where('zone_id',$this->zone_id); } // Remove any children that we have session details for (SAME AS HC) @@ -116,9 +122,12 @@ class Address extends Model return $this; switch ($this->role) { - // ZCs dont have parents. + // ZCs dont have parents, but we may have a default case DomainController::NODE_ZC: - return NULL; + if (($x=$this->zone->systems->where('pivot.default',TRUE))->count()) + return $x->first()->match($this->zone)->first(); + else + return NULL; // RC case DomainController::NODE_RC: diff --git a/app/Models/System.php b/app/Models/System.php index ce5afdd..20d2ed8 100644 --- a/app/Models/System.php +++ b/app/Models/System.php @@ -34,7 +34,7 @@ class System extends Model public function sessions() { return $this->belongsToMany(Zone::class) - ->withPivot(['sespass','pktpass','ticpass','fixpass','zt_ipv4','zt_ipv6']); + ->withPivot(['sespass','pktpass','ticpass','fixpass','zt_ipv4','zt_ipv6','default']); } /** diff --git a/app/Models/Zone.php b/app/Models/Zone.php index cc73a9f..106c612 100644 --- a/app/Models/Zone.php +++ b/app/Models/Zone.php @@ -40,4 +40,13 @@ class Zone extends Model { return $this->belongsTo(System::class); } + + /** + * Get the default route for this zone + */ + public function systems() + { + return $this->belongsToMany(System::class) + ->withPivot(['default']); + } } \ No newline at end of file diff --git a/database/migrations/2021_08_08_125817_default_route.php b/database/migrations/2021_08_08_125817_default_route.php new file mode 100644 index 0000000..f84119e --- /dev/null +++ b/database/migrations/2021_08_08_125817_default_route.php @@ -0,0 +1,36 @@ +boolean('default')->nullable(); + }); + + DB::statement('CREATE UNIQUE INDEX default_zone ON system_zone (zone_id) WHERE "default" = true'); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + DB::statement("DROP UNIQUE INDEX default_zone"); + + Schema::table('system_zone', function (Blueprint $table) { + $table->dropColumn('default'); + }); + } +} diff --git a/resources/views/domain/view.blade.php b/resources/views/domain/view.blade.php index 5b8d8eb..e5b685a 100644 --- a/resources/views/domain/view.blade.php +++ b/resources/views/domain/view.blade.php @@ -70,7 +70,7 @@ @foreach ($o->zones->sortBy('zone_id') as $oz) @foreach ($oz->addresses as $ao) - {{ $ao->system->full_name($ao) }} @auth@if($ao->session('sespass'))*@elseif($ao->system->setup)+@endif[{{ $ao->system_id }}]@endauth + {{ $ao->system->full_name($ao) }} @auth@if($ao->session('sespass')){{ $ao->session('default') ? '**' : '*' }}@elseif($ao->system->setup)+@endif[{{ $ao->system_id }}]@endauth {{ $ao->system->sysop }} {{ $ao->system->location }} {{ $ao->ftn_3d }} @@ -82,7 +82,7 @@ @auth - *System defined here +This system + **Default route *System defined here +This system @endauth diff --git a/resources/views/system/addedit.blade.php b/resources/views/system/addedit.blade.php index 2433473..28291b1 100644 --- a/resources/views/system/addedit.blade.php +++ b/resources/views/system/addedit.blade.php @@ -38,12 +38,13 @@ use App\Http\Controllers\DomainController as DC; - + + @@ -56,6 +57,13 @@ use App\Http\Controllers\DomainController as DC; @foreach ($o->sessions->sortBy('zone_id') as $oo) + @@ -271,4 +279,40 @@ use App\Http\Controllers\DomainController as DC; @include('widgets.modal_delete') -@endsection \ No newline at end of file +@endsection + +@section('page-scripts') + +@append \ No newline at end of file diff --git a/routes/api.php b/routes/api.php index 1403e24..85be738 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,7 +1,8 @@ group(function () { Route::get('regions/{o}',[DomainController::class,'api_regions']); Route::get('hosts/{o}/{region}',[DomainController::class,'api_hosts']); Route::get('hubs/{o}/{host}',[DomainController::class,'api_hubs']); + Route::post('default/{o}',[ZoneController::class,'api_default']); });
   Passwords  
ZoneDefault Session Packet TIC
{{ $oo->zone_id }}@{{ $oo->domain->name }} + @if(($x=$oo->systems->where('pivot.default',TRUE))->count() && ($x->first()->id !== $o->id)) + + @else + + @endif + {{ $oo->pivot->sespass }} {{ $oo->pivot->pktpass }} {{ $oo->pivot->ticpass }}