Private
Public Access
109 lines
2.8 KiB
Go
109 lines
2.8 KiB
Go
package auth
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
|
|
r "repuestera_back/repositorios"
|
|
"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 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))
|
|
repo := r.ObtenerRepositorios()
|
|
user, err := repo.Usuario.ObtenerPorEmail(email)
|
|
if err != nil {
|
|
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": "Mail o contraseña inválidos"})
|
|
return
|
|
}
|
|
|
|
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 {
|
|
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
respuesta, err := services.ConstruirSesion(user.ID, empresaID)
|
|
if err != nil {
|
|
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
|
|
}
|
|
|
|
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 {
|
|
return 0, err
|
|
}
|
|
if len(empresas) > 0 {
|
|
return empresas[0].ID, nil
|
|
}
|
|
|
|
if services.EsPlataformaAdmin(usuarioID) {
|
|
return 0, nil
|
|
}
|
|
return 0, services.ErrSinEmpresas
|
|
}
|