Private
Public Access
cambios en unificar los permisos de empresa con los gfenerales
Signed-off-by: fede <federico.nicolas.polidoro@gmail.com>
This commit is contained in:
1 parent
7ba7d379ec
commit
f2cf7927eb
25 files changed
+104
-557
No files matched your search
+1
-60
@@ -63,7 +63,7 @@ func SesionMiddleware() gin.HandlerFunc {
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
} else if !esPlataformaAdmin(userID) {
|
||||
} else {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "La sesión no tiene una empresa activa"})
|
||||
c.Abort()
|
||||
return
|
||||
@@ -74,28 +74,6 @@ func SesionMiddleware() gin.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// PlataformaMiddleware valida el rol transversal de plataforma. No habilita
|
||||
// por sí solo el acceso a datos de una empresa.
|
||||
func PlataformaMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
claims, userID, ok := validarAccessToken(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
if !esPlataformaAdmin(userID) {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "No tenés permisos de plataforma"})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
empresaID, _ := jwtutils.ClaimUint(claims, jwtutils.ClaimEmpresaID)
|
||||
cargarContexto(c, claims, userID, empresaID)
|
||||
c.Set(utils.ContextPlataformaAdmin, true)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// RolEmpresaMiddleware restringe una ruta a determinados roles de la empresa.
|
||||
func RolEmpresaMiddleware(roles ...models_seguridad.RolEmpresa) gin.HandlerFunc {
|
||||
permitidos := make(map[string]bool, len(roles))
|
||||
@@ -175,11 +153,6 @@ func cargarContexto(c *gin.Context, claims jwtutils.MapClaims, userID, empresaID
|
||||
if name, ok := claims["name"].(string); ok {
|
||||
c.Set("name", name)
|
||||
}
|
||||
|
||||
// Un rol de plataforma revocado en la base no debe seguir vigente hasta
|
||||
// que expire el token.
|
||||
plataforma := jwtutils.ClaimBool(claims, jwtutils.ClaimPlataformaAdmin) && esPlataformaAdmin(userID)
|
||||
c.Set(utils.ContextPlataformaAdmin, plataforma)
|
||||
}
|
||||
|
||||
func membresiaActiva(usuarioID, empresaID uint) bool {
|
||||
@@ -199,35 +172,3 @@ func membresiaActiva(usuarioID, empresaID uint) bool {
|
||||
}
|
||||
return empresa.Activa
|
||||
}
|
||||
|
||||
func esPlataformaAdmin(usuarioID uint) bool {
|
||||
var count int64
|
||||
if err := config.DB.Model(&models_seguridad.PlataformaAdmin{}).
|
||||
Where("usuario_id = ? AND activo = ?", usuarioID, true).
|
||||
Count(&count).Error; err != nil {
|
||||
return false
|
||||
}
|
||||
return count > 0
|
||||
}
|
||||
|
||||
func PermisoMiddleware(permisoID uint) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if !utils.UsuarioTienePermiso(c, permisoID) {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "No tenés permiso para realizar esta acción"})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func PermisosMiddleware(permisosIDs ...uint) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if !utils.UsuarioTieneAlgunPermiso(c, permisosIDs) {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "No tenés permiso para realizar esta acción"})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user