Files
Repuestera/back/models/seguridad/empresa.go
T

108 lines
3.5 KiB
Go

package models_seguridad
import (
"time"
"gorm.io/gorm"
)
// Empresa es el tenant del sistema. Todos los datos de negocio pertenecen a
// una empresa y se filtran por EmpresaID.
type Empresa struct {
ID uint `gorm:"primaryKey" json:"id"`
Nombre string `gorm:"type:varchar(120);not null" json:"nombre"`
Slug string `gorm:"type:varchar(60);uniqueIndex;not null" json:"slug"`
Cuit *string `gorm:"type:varchar(11);uniqueIndex" json:"cuit,omitempty"`
Activa bool `gorm:"not null;default:true" json:"activa"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}
func (Empresa) TableName() string {
return "empresas"
}
type RolEmpresa string
const (
RolOwner RolEmpresa = "OWNER"
RolAdmin RolEmpresa = "ADMIN"
RolMiembro RolEmpresa = "MEMBER"
)
func (r RolEmpresa) Valido() bool {
switch r {
case RolOwner, RolAdmin, RolMiembro:
return true
default:
return false
}
}
func (r RolEmpresa) EsAdministrador() bool {
return r == RolOwner || r == RolAdmin
}
// EmpresaUsuario es la membresía de un usuario global dentro de una empresa.
type EmpresaUsuario struct {
EmpresaID uint `gorm:"primaryKey" json:"empresa_id"`
UsuarioID uint `gorm:"primaryKey" json:"usuario_id"`
Rol RolEmpresa `gorm:"type:varchar(20);not null;default:'MEMBER'" json:"rol"`
Activo bool `gorm:"not null;default:true" json:"activo"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
Empresa Empresa `gorm:"foreignKey:EmpresaID" json:"empresa,omitempty"`
Usuario Usuario `gorm:"foreignKey:UsuarioID" json:"usuario,omitempty"`
}
func (EmpresaUsuario) TableName() string {
return "empresa_usuarios"
}
type RolPlataforma string
const (
RolSuperadmin RolPlataforma = "SUPERADMIN"
RolSoporte RolPlataforma = "SOPORTE"
)
// PlataformaAdmin es un rol transversal, separado de los grupos de cada
// empresa. No debe usarse para acceder a datos de negocio sin membresía.
type PlataformaAdmin struct {
UsuarioID uint `gorm:"primaryKey" json:"usuario_id"`
Rol RolPlataforma `gorm:"type:varchar(20);not null;default:'SUPERADMIN'" json:"rol"`
Activo bool `gorm:"not null;default:true" json:"activo"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Usuario Usuario `gorm:"foreignKey:UsuarioID" json:"usuario,omitempty"`
}
func (PlataformaAdmin) TableName() string {
return "plataforma_admins"
}
// InvitacionEmpresa permite sumar un usuario existente o crear uno nuevo en
// una empresa sin compartir contraseñas.
type InvitacionEmpresa struct {
ID uint `gorm:"primaryKey" json:"id"`
EmpresaID uint `gorm:"not null;index" json:"empresa_id"`
Email string `gorm:"type:varchar(100);not null;index" json:"email"`
Rol RolEmpresa `gorm:"type:varchar(20);not null;default:'MEMBER'" json:"rol"`
GrupoID uint `gorm:"not null;default:0" json:"grupo_id"`
TokenHash string `gorm:"type:varchar(64);uniqueIndex;not null" json:"-"`
ExpiraEn time.Time `gorm:"not null;index" json:"expira_en"`
UsadaEn *time.Time `json:"usada_en,omitempty"`
CreadaPor uint `gorm:"not null" json:"creada_por"`
CreatedAt time.Time `json:"created_at"`
Empresa Empresa `gorm:"foreignKey:EmpresaID" json:"empresa,omitempty"`
}
func (InvitacionEmpresa) TableName() string {
return "invitaciones_empresa"
}