Files
PiCloud/examples/cms-poc/scripts/posts/post_update.rhai
MechaCat02 813bc46640
Some checks failed
CI / Rust — fmt, clippy, test (push) Failing after 39m53s
CI / Dashboard — check (push) Successful in 10m15s
docs(examples): add the CMS proof-of-concept dogfooding artifact
A WordPress-inspired CMS whose entire backend is PiCloud Rhai scripts
deployed with pic apply, plus a SvelteKit frontend. Built to dogfood the
project tool, CLI, and SDK; exercises docs/kv/files/users, docs+queue+cron
+pubsub triggers, the transactional-outbox notification chain, a docs
before-interceptor, set_if CAS, SSE, per-app CORS, env overlays, and a
durable Workflow (validate -> enrich || seo -> publish -> finalize) started
with workflow::start and polled with workflow::run_status, visualized live
with Svelte Flow.

FINDINGS.md / SECURITY.md capture every gap, surprise, and security issue
found (each tagged [PiCloud] vs [CMS]); the platform fixes for the
actionable ones ship in the preceding commit.

Also folds in the read-only `pic` allowlist entries added to
.claude/settings.json during the session (fewer-permission-prompts).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-19 20:15:29 +02:00

45 lines
1.5 KiB
Plaintext

// PUT /cms/admin/posts/:id — update a post (author+ ; own-only unless admin).
import "auth" as auth;
import "util" as util;
let g = auth::guard(ctx, ["admin", "author"]);
if !g.ok { return g.response; }
let user = g.user;
let id = ctx.request.params.id;
let posts = docs::collection("posts");
let doc = posts.get(id);
if doc == () { return #{ statusCode: 404, body: #{ error: "not found" } }; }
let cur = doc.data;
// IDOR guard: an author may edit only their OWN post; an admin edits any.
let is_admin = users::has_role(user.id, "admin");
if !is_admin && cur.author_id != user.id {
return #{ statusCode: 403, body: #{ error: "not your post" } };
}
let b = ctx.request.body;
if b.title != () { cur.title = b.title; }
if b.body_md != () { cur.body_md = b.body_md; }
if b.excerpt != () { cur.excerpt = b.excerpt; }
if b.tags != () { cur.tags = b.tags; }
let idx = kv::collection("slugs");
if b.slug != () {
let newslug = util::slugify(b.slug);
if newslug != cur.slug {
idx.delete("post:" + cur.slug);
if idx.has("post:" + newslug) { newslug = newslug + "-" + random::string(4).to_lower(); }
idx.set("post:" + newslug, id);
cur.slug = newslug;
}
}
if b.status != () && b.status != cur.status {
cur.status = b.status;
if b.status == "published" && cur.publish_at == () { cur.publish_at = time::now(); }
if b.status == "scheduled" && b.publish_at != () { cur.publish_at = b.publish_at; }
}
posts.update(id, cur);
#{ statusCode: 200, body: #{ id: id, slug: cur.slug, status: cur.status } }