clrghouz/app/Models/User.php

129 lines
2.6 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Collection;
use Laravel\Sanctum\HasApiTokens;
use App\Traits\{ScopeActive,UserSwitch};
/**
* Class User
*
* User Roles:
* + Site Admin
* + ZC - For Domain/Zone
* + RC - For sub portion of a Domain/Zone (aka Region)
* + Host Admin - For sub portion of a Region
* + Hub Admin - For a sub portion of a Hosts system
* + Sysop - Individual system
* + Guest
*
* @package App\Models
*/
class User extends Authenticatable implements MustVerifyEmail
{
use HasFactory,Notifiable,HasApiTokens,UserSwitch,ScopeActive;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
'last_on' => 'datetime:Y-m-d H:i:s',
'passkey' => 'json',
];
/* RELATIONS */
public function system()
{
return $this->belongsTo(System::class);
}
public function systems()
{
return $this->belongsToMany(System::class);
}
/* GENERAL METHODS */
public function addresses(Domain $o=NULL): Collection
{
return Address::select('addresses.*')
->join('systems',['systems.id'=>'addresses.system_id'])
->join('system_user',['system_user.system_id'=>'systems.id'])
->when(! is_null($o),function($query) use ($o) {
return $query
->join('zones',['zones.id'=>'addresses.zone_id'])
->where('zones.domain_id',$o->id);
})
->where('system_user.user_id',$this->id)
->with(['zone.domain'])
->get();
}
/**
* Is this user a ZC of a domain?
*
* @return bool
*/
public function isZC(): bool
{
return $this->zc()->count() > 0;
}
/**
* @return Collection
* @deprecated not used - but if it is, probably could use Address::points()?
*/
public function points(): Collection
{
$result = collect();
foreach($this->systems->pluck('addresses')->flatten()->where('role','>',Address::NODE_HC) as $ao)
$result = $result->merge($ao->children());
return $result;
}
/**
* Return the zones that this user is ZC for
*
* @return Collection
*/
public function zc(): Collection
{
$this->load('systems.addresses.nodes_hub');
return $this->systems->pluck('addresses')->flatten()->where('role_id',Address::NODE_ZC);
}
}