Private
Public Access
primeros cambios hechos para tener el multitenant
This commit is contained in:
1 parent
34ad4424f9
commit
4e082e8767
132 files changed
+6202
-762
No files matched your search
@@ -0,0 +1,125 @@
|
||||
package plataforma
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
models_seguridad "repuestera_back/models/seguridad"
|
||||
r "repuestera_back/repositorios"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func ListarAdmins(c *gin.Context) {
|
||||
admins, err := r.ObtenerRepositorios().Empresa.ListarPlataformaAdmins()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"admins": admins})
|
||||
}
|
||||
|
||||
func AgregarAdmin(c *gin.Context) {
|
||||
var req struct {
|
||||
UsuarioID uint `json:"usuario_id"`
|
||||
Email string `json:"email"`
|
||||
Rol string `json:"rol"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
repo := r.ObtenerRepositorios()
|
||||
var usuario *models_seguridad.Usuario
|
||||
var err error
|
||||
if req.UsuarioID != 0 {
|
||||
usuario, err = repo.Usuario.ObtenerPorID(req.UsuarioID)
|
||||
} else if strings.TrimSpace(req.Email) != "" {
|
||||
usuario, err = repo.Usuario.ObtenerPorEmail(strings.ToLower(strings.TrimSpace(req.Email)))
|
||||
} else {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Indicá usuario_id o email"})
|
||||
return
|
||||
}
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) || err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Usuario no encontrado"})
|
||||
return
|
||||
}
|
||||
|
||||
rol := models_seguridad.RolPlataforma(strings.ToUpper(strings.TrimSpace(req.Rol)))
|
||||
if rol == "" {
|
||||
rol = models_seguridad.RolSuperadmin
|
||||
}
|
||||
if rol != models_seguridad.RolSuperadmin && rol != models_seguridad.RolSoporte {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Rol de plataforma inválido"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := repo.Empresa.AgregarPlataformaAdmin(usuario.ID, rol); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, gin.H{"message": "Rol de plataforma asignado", "usuario": usuario, "rol": rol})
|
||||
}
|
||||
|
||||
func QuitarAdmin(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "ID inválido"})
|
||||
return
|
||||
}
|
||||
|
||||
repo := r.ObtenerRepositorios()
|
||||
total, err := repo.Empresa.ContarPlataformaAdmins()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if total <= 1 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Debe existir al menos un administrador de plataforma"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := repo.Empresa.QuitarPlataformaAdmin(uint(id)); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Rol de plataforma removido"})
|
||||
}
|
||||
|
||||
func ListarUsuarios(c *gin.Context) {
|
||||
pagina, _ := strconv.Atoi(c.DefaultQuery("p", "0"))
|
||||
tamano, _ := strconv.Atoi(c.DefaultQuery("s", "20"))
|
||||
|
||||
repo := r.ObtenerRepositorios()
|
||||
usuarios, total, err := repo.Usuario.ListarGlobales(pagina, tamano)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
respuesta := make([]gin.H, 0, len(usuarios))
|
||||
for _, usuario := range usuarios {
|
||||
rol := ""
|
||||
if admin, err := repo.Empresa.PlataformaAdminDe(usuario.ID); err == nil {
|
||||
rol = string(admin.Rol)
|
||||
}
|
||||
empresas, _ := repo.Empresa.ListarDeUsuario(usuario.ID)
|
||||
respuesta = append(respuesta, gin.H{
|
||||
"id": usuario.ID,
|
||||
"email": usuario.Email,
|
||||
"name": usuario.Name,
|
||||
"activo": usuario.Activo,
|
||||
"debe_cambiar_password": usuario.DebeCambiarPassword,
|
||||
"rol_plataforma": rol,
|
||||
"empresas": len(empresas),
|
||||
"created_at": usuario.CreatedAt,
|
||||
})
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"usuarios": respuesta, "total": total})
|
||||
}
|
||||
Reference in new issue
Block a user