133 lines
3.0 KiB
PHP
Executable File
133 lines
3.0 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Spatie\Permission\Traits\HasRoles;
|
|
|
|
class User extends Authenticatable
|
|
{
|
|
use HasRoles;
|
|
|
|
protected $fillable = [
|
|
'name',
|
|
'email',
|
|
'password',
|
|
'is_super_admin',
|
|
'current_escola_id',
|
|
];
|
|
|
|
protected $hidden = [
|
|
'password',
|
|
'remember_token',
|
|
];
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| RELACIONAMENTOS
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
|
|
public function escolas()
|
|
{
|
|
return $this->belongsToMany(
|
|
Escola::class,
|
|
'user_escolas',
|
|
'user_id',
|
|
'escola_id'
|
|
);
|
|
}
|
|
|
|
public function escolaAtual()
|
|
{
|
|
return $this->belongsTo(
|
|
Escola::class,
|
|
'current_escola_id'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Alunos pelos quais este usuário é responsável (N:N via pivot)
|
|
*/
|
|
public function alunosComoResponsavel()
|
|
{
|
|
return $this->belongsToMany(
|
|
Aluno::class,
|
|
'aluno_responsaveis',
|
|
'user_id',
|
|
'aluno_id'
|
|
)->withTimestamps();
|
|
}
|
|
|
|
/**
|
|
* Se você quiser acessar o "aluno" do próprio usuário (portal do aluno),
|
|
* dá pra ter 1:1 (um usuário pode ser o aluno).
|
|
*/
|
|
public function aluno()
|
|
{
|
|
return $this->hasOne(Aluno::class, 'user_id');
|
|
}
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| SUPER ADMIN
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
|
|
public function isSuperAdmin(): bool
|
|
{
|
|
return (bool) $this->is_super_admin;
|
|
}
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| FUNÇÃO / CARGO DO USUÁRIO (ROLE)
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
|
|
public function funcao(): ?string
|
|
{
|
|
if ($this->isSuperAdmin()) {
|
|
return 'Super Admin';
|
|
}
|
|
|
|
if (!$this->current_escola_id) {
|
|
return null;
|
|
}
|
|
|
|
setPermissionsTeamId($this->current_escola_id);
|
|
|
|
$role = $this->roles()->value('name');
|
|
|
|
if (!$role) {
|
|
return null;
|
|
}
|
|
|
|
$map = [
|
|
'coordenacao' => 'Coordenação',
|
|
'secretaria' => 'Secretaria',
|
|
'responsavel' => 'Responsável',
|
|
'aluno' => 'Aluno',
|
|
'representante_de_turma' => 'Representante de Turma',
|
|
'super_admin' => 'Super Admin',
|
|
];
|
|
|
|
return $map[$role] ?? ucfirst(str_replace('_', ' ', $role));
|
|
}
|
|
|
|
public function hasFuncao(string $funcao): bool
|
|
{
|
|
if ($funcao === 'super_admin') {
|
|
return $this->isSuperAdmin();
|
|
}
|
|
|
|
if (!$this->current_escola_id) {
|
|
return false;
|
|
}
|
|
|
|
setPermissionsTeamId($this->current_escola_id);
|
|
|
|
return $this->hasRole($funcao);
|
|
}
|
|
}
|