This commit is contained in:
251
api/app/Models/Cadastro/SujeitoAtencao/Usuario.php
Normal file
251
api/app/Models/Cadastro/SujeitoAtencao/Usuario.php
Normal file
@@ -0,0 +1,251 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Cadastro\SujeitoAtencao;
|
||||
|
||||
use App\Exceptions\Model\DeletingModelException;
|
||||
use App\Models\Almoxarifado\SaidaBaixa;
|
||||
use App\Models\Almoxarifado\SaidaRequisicao;
|
||||
use App\Models\Almoxarifado\SaidaTransferencia;
|
||||
use App\Models\AtencaoBasica\Parametrizacao\Equipe;
|
||||
use App\Models\AtencaoBasica\Parametrizacao\EquipeUsuario;
|
||||
use App\Models\Cadastro\Estabelecimento\Unidade;
|
||||
use App\Models\Cadastro\SujeitoAtencao\Lotacao;
|
||||
use App\Models\ExameImagem\Agendamento\Agendamento;
|
||||
use App\Models\Farmacia\Receita;
|
||||
use App\Models\Intranet\Estado;
|
||||
use Hash;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Tymon\JWTAuth\Contracts\JWTSubject;
|
||||
|
||||
class Usuario extends Authenticatable implements JWTSubject
|
||||
{
|
||||
use SoftDeletes;
|
||||
|
||||
protected $table = 'cadastro_usuario';
|
||||
|
||||
protected $fillable = [
|
||||
'cidadao_id',
|
||||
'registro_tipo_id',
|
||||
'registro_tipo_outro',
|
||||
'registro_numero',
|
||||
'registro_estado_id',
|
||||
'usuario',
|
||||
'email',
|
||||
'senha',
|
||||
'bloqueado',
|
||||
'is_administrador',
|
||||
'troca_senha',
|
||||
'tempo_sessao',
|
||||
'data_hora_ultimo_acesso',
|
||||
'data_hora_ultima_tentativa',
|
||||
'data_hora_ultima_troca_senha',
|
||||
];
|
||||
|
||||
protected $hidden = [
|
||||
'created_at',
|
||||
'created_lotacao_id',
|
||||
'created_centro_custo_id',
|
||||
'updated_at',
|
||||
'updated_lotacao_id',
|
||||
'updated_centro_custo_id',
|
||||
'deleted_at',
|
||||
'deleted_lotacao_id',
|
||||
'deleted_centro_custo_id',
|
||||
'cidadao_id',
|
||||
'senha'
|
||||
];
|
||||
|
||||
protected $with = [
|
||||
'registroTipo',
|
||||
'cidadao',
|
||||
'atencaoBasicaEquipes',
|
||||
];
|
||||
|
||||
protected $appends = [
|
||||
'nome',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'is_administrador' => 'boolean',
|
||||
];
|
||||
|
||||
public static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::creating(function ($model) {
|
||||
|
||||
if (auth()->check()) {
|
||||
|
||||
$model->created_lotacao_id = auth()->payload()->get('lotacao_id');
|
||||
$model->created_centro_custo_id = auth()->payload()->get('centro_custo_id');
|
||||
|
||||
$model->updated_lotacao_id = auth()->payload()->get('lotacao_id');
|
||||
$model->updated_centro_custo_id = auth()->payload()->get('centro_custo_id');
|
||||
}
|
||||
});
|
||||
|
||||
self::created(function($model){
|
||||
UsuarioHistoricoSenha::create([
|
||||
'usuario_id' => $model->id,
|
||||
'senha' => $model->senha,
|
||||
]);
|
||||
});
|
||||
|
||||
static::updating(function ($model) {
|
||||
|
||||
if (auth()->check()) {
|
||||
|
||||
$model->updated_lotacao_id = auth()->payload()->get('lotacao_id');
|
||||
$model->updated_centro_custo_id = auth()->payload()->get('centro_custo_id');
|
||||
}
|
||||
});
|
||||
|
||||
self::updated(function($model){
|
||||
if ($model->senha !== $model->getOriginal('senha')) {
|
||||
UsuarioHistoricoSenha::create([
|
||||
'usuario_id' => $model->id,
|
||||
'senha' => $model->senha,
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
static::deleting(function ($model) {
|
||||
|
||||
if (!$model->isAllowedToDelete) {
|
||||
|
||||
throw new DeletingModelException;
|
||||
}
|
||||
});
|
||||
|
||||
static::deleted(function ($model) {
|
||||
|
||||
if ($model->trashed() && auth()->check()) {
|
||||
|
||||
$model->deleted_lotacao_id = auth()->payload()->get('lotacao_id');
|
||||
$model->deleted_centro_custo_id = auth()->payload()->get('centro_custo_id');
|
||||
|
||||
$model->save();
|
||||
}
|
||||
});
|
||||
|
||||
static::restored(function ($model) {
|
||||
|
||||
$model->deleted_lotacao_id = null;
|
||||
$model->deleted_centro_custo_id = null;
|
||||
|
||||
$model->save();
|
||||
});
|
||||
}
|
||||
|
||||
public function getJWTIdentifier()
|
||||
{
|
||||
return $this->getKey();
|
||||
}
|
||||
|
||||
public function getJWTCustomClaims()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function cidadao()
|
||||
{
|
||||
return $this->belongsTo(Cidadao::class);
|
||||
}
|
||||
|
||||
public function registroTipo()
|
||||
{
|
||||
return $this->belongsTo(UsuarioRegistroTipo::class);
|
||||
}
|
||||
|
||||
public function registroEstado()
|
||||
{
|
||||
return $this->belongsTo(Estado::class);
|
||||
}
|
||||
|
||||
public function unidades()
|
||||
{
|
||||
return $this->hasMany(Unidade::class, 'gestor_id');
|
||||
}
|
||||
|
||||
public function lotacoes()
|
||||
{
|
||||
return $this->hasMany(Lotacao::class);
|
||||
}
|
||||
|
||||
public function lotacoesAtivas()
|
||||
{
|
||||
return $this->hasMany(Lotacao::class)->where('ativo', true);
|
||||
}
|
||||
|
||||
public function almoxarifadoSaidaRequisicoes()
|
||||
{
|
||||
return $this->hasMany(SaidaRequisicao::class);
|
||||
}
|
||||
|
||||
public function almoxarifadoSaidaTransferencias()
|
||||
{
|
||||
return $this->hasMany(SaidaTransferencia::class);
|
||||
}
|
||||
|
||||
public function almoxarifadoSaidaBaixas()
|
||||
{
|
||||
return $this->hasMany(SaidaBaixa::class, 'usuario_solicitante_id');
|
||||
}
|
||||
|
||||
public function farmaciaReceitas()
|
||||
{
|
||||
return $this->hasMany(Receita::class);
|
||||
}
|
||||
|
||||
public function getNomeAttribute()
|
||||
{
|
||||
return $this->cidadao->nome;
|
||||
}
|
||||
|
||||
public function setSenhaAttribute($value)
|
||||
{
|
||||
$this->attributes['senha'] = Hash::make($value);
|
||||
}
|
||||
|
||||
public function agendamentosExameImagem()
|
||||
{
|
||||
return $this->hasMany(Agendamento::class);
|
||||
}
|
||||
|
||||
public function getIsAllowedToDeleteAttribute()
|
||||
{
|
||||
return $this->hasNoOne([
|
||||
'unidades',
|
||||
'lotacoes',
|
||||
'almoxarifadoSaidaRequisicoes',
|
||||
'almoxarifadoSaidaTransferencias',
|
||||
'almoxarifadoSaidaBaixas',
|
||||
'farmaciaReceitas',
|
||||
]);
|
||||
}
|
||||
|
||||
private function hasNoOne(array $relations) : bool
|
||||
{
|
||||
foreach ($relations as $relation) {
|
||||
|
||||
if (!is_null($this->{$relation}) && $this->{$relation}()->get()->isNotEmpty()) {
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function atencaoBasicaEquipes()
|
||||
{
|
||||
|
||||
return $this->belongsToMany(Equipe::class, 'atencao_basica_parametrizacao_equipe_usuario')
|
||||
->using(EquipeUsuario::class)
|
||||
->as('equipeUsuario')
|
||||
->withPivot('ativo')
|
||||
->withTimestamps();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user