primeros cambios hechos para tener el multitenant

This commit is contained in:
fede committed 2026-11-10 17:06:56 -03:00
1 parent 34ad4424f9
commit 4e082e8767
132 files changed
+6202 -762

No files matched your search

+106
View File
@@ -0,0 +1,106 @@
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"`
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"
}
+3 -2
View File
@@ -8,12 +8,13 @@ import (
type Grupo struct {
ID uint `gorm:"primaryKey" json:"id"`
Nombre string `gorm:"uniqueIndex;not null;type:varchar(100)" json:"nombre"`
EmpresaID uint `gorm:"not null;default:1;index;uniqueIndex:uq_grupo_empresa_nombre,priority:1" json:"empresaId"`
Nombre string `gorm:"not null;type:varchar(100);uniqueIndex:uq_grupo_empresa_nombre,priority:2" json:"nombre"`
Descripcion string `gorm:"type:text" json:"descripcion"`
Imagen string `gorm:"type:varchar(500)" json:"imagen"`
Permisos []Permiso `gorm:"many2many:grupo_permisos;" json:"permisos,omitempty"`
Grupos []Grupo `gorm:"many2many:grupo_grupos;joinForeignKey:ParentGroupID;joinReferences:ChildGroupID" json:"grupos_hijos,omitempty"`
Usuarios []Usuario `gorm:"many2many:usuario_grupos;" json:"usuarios,omitempty"`
Usuarios []Usuario `gorm:"many2many:usuarios_grupos;" json:"usuarios,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
+31
View File
@@ -0,0 +1,31 @@
package models_seguridad
// IDs estables del catálogo global de permisos. Se mantienen fijos para no
// romper tokens, grupos ni datos existentes.
const (
PermisoGestionPermisos uint = 1
PermisoGestionGrupos uint = 2
PermisoGestionUsuarios uint = 3
PermisoGestionRepuestos uint = 4
PermisoGestionLogs uint = 5
PermisoGestionMarcas uint = 6
PermisoGestionEmpresa uint = 7
PermisoPlataforma uint = 8
)
// PermisosDeEmpresa son los permisos que puede recibir una empresa. Los
// permisos de plataforma (1 y 8) nunca se asignan a grupos de tenant.
func PermisosDeEmpresa() []uint {
return []uint{
PermisoGestionGrupos,
PermisoGestionUsuarios,
PermisoGestionRepuestos,
PermisoGestionLogs,
PermisoGestionMarcas,
PermisoGestionEmpresa,
}
}
func EsPermisoDePlataforma(id uint) bool {
return id == PermisoGestionPermisos || id == PermisoPlataforma
}
+11 -9
View File
@@ -7,15 +7,17 @@ import (
)
type Usuario struct {
ID uint `gorm:"primaryKey" json:"id"`
Email string `gorm:"uniqueIndex;not null;type:varchar(100)" json:"email"`
Password string `gorm:"not null" json:"-"`
Name string `gorm:"not null" json:"name"`
Grupos []Grupo `gorm:"many2many:usuarios_grupos;" json:"grupos,omitempty"`
// Empresas []Empresa `gorm:"many2many:user_empresa;" json:"empresas,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
ID uint `gorm:"primaryKey" json:"id"`
Email string `gorm:"uniqueIndex;not null;type:varchar(100)" json:"email"`
Password string `gorm:"not null" json:"-"`
Name string `gorm:"not null" json:"name"`
Activo bool `gorm:"not null;default:true" json:"activo"`
DebeCambiarPassword bool `gorm:"not null;default:false" json:"-"`
Grupos []Grupo `gorm:"many2many:usuarios_grupos;" json:"grupos,omitempty"`
Empresas []Empresa `gorm:"many2many:empresa_usuarios;" json:"empresas,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}
func (Usuario) TableName() string {