Private
Public Access
108 lines
2.9 KiB
Go
108 lines
2.9 KiB
Go
package permission
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"repuestera_back/config"
|
|
"repuestera_back/dto"
|
|
"repuestera_back/models"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func AsignarPermisoAGrupo(c *gin.Context) {
|
|
var req dto.GrupoPermisoRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
grupoID := c.Param("id")
|
|
var grupo models.Grupo
|
|
if err := config.DB.First(&grupo, grupoID).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Grupo no encontrado"})
|
|
return
|
|
}
|
|
|
|
var permiso models.Permiso
|
|
if err := config.DB.First(&permiso, req.PermissionID).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Permiso no encontrado"})
|
|
return
|
|
}
|
|
|
|
config.DB.Model(&grupo).Association("Permisos").Append(&permiso)
|
|
c.JSON(http.StatusOK, gin.H{"message": "Permiso asignado al grupo"})
|
|
}
|
|
|
|
func QuitarPermisoAGrupo(c *gin.Context) {
|
|
var req dto.GrupoPermisoRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
grupoID := c.Param("id")
|
|
var grupo models.Grupo
|
|
if err := config.DB.First(&grupo, grupoID).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Grupo no encontrado"})
|
|
return
|
|
}
|
|
|
|
var permiso models.Permiso
|
|
if err := config.DB.First(&permiso, req.PermissionID).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Permiso no encontrado"})
|
|
return
|
|
}
|
|
|
|
config.DB.Model(&grupo).Association("Permisos").Delete(&permiso)
|
|
c.JSON(http.StatusOK, gin.H{"message": "Permiso quitado del grupo"})
|
|
}
|
|
|
|
func AgregarSubGrupo(c *gin.Context) {
|
|
var req dto.GrupoHijoRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
padreID := c.Param("id")
|
|
var padre models.Grupo
|
|
if err := config.DB.First(&padre, padreID).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Grupo padre no encontrado"})
|
|
return
|
|
}
|
|
|
|
var hijo models.Grupo
|
|
if err := config.DB.First(&hijo, req.ChildGroupID).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Grupo hijo no encontrado"})
|
|
return
|
|
}
|
|
|
|
config.DB.Model(&padre).Association("Grupos").Append(&hijo)
|
|
c.JSON(http.StatusOK, gin.H{"message": "Subgrupo agregado"})
|
|
}
|
|
|
|
func QuitarSubGrupo(c *gin.Context) {
|
|
var req dto.GrupoHijoRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
padreID := c.Param("id")
|
|
var padre models.Grupo
|
|
if err := config.DB.First(&padre, padreID).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Grupo padre no encontrado"})
|
|
return
|
|
}
|
|
|
|
var hijo models.Grupo
|
|
if err := config.DB.First(&hijo, req.ChildGroupID).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Grupo hijo no encontrado"})
|
|
return
|
|
}
|
|
|
|
config.DB.Model(&padre).Association("Grupos").Delete(&hijo)
|
|
c.JSON(http.StatusOK, gin.H{"message": "Subgrupo quitado"})
|
|
}
|