2021-06-17 14:08:30 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2022-01-01 14:52:21 +00:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2021-06-17 14:08:30 +00:00
|
|
|
|
|
|
|
class System extends Model
|
|
|
|
{
|
2022-01-05 13:19:57 +00:00
|
|
|
use HasFactory;
|
2021-06-17 14:08:30 +00:00
|
|
|
|
2021-07-04 13:24:38 +00:00
|
|
|
protected $dates = ['last_session'];
|
|
|
|
|
2022-01-01 14:52:21 +00:00
|
|
|
/* SCOPES */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Only query active records
|
2023-04-22 11:31:09 +00:00
|
|
|
* @todo test false action
|
2022-01-01 14:52:21 +00:00
|
|
|
*/
|
|
|
|
public function scopeActive($query)
|
|
|
|
{
|
|
|
|
$uo = Auth::user();
|
|
|
|
|
|
|
|
return $query
|
2023-04-22 11:31:09 +00:00
|
|
|
->when($uo && ! $uo->isAdmin(),function($query) use ($uo) {
|
2022-01-01 14:52:21 +00:00
|
|
|
return $query->whereIn('id',$uo->systems->pluck('id'))
|
|
|
|
->orWhere($this->getTable().'.active',TRUE);
|
2023-04-22 11:31:09 +00:00
|
|
|
},function($query) { $query->where($this->getTable().'.active',TRUE); })
|
2022-01-01 14:52:21 +00:00
|
|
|
->orderBy('name');
|
|
|
|
}
|
|
|
|
|
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)
|
2022-11-19 01:02:13 +00:00
|
|
|
->withTrashed()
|
2022-01-01 05:59:35 +00:00
|
|
|
->FTNorder();
|
2021-06-20 13:03:20 +00:00
|
|
|
}
|
2021-06-24 13:09:09 +00:00
|
|
|
|
2022-12-02 11:54:02 +00:00
|
|
|
public function logs()
|
|
|
|
{
|
|
|
|
return $this->hasMany(SystemLog::class);
|
|
|
|
}
|
|
|
|
|
2023-04-15 03:34:08 +00:00
|
|
|
public function logs_recent()
|
|
|
|
{
|
|
|
|
return $this->hasMany(SystemLog::class)
|
|
|
|
->orderby('created_at','DESC')
|
|
|
|
->limit(10);
|
|
|
|
}
|
|
|
|
|
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-21 11:15:22 +00:00
|
|
|
public function users()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(User::class);
|
|
|
|
}
|
|
|
|
|
2021-08-15 01:42:38 +00:00
|
|
|
/**
|
|
|
|
* This system is the ZC for the following zones
|
|
|
|
*/
|
|
|
|
public function zcs()
|
|
|
|
{
|
2021-08-15 06:01:51 +00:00
|
|
|
return $this->hasMany(Zone::class);
|
2021-08-15 01:42:38 +00:00
|
|
|
}
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2022-11-25 10:44:03 +00:00
|
|
|
/* ATTRIBUTES */
|
|
|
|
|
|
|
|
public function getAccessMethodAttribute(): string
|
|
|
|
{
|
|
|
|
switch ($this->method) {
|
|
|
|
case 23: return sprintf('telnet://%s:%s',$this->address,$this->port);
|
|
|
|
case 22: return sprintf('ssh://%s:%s',$this->address,$this->port);
|
|
|
|
case 513: return sprintf('rlogin://%s:%s',$this->address,$this->port);
|
|
|
|
default:
|
|
|
|
return $this->method ? sprintf('%s:%s',$this->address,$this->port) : 'No access method available.';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAccessMailerAttribute(): string
|
|
|
|
{
|
|
|
|
switch ($this->mailer_type) {
|
|
|
|
case Setup::O_BINKP: return sprintf('binkp://%s:%s',$this->mailer_address,$this->mailer_port);
|
|
|
|
case Setup::O_EMSI: return sprintf('emsi://%s:%s',$this->mailer_address,$this->mailer_port);
|
|
|
|
default:
|
|
|
|
return $this->mailer_type ? sprintf('%s:%s',$this->address,$this->port) : 'No mailer available.';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:54:23 +00:00
|
|
|
/* METHODS */
|
|
|
|
|
2021-08-25 12:13:49 +00:00
|
|
|
public function echoareas()
|
|
|
|
{
|
|
|
|
return Echoarea::select('echoareas.*')
|
|
|
|
->join('address_echoarea',['address_echoarea.echoarea_id'=>'echoareas.id'])
|
|
|
|
->join('addresses',['addresses.id'=>'address_echoarea.address_id'])
|
|
|
|
->where('addresses.system_id',$this->id);
|
|
|
|
}
|
|
|
|
|
2022-11-01 11:24:36 +00:00
|
|
|
public function fileareas()
|
|
|
|
{
|
|
|
|
return Filearea::select('fileareas.*')
|
|
|
|
->join('address_filearea',['address_filearea.filearea_id'=>'fileareas.id'])
|
|
|
|
->join('addresses',['addresses.id'=>'address_filearea.address_id'])
|
|
|
|
->where('addresses.system_id',$this->id);
|
|
|
|
}
|
|
|
|
|
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']) {
|
2022-01-24 11:56:13 +00:00
|
|
|
case Address::NODE_ZC;
|
2021-06-24 13:09:09 +00:00
|
|
|
return sprintf('ZC-%s-%05d',$o->zone->domain->name,$o->zone->zone_id);
|
|
|
|
|
2022-01-24 11:56:13 +00:00
|
|
|
case Address::NODE_RC;
|
2021-06-24 13:09:09 +00:00
|
|
|
return sprintf('RC-%s-%05d',$o->zone->domain->name,$o->region_id);
|
|
|
|
|
2022-01-24 11:56:13 +00:00
|
|
|
case Address::NODE_NC;
|
2021-07-26 11:21:58 +00:00
|
|
|
return sprintf('NC-%s-%05d',$o->zone->domain->name,$o->host_id);
|
|
|
|
|
2022-01-24 11:56:13 +00:00
|
|
|
case Address::NODE_HC;
|
|
|
|
case Address::NODE_ACTIVE;
|
2021-06-24 13:09:09 +00:00
|
|
|
default:
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
}
|
2021-09-06 13:39:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the system's address in the same zone
|
2022-01-24 11:56:13 +00:00
|
|
|
* This function can filter based on the address type needed.
|
2021-09-06 13:39:32 +00:00
|
|
|
*
|
|
|
|
* @param Zone $o
|
2022-01-24 11:56:13 +00:00
|
|
|
* @param int $type
|
2021-09-06 13:39:32 +00:00
|
|
|
* @return Collection
|
|
|
|
*/
|
2023-06-23 06:29:56 +00:00
|
|
|
public function match(Zone $o,int $type=(Address::NODE_NC|Address::NODE_HC|Address::NODE_ACTIVE|Address::NODE_PVT|Address::NODE_POINT)): Collection
|
2021-09-06 13:39:32 +00:00
|
|
|
{
|
2022-01-24 11:56:13 +00:00
|
|
|
return $this->addresses
|
|
|
|
->where('zone_id',$o->id)
|
|
|
|
->filter(function($item) use ($type) {
|
|
|
|
return $item->role & $type;
|
|
|
|
});
|
2021-09-06 13:39:32 +00:00
|
|
|
}
|
2022-03-14 11:28:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the addresses and return which ones are in my zones
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Eloquent\Collection $addresses
|
|
|
|
* @param int $type
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function inMyZones(Collection $addresses,int $type=(Address::NODE_HC|Address::NODE_ACTIVE|Address::NODE_PVT|Address::NODE_POINT)): Collection
|
|
|
|
{
|
|
|
|
$myzones = $this->addresses->pluck('zone_id')->unique();
|
|
|
|
|
|
|
|
return $addresses->filter(function($item) use ($myzones,$type) {
|
|
|
|
return ($item->role & $type) && ($myzones->search($item->zone_id) !== FALSE);
|
|
|
|
});
|
|
|
|
}
|
2021-06-17 14:08:30 +00:00
|
|
|
}
|