Private
Public Access
listoooooooooo
Signed-off-by: fede <federico.nicolas.polidoro@gmail.com>
This commit is contained in:
1 parent
ff21aa4264
commit
ce83c494c9
4 files changed
+48
-86
No files matched your search
@@ -62,29 +62,6 @@ func (EmpresaUsuario) TableName() string {
|
|||||||
return "empresa_usuarios"
|
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
|
// InvitacionEmpresa permite sumar un usuario existente o crear uno nuevo en
|
||||||
// una empresa sin compartir contraseñas.
|
// una empresa sin compartir contraseñas.
|
||||||
type InvitacionEmpresa struct {
|
type InvitacionEmpresa struct {
|
||||||
|
|||||||
@@ -27,12 +27,13 @@ func (Grupo) TableName() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g *Grupo) Buscar(id uint, visitados map[uint]bool) bool {
|
func (g *Grupo) Buscar(id uint, visitados map[uint]bool) bool {
|
||||||
if visitados[g.ID] {
|
if visitados[g.ID] == true {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
visitados[g.ID] = true
|
visitados[g.ID] = true
|
||||||
|
|
||||||
for i := range g.Permisos {
|
for i := range g.Permisos {
|
||||||
|
println(visitados)
|
||||||
if g.Permisos[i].Buscar(id, visitados) {
|
if g.Permisos[i].Buscar(id, visitados) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -46,11 +47,13 @@ func (g *Grupo) Buscar(id uint, visitados map[uint]bool) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g *Grupo) ListarPermisos(permisos map[uint]Permiso, visitados map[uint]bool) {
|
func (g *Grupo) ListarPermisos(permisos map[uint]Permiso, visitados map[uint]bool) {
|
||||||
if visitados[g.ID] {
|
if visitados[g.ID] == true {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
visitados[g.ID] = true
|
visitados[g.ID] = true
|
||||||
|
|
||||||
|
|
||||||
|
println(visitados)
|
||||||
for i := range g.Permisos {
|
for i := range g.Permisos {
|
||||||
g.Permisos[i].ListarPermisos(permisos, visitados)
|
g.Permisos[i].ListarPermisos(permisos, visitados)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,37 +3,16 @@ package models_seguridad
|
|||||||
// IDs estables del catálogo global de permisos. Se mantienen fijos para no
|
// IDs estables del catálogo global de permisos. Se mantienen fijos para no
|
||||||
// romper tokens, grupos ni datos existentes.
|
// romper tokens, grupos ni datos existentes.
|
||||||
const (
|
const (
|
||||||
PermisoGestionPermisos uint = 1
|
PermisoGestionPermisos uint = 1
|
||||||
PermisoGestionGrupos uint = 2
|
PermisoGestionGrupos uint = 2
|
||||||
PermisoGestionUsuarios uint = 3
|
PermisoGestionUsuarios uint = 3
|
||||||
PermisoGestionRepuestos uint = 4
|
PermisoGestionRepuestos uint = 4
|
||||||
PermisoGestionLogs uint = 6
|
PermisoGestionLogs uint = 6
|
||||||
PermisoGestionMarcas uint = 5
|
PermisoGestionMarcas uint = 5
|
||||||
PermisoGestionEmpresa uint = 7
|
PermisoGestionEmpresa uint = 7
|
||||||
PermisoPlataforma uint = 8
|
PermisoPlataforma uint = 8
|
||||||
PermisoGestionStock uint = 9
|
PermisoGestionStock uint = 9
|
||||||
PermisoGestionAlmacenes uint = 10
|
PermisoGestionAlmacenes uint = 10
|
||||||
PermisoGestionProveedores uint = 11
|
PermisoGestionProveedores uint = 11
|
||||||
PermisoGestionClientes uint = 12
|
PermisoGestionClientes uint = 12
|
||||||
)
|
)
|
||||||
|
|
||||||
// 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,
|
|
||||||
PermisoGestionStock,
|
|
||||||
PermisoGestionAlmacenes,
|
|
||||||
PermisoGestionProveedores,
|
|
||||||
PermisoGestionClientes,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func EsPermisoDePlataforma(id uint) bool {
|
|
||||||
return id == PermisoGestionPermisos || id == PermisoPlataforma
|
|
||||||
}
|
|
||||||
@@ -51,42 +51,45 @@ func (r *repositorioGrupo) ObtenerTodosConRelaciones() ([]models_seguridad.Grupo
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *repositorioGrupo) cargarGruposRecursivo(grupo *models_seguridad.Grupo, simple bool) error {
|
func (r *repositorioGrupo) cargarGruposRecursivo(grupo *models_seguridad.Grupo, simple bool) error {
|
||||||
|
return r.cargarGruposRec(grupo, simple, map[uint]struct{}{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *repositorioGrupo) cargarGruposRec(
|
||||||
|
grupo *models_seguridad.Grupo,
|
||||||
|
simple bool,
|
||||||
|
ancestros map[uint]struct{},
|
||||||
|
) error {
|
||||||
|
// Corta ciclos: si este grupo ya está en el camino actual, no bajamos más.
|
||||||
|
if _, ok := ancestros[grupo.ID]; ok {
|
||||||
|
grupo.Grupos = nil
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
ancestros[grupo.ID] = struct{}{}
|
||||||
|
defer delete(ancestros, grupo.ID)
|
||||||
|
|
||||||
|
db := r.scopedDB()
|
||||||
if simple {
|
if simple {
|
||||||
err := r.scopedDB().
|
db = db.Preload("Grupos.Permisos", func(db *gorm.DB) *gorm.DB {
|
||||||
Model(grupo).
|
return db.Select("id", "nombre", "alias", "imagen")
|
||||||
Preload("Permisos", func(db *gorm.DB) *gorm.DB {
|
})
|
||||||
return db.Select("id", "nombre", "alias", "imagen")
|
|
||||||
}).
|
|
||||||
Association("Grupos").
|
|
||||||
Find(&grupo.Grupos)
|
|
||||||
if err != nil {
|
|
||||||
// GORM devuelve record not found cuando no hay subgrupos; no es error.
|
|
||||||
if !errors.Is(err, gorm.ErrRecordNotFound) && err.Error() != "record not found" {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
err := r.scopedDB().
|
db = db.Preload("Grupos.Permisos")
|
||||||
Model(grupo).
|
|
||||||
Preload("Permisos").
|
|
||||||
Association("Grupos").
|
|
||||||
Find(&grupo.Grupos)
|
|
||||||
if err != nil {
|
|
||||||
if !errors.Is(err, gorm.ErrRecordNotFound) && err.Error() != "record not found" {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var tmp models_seguridad.Grupo
|
||||||
|
if err := db.First(&tmp, grupo.ID).Error; err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
grupo.Grupos = tmp.Grupos
|
||||||
|
|
||||||
for i := range grupo.Grupos {
|
for i := range grupo.Grupos {
|
||||||
if err := r.cargarGruposRecursivo(
|
if err := r.cargarGruposRec(&grupo.Grupos[i], simple, ancestros); err != nil {
|
||||||
&grupo.Grupos[i],
|
|
||||||
simple,
|
|
||||||
); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user