Move DomainController::NODE* to Address::NODE*, make role mandatory in the database, change logic so that mail generated by the host comes from a node address.

This commit is contained in:
Deon George
2022-01-24 22:56:13 +11:00
parent efa7195633
commit d660d5a6df
15 changed files with 143 additions and 110 deletions

View File

@@ -10,7 +10,6 @@ use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpFoundation\File\File;
use App\Classes\FTN as FTNBase;
use App\Http\Controllers\DomainController;
use App\Models\{Address,Setup,Software,System,Zone};
class Packet extends FTNBase implements \Iterator, \Countable
@@ -462,7 +461,7 @@ class Packet extends FTNBase implements \Iterator, \Countable
}
$ao->active = TRUE;
$ao->role = DomainController::NODE_UNKNOWN;
$ao->role = Address::NODE_UNKNOWN;
System::unguard();
$so = System::firstOrCreate([

View File

@@ -194,7 +194,7 @@ abstract class Protocol
$addresses = collect();
foreach (($this->originate ? $this->node->aka_remote_authed : $this->node->aka_remote) as $ao)
$addresses = $addresses->merge($this->setup->system->match($ao->zone));
$addresses = $addresses->merge($this->setup->system->match($ao->zone,Address::NODE_ZC|Address::NODE_RC|Address::NODE_NC|Address::NODE_HC|Address::NODE_ACTIVE|Address::NODE_PVT|Address::NODE_POINT));
$addresses = $addresses->unique();

View File

@@ -154,7 +154,7 @@ final class Binkp extends BaseProtocol
$addresses = collect();
foreach ($this->node->aka_remote_authed as $ao)
$addresses = $addresses->merge($this->setup->system->match($ao->zone));
$addresses = $addresses->merge($this->setup->system->match($ao->zone,Address::NODE_ZC|Address::NODE_RC|Address::NODE_NC|Address::NODE_HC|Address::NODE_ACTIVE|Address::NODE_PVT|Address::NODE_POINT));
$addresses = $addresses->unique();

View File

@@ -9,17 +9,6 @@ use App\Models\{Address,Domain,Zone};
class DomainController extends Controller
{
public const NODE_ACTIVE = 0; // Active
public const NODE_ZC = 1<<0; // Zone
public const NODE_RC = 1<<1; // Region
public const NODE_NC = 1<<2; // Host
public const NODE_HC = 1<<3; // Hub
public const NODE_POINT = 1<<4; // Point
public const NODE_PVT = 1<<5; // Pvt
public const NODE_HOLD = 1<<6; // Hold
public const NODE_DOWN = 1<<7; // Down
public const NODE_UNKNOWN = 1<<8; // Down
// http://ftsc.org/docs/frl-1002.001
public const NUMBER_MAX = 0x7fff;
@@ -65,7 +54,7 @@ class DomainController extends Controller
*/
public function api_hosts(Zone $o,int $region): Collection
{
$oo = Address::where('role',self::NODE_NC)
$oo = Address::where('role',Address::NODE_NC)
->where('zone_id',$o->id)
->when($region,function($query,$region) { return $query->where('region_id',$region)->where('node_id',0); })
->when((! $region),function($query) use ($region) { return $query->where('region_id',0); })
@@ -107,7 +96,7 @@ class DomainController extends Controller
*/
public function api_regions(Zone $o): Collection
{
$oo = Address::where('role',self::NODE_RC)
$oo = Address::where('role',Address::NODE_RC)
->where('zone_id',$o->id)
->where('node_id',0)
->where('point_id',0)

View File

@@ -53,13 +53,13 @@ class SystemController extends Controller
->where('host_id',0)
->where('node_id',0)
->where('point_id',0)
->where('role',DomainController::NODE_RC);
->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',DomainController::NODE_NC);
->where('role',Address::NODE_NC);
});
if ($o->count()) {
@@ -75,7 +75,7 @@ class SystemController extends Controller
$oo->host_id = 0;
$oo->node_id = 0;
$oo->point_id = 0;
$oo->role = DomainController::NODE_RC;
$oo->role = Address::NODE_RC;
$oo->active = TRUE;
$o->addresses()->save($oo);
@@ -92,12 +92,12 @@ class SystemController extends Controller
$o = Address::where(function($query) use ($value) {
return $query->where(function($query) use ($value) {
return $query->where('region_id',$value)
->where('role',DomainController::NODE_RC);
->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',DomainController::NODE_NC);
->where('role',Address::NODE_NC);
});
})
->where('zone_id',$request->post('zone_id'))
@@ -119,7 +119,7 @@ class SystemController extends Controller
->where('host_id',$request->post('host_id_new'))
->where('node_id',$value)
->where('point_id',0)
->where('role',DomainController::NODE_RC);
->where('role',Address::NODE_RC);
});
if ($o->count()) {
@@ -147,7 +147,7 @@ class SystemController extends Controller
'host_id'=>$request->host_id_new,
'node_id'=>0,
'point_id'=>0,
'role'=>DomainController::NODE_NC,
'role'=>Address::NODE_NC,
]);
}
@@ -161,7 +161,7 @@ class SystemController extends Controller
$oo->host_id = $request->post('host_id_new');
$oo->node_id = $request->post('node_id_new');
$oo->point_id = 0;
$oo->role = DomainController::NODE_NC;
$oo->role = Address::NODE_NC;
$oo->active = TRUE;
$o->addresses()->save($oo);
@@ -204,7 +204,7 @@ class SystemController extends Controller
$oo->node_id = $request->post('node_id');
$oo->point_id = $request->post('point_id');
$oo->hub_id = $request->post('hub_id') > 0 ? $request->post('hub_id') : NULL;
$oo->role = (! $oo->point_id) && $request->post('hub') ? DomainController::NODE_HC : NULL;
$oo->role = (! $oo->point_id) && $request->post('hub') ? Address::NODE_HC : Address::NODE_ACTIVE;
$oo->active = TRUE;
$o->addresses()->save($oo);
@@ -242,7 +242,7 @@ class SystemController extends Controller
$zo = Zone::findOrFail($validate['zone_id']);
// If this session is for the ZC, it now becomes the default.
if (in_array(DomainController::NODE_ZC,$o->match($zo)->pluck('role')->toArray())) {
if ($o->match($zo,Address::NODE_ZC)->count()) {
SystemZone::where('default',TRUE)->update(['default'=>FALSE]);
$validate['default'] = TRUE;
}

View File

@@ -93,7 +93,7 @@ class ZoneController extends Controller
'host_id'=>0,
'node_id'=>0,
'point_id'=>0,
'role'=>DomainController::NODE_ZC,
'role'=>Address::NODE_ZC,
]);
}
@@ -121,7 +121,7 @@ class ZoneController extends Controller
$default = $o->systems->where('pivot.default',TRUE);
if ($default->count() && $default->first()->addresses->pluck('role')->search(DomainController::NODE_ZC) !== FALSE)
if ($default->count() && $default->first()->addresses->pluck('role')->search(Address::NODE_ZC) !== FALSE)
abort(412);
if ($default->count() && ($default->first()->id != $request->sid))

View File

@@ -12,7 +12,6 @@ use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use App\Http\Controllers\DomainController;
use App\Models\{Address,Domain,Nodelist,Setup,System,Zone};
use App\Traits\Import as ImportTrait;
@@ -134,13 +133,13 @@ class NodelistImport implements ShouldQueue
$region = 0;
$host = 0;
$hub_id = NULL;
$role = DomainController::NODE_ZC;
$role = Address::NODE_ZC;
break;
case 'Region':
$region = $fields[1];
$role = DomainController::NODE_RC;
$role = Address::NODE_RC;
$host = 0;
$hub_id = NULL;
@@ -149,36 +148,37 @@ class NodelistImport implements ShouldQueue
case 'Host':
$host = $fields[1];
$hub_id = NULL;
$role = DomainController::NODE_NC;
$role = Address::NODE_NC;
break;
case 'Hub':
$node = $fields[1];
$role = DomainController::NODE_HC;
$role = Address::NODE_HC;
break;
case 'Pvt':
$node = $fields[1];
$role = DomainController::NODE_PVT;
$role = Address::NODE_PVT;
break;
case 'Hold':
$node = $fields[1];
$role = DomainController::NODE_HOLD;
$role = Address::NODE_HOLD;
break;
case 'Down':
$node = $fields[1];
$role = DomainController::NODE_DOWN;
$role = Address::NODE_DOWN;
break;
case '':
$node = $fields[1];
$role = Address::NODE_ACTIVE;
break;
default:
@@ -205,8 +205,6 @@ class NodelistImport implements ShouldQueue
$ao->role = $role;
$ao->hub_id = $hub_id;
$role = NULL;
if ($ao->exists)
Log::debug(sprintf('%s:Processing existing address [%s]',self::LOGKEY,$ao->ftn));
@@ -330,7 +328,7 @@ class NodelistImport implements ShouldQueue
$so->baud = $fields[6];
*/
if ($method && ($ao->role != DomainController::NODE_PVT)) {
if ($method && ($ao->role != Address::NODE_PVT)) {
$so->mailer_type = $method;
$so->mailer_address = $address;
$so->mailer_port = $port;
@@ -352,7 +350,7 @@ class NodelistImport implements ShouldQueue
try {
$so->addresses()->save($ao);
if ($ao->role == DomainController::NODE_HC)
if ($ao->role == Address::NODE_HC)
$hub_id = $ao->id;
$no->addresses()->attach($ao,['role'=>$ao->role]);
@@ -382,4 +380,4 @@ class NodelistImport implements ShouldQueue
Log::info(sprintf('%s:Updated %d records from %d systems',self::LOGKEY,$p,$c));
}
}
}

View File

@@ -28,6 +28,17 @@ class Address extends Model
protected $with = ['zone'];
public const NODE_ZC = 1<<0; // Zone
public const NODE_RC = 1<<1; // Region
public const NODE_NC = 1<<2; // Host
public const NODE_HC = 1<<3; // Hub
public const NODE_ACTIVE = 1<<4; // Node
public const NODE_PVT = 1<<5; // Pvt
public const NODE_HOLD = 1<<6; // Hold
public const NODE_DOWN = 1<<7; // Down
public const NODE_POINT = 1<<8; // Point
public const NODE_UNKNOWN = 1<<15; // Unknown
/* SCOPES */
public function scopeFTNOrder($query)
@@ -52,20 +63,20 @@ class Address extends Model
if (! $this->session('default')) {
switch ($this->role) {
case DomainController::NODE_ZC:
case self::NODE_ZC:
$children = self::select('addresses.*')
->where('zone_id',$this->zone_id);
break;
case DomainController::NODE_RC:
case self::NODE_RC:
$children = self::select('addresses.*')
->where('zone_id',$this->zone_id)
->where('region_id',$this->region_id);
break;
case DomainController::NODE_NC:
case self::NODE_NC:
$children = self::select('addresses.*')
->where('zone_id',$this->zone_id)
->where('region_id',$this->region_id)
@@ -73,16 +84,16 @@ class Address extends Model
break;
case DomainController::NODE_HC:
case self::NODE_HC:
// Identify our children.
$children = self::select('addresses.*')
->where('hub_id',$this->id);
break;
case DomainController::NODE_UNKNOWN:
case DomainController::NODE_ACTIVE:
case DomainController::NODE_POINT:
case self::NODE_UNKNOWN:
case self::NODE_ACTIVE:
case self::NODE_POINT:
// Nodes dont have children, but must return a relationship instance
return $this->hasOne(self::class,NULL,'void');
@@ -158,14 +169,14 @@ class Address extends Model
switch ($this->role) {
// ZCs dont have parents, but we may have a default
case DomainController::NODE_ZC:
case self::NODE_ZC:
if (($x=$this->zone->systems->where('pivot.default',TRUE))->count())
return $x->first()->match($this->zone)->first();
return $x->first()->match($this->zone,255)->first();
else
return NULL;
// RC
case DomainController::NODE_RC:
case self::NODE_RC:
$parent = self::where('zone_id',$this->zone_id)
->where('region_id',0)
->where('host_id',0)
@@ -175,7 +186,7 @@ class Address extends Model
break;
// Hosts
case DomainController::NODE_NC:
case self::NODE_NC:
// See if we have a RC
$parent = self::where('zone_id',$this->zone_id)
->where('region_id',$this->region_id)
@@ -195,9 +206,9 @@ class Address extends Model
break;
// Hubs
case DomainController::NODE_HC:
case self::NODE_HC:
// Normal Nodes
case DomainController::NODE_ACTIVE:
case self::NODE_ACTIVE:
// If we are a child of a hub, then check our hub
$parent = (($this->hub_id)
? self::where('id',$this->hub_id)
@@ -209,9 +220,9 @@ class Address extends Model
break;
case DomainController::NODE_UNKNOWN:
case DomainController::NODE_POINT:
case DomainController::NODE_DOWN:
case self::NODE_UNKNOWN:
case self::NODE_POINT:
case self::NODE_DOWN:
// @todo Points - if the boss is defined, we should return it.
return NULL;
@@ -262,17 +273,17 @@ class Address extends Model
public function getRoleNameAttribute(): string
{
switch ($this->role) {
case DomainController::NODE_ACTIVE:
return 'NODE';
case DomainController::NODE_ZC:
case self::NODE_ZC:
return 'ZC';
case DomainController::NODE_RC:
case self::NODE_RC:
return 'RC';
case DomainController::NODE_NC:
case self::NODE_NC:
return 'NC';
case DomainController::NODE_HC:
case self::NODE_HC:
return 'HUB';
case DomainController::NODE_POINT:
case self::NODE_ACTIVE:
return 'NODE';
case self::NODE_POINT:
return 'POINT';
default:
return '?';
@@ -374,9 +385,11 @@ class Address extends Model
* @param bool $update
* @return Packet|null
*/
public function getEchomail(bool $update=TRUE): ?Packet
public function getEchomail(bool $update=TRUE,Collection $echomail=NULL): ?Packet
{
$pkt = NULL;
if ($echomail)
return $this->getPacket($echomail);
if (($x=$this->echomailWaiting())
->count())

View File

@@ -69,7 +69,7 @@ final class Echomail extends Model implements Packet
}
// Our address
$ftns = Setup::findOrFail(config('app.id'))->system->match($model->fftn->zone);
$ftns = Setup::findOrFail(config('app.id'))->system->match($model->fftn->zone,Address::NODE_ACTIVE|Address::NODE_PVT|Address::NODE_HOLD);
// Add our address to the seenby;
$model->set_seenby = array_merge($model->set_seenby,$ftns->pluck('id')->toArray());

View File

@@ -7,8 +7,6 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\DomainController;
class System extends Model
{
use HasFactory;
@@ -103,17 +101,17 @@ class System extends Model
public function full_name(Address $o): string
{
switch ($o->attributes['role']) {
case DomainController::NODE_ZC;
case Address::NODE_ZC;
return sprintf('ZC-%s-%05d',$o->zone->domain->name,$o->zone->zone_id);
case DomainController::NODE_RC;
case Address::NODE_RC;
return sprintf('RC-%s-%05d',$o->zone->domain->name,$o->region_id);
case DomainController::NODE_NC;
case Address::NODE_NC;
return sprintf('NC-%s-%05d',$o->zone->domain->name,$o->host_id);
case DomainController::NODE_HC;
case NULL:
case Address::NODE_HC;
case Address::NODE_ACTIVE;
default:
return $this->name;
}
@@ -121,12 +119,18 @@ class System extends Model
/**
* Return the system's address in the same zone
* This function can filter based on the address type needed.
*
* @param Zone $o
* @param int $type
* @return Collection
*/
public function match(Zone $o): Collection
public function match(Zone $o,int $type=(Address::NODE_HC|Address::NODE_ACTIVE|Address::NODE_PVT|Address::NODE_POINT)): Collection
{
return $this->addresses->where('zone_id',$o->id);
return $this->addresses
->where('zone_id',$o->id)
->filter(function($item) use ($type) {
return $item->role & $type;
});
}
}