Private
Public Access
93 lines
2.8 KiB
Go
93 lines
2.8 KiB
Go
package auth
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
models_seguridad "repuestera_back/models/seguridad"
|
|
r "repuestera_back/repositorios"
|
|
"repuestera_back/utils"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
func CambiarContraseña(c *gin.Context) {
|
|
var req struct {
|
|
UserID uint `json:"user_id"`
|
|
OldPassword string `json:"old_password"`
|
|
NewPassword string `json:"new_password" binding:"required"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
if len(req.NewPassword) < 8 {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "La nueva contraseña debe tener al menos 8 caracteres"})
|
|
return
|
|
}
|
|
|
|
authUserID, _ := c.Get("userID")
|
|
authUserIDUint, _ := authUserID.(uint)
|
|
|
|
targetID := authUserIDUint
|
|
esPropia := req.UserID == 0 || req.UserID == authUserIDUint
|
|
if !esPropia {
|
|
targetID = req.UserID
|
|
}
|
|
|
|
repo := r.ObtenerRepositorios()
|
|
user, err := repo.Usuario.ObtenerPorID(targetID)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Usuario no encontrado"})
|
|
return
|
|
}
|
|
|
|
// Cambiar la contraseña propia siempre exige la contraseña actual.
|
|
if esPropia {
|
|
if req.OldPassword == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Se requiere la contraseña actual"})
|
|
return
|
|
}
|
|
if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(req.OldPassword)); err != nil {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Contraseña actual incorrecta"})
|
|
return
|
|
}
|
|
} else {
|
|
empresaID := utils.EmpresaID(c)
|
|
rolCaller := models_seguridad.RolEmpresa(utils.EmpresaRol(c))
|
|
|
|
esAdmin := utils.UsuarioTienePermiso(c, 3) || rolCaller.EsAdministrador()
|
|
if !esAdmin {
|
|
c.JSON(http.StatusForbidden, gin.H{"error": "No tenés permiso para cambiar la contraseña de otro usuario"})
|
|
return
|
|
}
|
|
|
|
membresia, err := repo.Empresa.Membresia(empresaID, targetID)
|
|
if err != nil || !membresia.Activo {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "El usuario no pertenece a esta empresa"})
|
|
return
|
|
}
|
|
|
|
// Solo un owner (o plataforma) puede resetear a otro administrador.
|
|
if !esPropia && rolCaller != models_seguridad.RolOwner && membresia.Rol.EsAdministrador() {
|
|
c.JSON(http.StatusForbidden, gin.H{"error": "Solo el owner puede cambiar la contraseña de un administrador"})
|
|
return
|
|
}
|
|
}
|
|
|
|
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.NewPassword), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error al procesar la contraseña"})
|
|
return
|
|
}
|
|
|
|
// Un reset administrativo obliga al usuario a definir su propia clave.
|
|
if err := repo.Usuario.ActualizarContraseña(user.ID, string(hashedPassword), !esPropia); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error al actualizar la contraseña"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "Contraseña actualizada exitosamente"})
|
|
}
|