Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
148 lines
3.2 KiB
Go
148 lines
3.2 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,
|
|
) {
|
|
|
|
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"`
|
|
}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&req)
|
|
if err != nil {
|
|
api.WriteError(
|
|
w,
|
|
400,
|
|
"bad_json",
|
|
err.Error(),
|
|
)
|
|
return
|
|
}
|
|
domainUUID := s.domainUUIDForRequest(r)
|
|
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
|
|
}
|
|
var revisionCount int
|
|
_ = s.store.DB.QueryRow(`SELECT COUNT(*) FROM revisions WHERE object_uuid=?`, req.ObjectUUID).Scan(&revisionCount)
|
|
capability := "write"
|
|
if revisionCount == 0 {
|
|
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.SaveRevisionContent(
|
|
req.ObjectUUID,
|
|
[]byte(req.Body),
|
|
req.Message,
|
|
req.Language,
|
|
req.ContentType,
|
|
req.Filename,
|
|
)
|
|
|
|
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,
|
|
})
|
|
}
|