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
@@ -4,66 +4,105 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"repuestera_back/dto"
|
||||
"repuestera_back/jwtutils"
|
||||
r "repuestera_back/repositorios"
|
||||
"repuestera_back/utils"
|
||||
"repuestera_back/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
type loginRequest struct {
|
||||
Email string `json:"email" binding:"required,email"`
|
||||
Password string `json:"password" binding:"required"`
|
||||
EmpresaID uint `json:"empresa_id"`
|
||||
EmpresaSlug string `json:"empresa_slug"`
|
||||
}
|
||||
|
||||
func Login(c *gin.Context) {
|
||||
var req struct {
|
||||
Email string `json:"email" binding:"required,email"`
|
||||
Password string `json:"password" binding:"required"`
|
||||
}
|
||||
var req loginRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
email := strings.ToLower(strings.TrimSpace(req.Email))
|
||||
user, err := r.ObtenerRepositorios().Usuario.ObtenerPorEmail(email)
|
||||
repo := r.ObtenerRepositorios()
|
||||
user, err := repo.Usuario.ObtenerPorEmail(email)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Mail Invalido"})
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Mail o contraseña inválidos"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(req.Password)); err != nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Contrasena invalida"})
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Mail o contraseña inválidos"})
|
||||
return
|
||||
}
|
||||
|
||||
usuarioConPermisos, err := r.ObtenerRepositorios().Usuario.PrecargarGruposPermisosPorID(user.ID)
|
||||
if !user.Activo {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "La cuenta está deshabilitada"})
|
||||
return
|
||||
}
|
||||
|
||||
empresaID, err := seleccionarEmpresa(user.ID, req.EmpresaID, req.EmpresaSlug)
|
||||
if err != nil {
|
||||
log.Printf("login: no se pudieron cargar los permisos del usuario %d: %v", user.ID, err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "No se pudieron cargar los permisos del usuario"})
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
permisos := utils.PermisosDeUsuario(usuarioConPermisos)
|
||||
|
||||
accessToken, err := jwtutils.GenerarToken(user.ID, user.Email, user.Name, jwtutils.TokenTypeAccess, permisos, 15*time.Minute)
|
||||
respuesta, err := services.ConstruirSesion(user.ID, empresaID)
|
||||
if err != nil {
|
||||
log.Printf("login: no se pudo generar el access token del usuario %d: %v", user.ID, err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to generate token"})
|
||||
status := http.StatusForbidden
|
||||
if err == services.ErrEmpresaInactiva {
|
||||
status = http.StatusForbidden
|
||||
}
|
||||
if err == services.ErrSinEmpresas || err == services.ErrSinAcceso {
|
||||
status = http.StatusForbidden
|
||||
}
|
||||
if err != services.ErrSinAcceso && err != services.ErrSinEmpresas && err != services.ErrEmpresaInactiva && err != services.ErrUsuarioInactivo {
|
||||
log.Printf("login: error construyendo sesión del usuario %d: %v", user.ID, err)
|
||||
status = http.StatusInternalServerError
|
||||
}
|
||||
c.JSON(status, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
refreshToken, err := jwtutils.GenerarToken(user.ID, user.Email, user.Name, jwtutils.TokenTypeRefresh, permisos, 24*time.Hour)
|
||||
c.JSON(http.StatusOK, respuesta)
|
||||
}
|
||||
|
||||
func seleccionarEmpresa(usuarioID, empresaID uint, slug string) (uint, error) {
|
||||
repo := r.ObtenerRepositorios()
|
||||
|
||||
if empresaID != 0 {
|
||||
membresia, err := repo.Empresa.Membresia(empresaID, usuarioID)
|
||||
if err != nil || !membresia.Activo {
|
||||
return 0, services.ErrSinAcceso
|
||||
}
|
||||
return empresaID, nil
|
||||
}
|
||||
|
||||
if slug != "" {
|
||||
empresa, err := repo.Empresa.ObtenerPorSlug(strings.TrimSpace(slug))
|
||||
if err != nil {
|
||||
return 0, services.ErrSinAcceso
|
||||
}
|
||||
membresia, err := repo.Empresa.Membresia(empresa.ID, usuarioID)
|
||||
if err != nil || !membresia.Activo {
|
||||
return 0, services.ErrSinAcceso
|
||||
}
|
||||
return empresa.ID, nil
|
||||
}
|
||||
|
||||
empresas, err := services.ListarEmpresasDeUsuario(usuarioID)
|
||||
if err != nil {
|
||||
log.Printf("login: no se pudo generar el refresh token del usuario %d: %v", user.ID, err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to generate refresh token"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, dto.TokenResponse{
|
||||
AccessToken: accessToken,
|
||||
RefreshToken: refreshToken,
|
||||
TokenType: "Bearer",
|
||||
ExpiresIn: 900,
|
||||
Permissions: permisos,
|
||||
})
|
||||
return 0, err
|
||||
}
|
||||
if len(empresas) > 0 {
|
||||
return empresas[0].ID, nil
|
||||
}
|
||||
|
||||
if services.EsPlataformaAdmin(usuarioID) {
|
||||
return 0, nil
|
||||
}
|
||||
return 0, services.ErrSinEmpresas
|
||||
}
|
||||
Reference in new issue
Block a user