45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
package middleware
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// CheckCollaboratorReadOnly verifica se um colaborador está tentando fazer operações de escrita
|
|
// Se sim, bloqueia com 403
|
|
func CheckCollaboratorReadOnly(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// Verificar agency_role do contexto
|
|
agencyRole, ok := r.Context().Value("agency_role").(string)
|
|
if !ok {
|
|
// Se não houver agency_role no contexto, é um customer, deixa passar
|
|
next.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
|
|
// Apenas colaboradores têm restrição de read-only
|
|
if agencyRole != "collaborator" {
|
|
next.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
|
|
// Verificar se é uma operação de escrita
|
|
method := r.Method
|
|
if method == http.MethodPost || method == http.MethodPut || method == http.MethodDelete {
|
|
// Verificar a rota
|
|
path := r.URL.Path
|
|
|
|
// Bloquear operações de escrita em CRM
|
|
if strings.Contains(path, "/api/crm/") {
|
|
userID, _ := r.Context().Value(UserIDKey).(string)
|
|
log.Printf("❌ COLLABORATOR WRITE BLOCKED: User %s (collaborator) tried %s %s", userID, method, path)
|
|
http.Error(w, "Colaboradores têm acesso somente leitura", http.StatusForbidden)
|
|
return
|
|
}
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|