Private
Public Access
fix+feat: añadido crear usuario y separado en componentes los modals
This commit is contained in:
1 parent
eff116fab0
commit
62ec25b768
14 files changed
+387
-131
No files matched your search
@@ -0,0 +1,71 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"repuestera_back/config"
|
||||
"repuestera_back/dto"
|
||||
models_seguridad "repuestera_back/models/seguridad"
|
||||
"repuestera_back/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
func CambiarContraseña(c *gin.Context) {
|
||||
var req dto.CambiarContraseñaRequest
|
||||
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 mas de 8 caracteres"})
|
||||
return
|
||||
}
|
||||
|
||||
authUserID, _ := c.Get("userID")
|
||||
authUserIDUint, _ := authUserID.(uint)
|
||||
|
||||
targetID := authUserIDUint
|
||||
if req.UserID != 0 {
|
||||
targetID = req.UserID
|
||||
}
|
||||
|
||||
esAdmin := utils.UsuarioTienePermiso(c, 3)
|
||||
|
||||
if targetID != authUserIDUint && !esAdmin {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "No tienes permiso para cambiar la contraseña de otro usuario"})
|
||||
return
|
||||
}
|
||||
|
||||
var user models_seguridad.Usuario
|
||||
if err := config.DB.First(&user, targetID).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "usuario no encontrado"})
|
||||
return
|
||||
}
|
||||
|
||||
if !esAdmin {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
if err := config.DB.Model(&user).Update("password", string(hashedPassword)).Error; 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"})
|
||||
}
|
||||
Reference in new issue
Block a user