130 lines
5.0 KiB
Go
130 lines
5.0 KiB
Go
package server
|
|
|
|
import (
|
|
"database/sql"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
func cityGovernanceRoute(path string) bool {
|
|
if !strings.HasPrefix(path, "/_/api/v1/city/") {
|
|
return false
|
|
}
|
|
p := strings.Split(strings.TrimPrefix(path, "/_/api/v1/city/"), "/")
|
|
return (len(p) == 3 || (len(p) == 4 && (p[3] == "preview" || p[3] == "members" || p[3] == "access" || p[3] == "publication" || p[3] == "auto-publication"))) && (p[0] == "zones" || p[0] == "spots" || p[0] == "domains") && p[1] != "" && p[2] == "governance"
|
|
}
|
|
|
|
func (s *Server) handleCityGovernance(w http.ResponseWriter, r *http.Request) {
|
|
privateCityResponse(w)
|
|
p := strings.Split(strings.TrimPrefix(r.URL.Path, "/_/api/v1/city/"), "/")
|
|
preview := len(p) == 4 && p[3] == "preview"
|
|
members := len(p) == 4 && p[3] == "members"
|
|
access := len(p) == 4 && p[3] == "access"
|
|
publication := len(p) == 4 && p[3] == "publication"
|
|
automatic := len(p) == 4 && p[3] == "auto-publication"
|
|
if !((len(p) == 3 || access || publication || automatic) && (r.Method == "GET" || r.Method == "PATCH")) && !(preview && r.Method == "POST") && !(members && r.Method == "GET") {
|
|
writeEventJSON(w, 405, map[string]string{"error": "Method not allowed"})
|
|
return
|
|
}
|
|
principal := s.requestPrincipal(r)
|
|
if principal == nil {
|
|
writeEventJSON(w, 403, map[string]string{"error": translate(s.getLocale(s.domainUUIDForRequest(r)), "city.governance.denied")})
|
|
return
|
|
}
|
|
domain := s.domainUUIDForRequest(r)
|
|
kind := map[string]string{"spots": "spot", "domains": "domain"}[p[0]]
|
|
var err error
|
|
if p[0] == "zones" {
|
|
kind, err = s.store.CityZoneKind(r.Context(), domain, p[1])
|
|
}
|
|
var result any
|
|
if err == nil && automatic {
|
|
var change *store.AutoPublicationChange
|
|
if r.Method == "PATCH" {
|
|
change = &store.AutoPublicationChange{}
|
|
decoder := json.NewDecoder(http.MaxBytesReader(w, r.Body, 4096))
|
|
decoder.DisallowUnknownFields()
|
|
if decoder.Decode(change) != nil || decoder.Decode(new(any)) != io.EOF {
|
|
err = store.ErrGovernanceInvalid
|
|
}
|
|
}
|
|
if err == nil {
|
|
result, err = s.store.AutoPublication(r.Context(), domain, principal.UUID, kind, p[1], change)
|
|
}
|
|
} else if err == nil && publication {
|
|
var change *store.PublicationChange
|
|
if r.Method == "PATCH" {
|
|
change = &store.PublicationChange{}
|
|
decoder := json.NewDecoder(http.MaxBytesReader(w, r.Body, 4096))
|
|
decoder.DisallowUnknownFields()
|
|
if decoder.Decode(change) != nil || decoder.Decode(new(any)) != io.EOF {
|
|
err = store.ErrGovernanceInvalid
|
|
}
|
|
}
|
|
if err == nil {
|
|
result, err = s.store.LocalityPublication(r.Context(), domain, principal.UUID, kind, p[1], change)
|
|
}
|
|
} else if err == nil && access {
|
|
var change *store.LocalityAccess
|
|
if r.Method == "PATCH" {
|
|
change = &store.LocalityAccess{}
|
|
decoder := json.NewDecoder(http.MaxBytesReader(w, r.Body, 32<<10))
|
|
decoder.DisallowUnknownFields()
|
|
if decoder.Decode(change) != nil || decoder.Decode(new(any)) != io.EOF {
|
|
err = store.ErrGovernanceInvalid
|
|
}
|
|
}
|
|
if err == nil {
|
|
result, err = s.store.LocalityAccess(r.Context(), domain, principal.UUID, kind, p[1], change)
|
|
}
|
|
} else if err == nil && members {
|
|
result, err = s.store.CityGovernanceMembers(r.Context(), domain, principal.UUID, kind, p[1], r.URL.Query().Get("q"))
|
|
} else if err == nil && r.Method == "GET" {
|
|
result, err = s.store.CityGovernance(r.Context(), domain, principal.UUID, kind, p[1])
|
|
} else if err == nil {
|
|
var change store.CityGovernanceChange
|
|
decoder := json.NewDecoder(http.MaxBytesReader(w, r.Body, 16<<10))
|
|
decoder.DisallowUnknownFields()
|
|
if decoder.Decode(&change) != nil || decoder.Decode(new(any)) != io.EOF {
|
|
err = store.ErrGovernanceInvalid
|
|
} else {
|
|
result, err = s.store.ChangeCityGovernance(r.Context(), domain, principal.UUID, kind, p[1], change, preview)
|
|
}
|
|
}
|
|
if err != nil {
|
|
status, key := 503, "city.governance.unavailable"
|
|
switch {
|
|
case errors.Is(err, sql.ErrNoRows), errors.Is(err, store.ErrAccessDenied):
|
|
status, key = 403, "city.governance.denied"
|
|
case errors.Is(err, store.ErrGovernanceConflict):
|
|
status, key = 409, err.Error()
|
|
case errors.Is(err, store.ErrLocalityAccessEnforcing), errors.Is(err, store.ErrLocalityAccessAlias), errors.Is(err, store.ErrPublicationImpact), errors.Is(err, store.ErrGovernanceCeiling), errors.Is(err, store.ErrGovernanceConfirm), errors.Is(err, store.ErrGovernanceInvalid):
|
|
status, key = 400, err.Error()
|
|
}
|
|
writeEventJSON(w, status, map[string]string{"error": translate(s.getLocale(domain), key)})
|
|
return
|
|
}
|
|
writeEventJSON(w, 200, result)
|
|
}
|
|
|
|
func governanceEndpoint(kind, id string) string {
|
|
collection := map[string]string{"quartier": "zones", "street": "zones", "spot": "spots", "domain": "domains"}[kind]
|
|
return "/_/api/v1/city/" + collection + "/" + id + "/governance"
|
|
}
|
|
|
|
func (s *Server) governanceForUI(r *http.Request, kind, id string) string {
|
|
p := s.requestPrincipal(r)
|
|
if p == nil {
|
|
return ""
|
|
}
|
|
if _, err := s.store.CityGovernance(r.Context(), s.domainUUIDForRequest(r), p.UUID, kind, id); err != nil {
|
|
return ""
|
|
}
|
|
return governanceEndpoint(kind, id)
|
|
}
|