atualizacao
This commit is contained in:
@@ -2,47 +2,131 @@
|
||||
|
||||
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 Spatie\Permission\Traits\HasRoles;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
/** @use HasFactory<\Database\Factories\UserFactory> */
|
||||
use HasFactory, Notifiable;
|
||||
use HasRoles;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'email',
|
||||
'password',
|
||||
'is_super_admin',
|
||||
'current_escola_id',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for serialization.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
protected $hidden = [
|
||||
'password',
|
||||
'remember_token',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| RELACIONAMENTOS
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
public function escolas()
|
||||
{
|
||||
return [
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user