2021-06-17 14:08:30 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
2021-06-24 13:09:09 +00:00
|
|
|
use App\Http\Controllers\DomainController;
|
2021-06-17 14:08:30 +00:00
|
|
|
use App\Traits\ScopeActive;
|
2021-07-26 11:21:58 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2021-06-17 14:08:30 +00:00
|
|
|
|
|
|
|
class System extends Model
|
|
|
|
{
|
|
|
|
use HasFactory,ScopeActive;
|
|
|
|
|
2021-07-04 13:24:38 +00:00
|
|
|
protected $dates = ['last_session'];
|
|
|
|
|
2021-06-17 14:08:30 +00:00
|
|
|
/* RELATIONS */
|
|
|
|
|
2021-06-20 13:03:20 +00:00
|
|
|
public function addresses()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Address::class)
|
|
|
|
->orderBy('region_id')
|
|
|
|
->orderBy('host_id')
|
|
|
|
->orderBy('node_id')
|
|
|
|
->orderBy('point_id');
|
|
|
|
}
|
2021-06-24 13:09:09 +00:00
|
|
|
|
2021-07-04 11:47:23 +00:00
|
|
|
/**
|
|
|
|
* Session Passwords for system
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
|
|
*/
|
|
|
|
public function sessions()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(Zone::class)
|
2021-08-09 13:35:22 +00:00
|
|
|
->withPivot(['sespass','pktpass','ticpass','fixpass','zt_ipv4','zt_ipv6','default']);
|
2021-07-04 11:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If this system is configured as this host
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
|
|
|
*/
|
|
|
|
public function setup()
|
|
|
|
{
|
|
|
|
return $this->hasOne(Setup::class);
|
|
|
|
}
|
|
|
|
|
2021-08-15 01:42:38 +00:00
|
|
|
/**
|
|
|
|
* This system is the ZC for the following zones
|
|
|
|
*/
|
|
|
|
public function zcs()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(Zone::class);
|
|
|
|
}
|
|
|
|
|
2021-07-04 11:47:23 +00:00
|
|
|
/**
|
|
|
|
* Zones a system has addresses for
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
|
|
|
|
*/
|
|
|
|
public function zones()
|
|
|
|
{
|
|
|
|
return $this->hasManyThrough(Zone::class,Address::class,'system_id','id','id','zone_id');
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:54:23 +00:00
|
|
|
/* METHODS */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the system's address in the same zone
|
|
|
|
*
|
2021-07-26 11:21:58 +00:00
|
|
|
* @param Zone $o
|
|
|
|
* @return Collection
|
2021-07-15 14:54:23 +00:00
|
|
|
*/
|
2021-07-26 11:21:58 +00:00
|
|
|
public function match(Zone $o): Collection
|
2021-07-15 14:54:23 +00:00
|
|
|
{
|
2021-07-26 11:21:58 +00:00
|
|
|
return $this->addresses->where('zone_id',$o->id);
|
2021-07-15 14:54:23 +00:00
|
|
|
}
|
2021-06-24 13:09:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the system name, or role name for the zone
|
|
|
|
*
|
|
|
|
* @param Address $o
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-06-25 11:31:57 +00:00
|
|
|
public function full_name(Address $o): string
|
2021-06-24 13:09:09 +00:00
|
|
|
{
|
|
|
|
switch ($o->attributes['role']) {
|
|
|
|
case DomainController::NODE_ZC;
|
|
|
|
return sprintf('ZC-%s-%05d',$o->zone->domain->name,$o->zone->zone_id);
|
|
|
|
|
|
|
|
case DomainController::NODE_RC;
|
|
|
|
return sprintf('RC-%s-%05d',$o->zone->domain->name,$o->region_id);
|
|
|
|
|
|
|
|
case DomainController::NODE_NC;
|
2021-07-26 11:21:58 +00:00
|
|
|
return sprintf('NC-%s-%05d',$o->zone->domain->name,$o->host_id);
|
|
|
|
|
2021-06-24 13:09:09 +00:00
|
|
|
case DomainController::NODE_HC;
|
|
|
|
case NULL:
|
|
|
|
default:
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
}
|
2021-06-17 14:08:30 +00:00
|
|
|
}
|