170 lines
4.3 KiB
Go
170 lines
4.3 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
|
|
"pageswiki/internal/api"
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
func (s *Server) handleCreateObjectAPI(
|
|
w http.ResponseWriter,
|
|
r *http.Request,
|
|
) {
|
|
|
|
var req struct {
|
|
Kind string `json:"kind"`
|
|
Title string `json:"title"`
|
|
Body string `json:"body"`
|
|
}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&req)
|
|
if err != nil {
|
|
api.WriteError(
|
|
w,
|
|
400,
|
|
"bad_json",
|
|
err.Error(),
|
|
)
|
|
return
|
|
}
|
|
|
|
id, err := s.store.CreateObjectInDomain(s.domainUUIDForRequest(r), req.Kind)
|
|
|
|
if err != nil {
|
|
api.WriteError(
|
|
w,
|
|
500,
|
|
"create_failed",
|
|
err.Error(),
|
|
)
|
|
return
|
|
}
|
|
|
|
_ = json.NewEncoder(w).Encode(map[string]any{
|
|
"uuid": id,
|
|
})
|
|
}
|
|
|
|
func (s *Server) handleListObjectsAPI(
|
|
w http.ResponseWriter,
|
|
r *http.Request,
|
|
) {
|
|
s.serveObjectMetadataList(w, r)
|
|
}
|
|
|
|
func (s *Server) handleSaveRevisionAPI(
|
|
w http.ResponseWriter,
|
|
r *http.Request,
|
|
) {
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
w.Header().Set("Cache-Control", "private, no-store")
|
|
if r.Method != http.MethodPost {
|
|
w.Header().Set("Allow", "POST")
|
|
api.WriteError(w, 405, "method_not_allowed", "POST required")
|
|
return
|
|
}
|
|
|
|
var req struct {
|
|
ObjectUUID string `json:"object_uuid"`
|
|
Body string `json:"body"`
|
|
Message string `json:"message"`
|
|
Language string `json:"lang"`
|
|
ContentType string `json:"content_type"`
|
|
Filename string `json:"filename"`
|
|
BaseRevision *string `json:"base_revision_uuid"`
|
|
EditorPrincipal *string `json:"editor_principal_uuid"`
|
|
}
|
|
|
|
err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 4<<20)).Decode(&req)
|
|
if err != nil {
|
|
api.WriteError(
|
|
w,
|
|
400,
|
|
"bad_json",
|
|
err.Error(),
|
|
)
|
|
return
|
|
}
|
|
domainUUID := s.domainUUIDForRequest(r)
|
|
actorUUID := ""
|
|
if req.EditorPrincipal != nil {
|
|
principal, e := s.store.PrincipalForSession(domainUUID, s.authTokenFromRequest(r))
|
|
id := ""
|
|
if e == nil && principal != nil {
|
|
id = principal.UUID
|
|
}
|
|
if id != *req.EditorPrincipal {
|
|
api.WriteError(w, 401, "editor_identity_changed", "editor identity changed; reopen with the original account")
|
|
return
|
|
}
|
|
}
|
|
if mode, _ := s.store.GetConfigInDomain(domainUUID, "authorization_mode"); mode == "enforcing" {
|
|
principal, authErr := s.store.PrincipalForSession(domainUUID, s.authTokenFromRequest(r))
|
|
if authErr != nil {
|
|
api.WriteError(w, http.StatusUnauthorized, "authentication_required", "authentication required")
|
|
return
|
|
}
|
|
actorUUID = principal.UUID
|
|
hasRevision, revisionErr := s.store.ObjectHasRevision(r.Context(), req.ObjectUUID)
|
|
if revisionErr != nil {
|
|
api.WriteError(w, 500, "storage_unavailable", "revision state unavailable")
|
|
return
|
|
}
|
|
capability := "write"
|
|
if !hasRevision {
|
|
capability = "create"
|
|
}
|
|
allowed := false
|
|
links, _ := s.store.ListPathLinksInDomain(domainUUID, req.ObjectUUID)
|
|
for _, link := range links {
|
|
if s.store.Authorize(store.AuthorizationRequest{DomainUUID: domainUUID, PrincipalUUID: principal.UUID, Capability: capability, TargetType: "path", TargetID: link.Path}) == nil {
|
|
allowed = true
|
|
break
|
|
}
|
|
}
|
|
if !allowed && s.authorizePrincipalObject(domainUUID, principal.UUID, capability, req.ObjectUUID) == nil {
|
|
allowed = true
|
|
}
|
|
if !allowed {
|
|
api.WriteError(w, http.StatusForbidden, "forbidden", "forbidden")
|
|
return
|
|
}
|
|
}
|
|
|
|
revision, err := s.store.SaveRevisionContentInDomain(r.Context(), domainUUID, actorUUID, req.ObjectUUID, []byte(req.Body), req.Message, req.Language, req.ContentType, req.Filename, req.BaseRevision)
|
|
if errors.Is(err, store.ErrRevisionConflict) {
|
|
api.WriteError(w, http.StatusConflict, "revision_conflict", "document changed; reload the language head before saving")
|
|
return
|
|
}
|
|
|
|
if err != nil {
|
|
status := http.StatusInternalServerError
|
|
if errors.Is(err, store.ErrInvalidRevisionLanguage) ||
|
|
errors.Is(err, store.ErrPageNotFound) ||
|
|
errors.Is(err, store.ErrEmbeddedAssetUnavailable) {
|
|
status = http.StatusBadRequest
|
|
}
|
|
|
|
api.WriteError(
|
|
w,
|
|
status,
|
|
"save_failed",
|
|
err.Error(),
|
|
)
|
|
return
|
|
}
|
|
|
|
_ = json.NewEncoder(w).Encode(map[string]any{
|
|
"status": "ok",
|
|
"revision_uuid": revision.UUID,
|
|
"document_hash": revision.DocumentHash,
|
|
"lang": revision.Language,
|
|
"content_type": revision.ContentType,
|
|
"filename": revision.Filename,
|
|
"body": revision.Body,
|
|
})
|
|
}
|