diff --git a/src/app/(frontend)/wiki/[slug]/edit/page.tsx b/src/app/(frontend)/wiki/[slug]/edit/page.tsx
index 98ce5add..ed5312b8 100644
--- a/src/app/(frontend)/wiki/[slug]/edit/page.tsx
+++ b/src/app/(frontend)/wiki/[slug]/edit/page.tsx
@@ -7,7 +7,12 @@ import { ArrowLeftIcon, PencilIcon } from "lucide-react";
import { Card, CardContent } from "@/components/ui/card";
import { LockdownBanner } from "@/components/frontend/wiki/LockdownBanner";
import { WikiEditor } from "@/components/frontend/wiki/WikiEditor";
-import { listTemplates, templateMap } from "@/lib/wiki/service";
+import {
+ canViewAllDrafts,
+ draftVisibilityWhere,
+ listTemplates,
+ templateMap,
+} from "@/lib/wiki/service";
import hasRoles from "@/utils/access-control/hasRoles";
export const metadata = {
@@ -43,9 +48,12 @@ export default async function EditWikiPage({
const payload = await getPayload({ config });
const headers = await nextHeaders();
const { user } = await payload.auth({ headers, canSetHeaders: false });
+ const draftClause = draftVisibilityWhere(user, await canViewAllDrafts(payload, user));
const pageResult = await payload.find({
collection: "wiki-pages",
- where: { slug: { equals: slug } },
+ where: draftClause
+ ? { AND: [{ slug: { equals: slug } }, draftClause] }
+ : { slug: { equals: slug } },
limit: 1,
depth: 1,
overrideAccess: true,
@@ -104,6 +112,7 @@ export default async function EditWikiPage({
listTemplates(payload),
payload.find({
collection: "wiki-pages",
+ where: draftVisibilityWhere(user, await canViewAllDrafts(payload, user)) ?? {},
sort: "title",
limit: 200,
depth: 0,
@@ -152,6 +161,7 @@ export default async function EditWikiPage({
category: page.category,
tags: page.tags ?? [],
body: page.body,
+ draft: page.draft === true,
}}
templates={templateMap(templates)}
templateCssMap={templateCssMap(templates)}
diff --git a/src/app/(frontend)/wiki/[slug]/history/page.tsx b/src/app/(frontend)/wiki/[slug]/history/page.tsx
index 33e76104..f8f7a0f0 100644
--- a/src/app/(frontend)/wiki/[slug]/history/page.tsx
+++ b/src/app/(frontend)/wiki/[slug]/history/page.tsx
@@ -9,6 +9,7 @@ import { Button } from "@/components/ui/button";
import { Card, CardContent } from "@/components/ui/card";
import { RevisionList, type WikiRevisionSummary } from "@/components/frontend/wiki/RevisionList";
import { hasIntelligenceQualification } from "@/utils/access-control/hasIntelligenceQualification";
+import { canViewAllDrafts, draftVisibilityWhere } from "@/lib/wiki/service";
export const metadata = {
title: "Wiki history: Polaris Task Force",
@@ -52,9 +53,12 @@ export default async function WikiHistoryPage({
headers,
canSetHeaders: false,
});
+ const draftClause = draftVisibilityWhere(user, await canViewAllDrafts(payload, user));
const pageResult = await payload.find({
collection: "wiki-pages",
- where: { slug: { equals: slug } },
+ where: draftClause
+ ? { AND: [{ slug: { equals: slug } }, draftClause] }
+ : { slug: { equals: slug } },
limit: 1,
depth: 1,
overrideAccess: true,
diff --git a/src/app/(frontend)/wiki/[slug]/page.tsx b/src/app/(frontend)/wiki/[slug]/page.tsx
index 4066b322..50673d3c 100644
--- a/src/app/(frontend)/wiki/[slug]/page.tsx
+++ b/src/app/(frontend)/wiki/[slug]/page.tsx
@@ -8,11 +8,17 @@ import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Card, CardContent } from "@/components/ui/card";
import { LockdownBanner } from "@/components/frontend/wiki/LockdownBanner";
+import { DraftBanner } from "@/components/frontend/wiki/DraftBanner";
import { WikiPageViewMeta } from "@/components/frontend/wiki/WikiPageViewMeta";
import { WikiContent } from "@/components/frontend/wiki/WikiContent";
import { WikiToolbar } from "@/components/frontend/wiki/WikiToolbar";
import { hasIntelligenceQualification } from "@/utils/access-control/hasIntelligenceQualification";
-import { listTemplates, templateMap } from "@/lib/wiki/service";
+import {
+ canViewAllDrafts,
+ draftVisibilityWhere,
+ listTemplates,
+ templateMap,
+} from "@/lib/wiki/service";
import { prepareMarkdown } from "@/lib/wiki/prepare";
import { buildTemplateStyles } from "@/lib/wiki/templateCss";
@@ -70,9 +76,12 @@ export default async function WikiDetailPage({
canSetHeaders: false,
});
+ const draftClause = draftVisibilityWhere(user, await canViewAllDrafts(payload, user));
const pageResult = await payload.find({
collection: "wiki-pages",
- where: { slug: { equals: slug } },
+ where: draftClause
+ ? { AND: [{ slug: { equals: slug } }, draftClause] }
+ : { slug: { equals: slug } },
limit: 1,
depth: 1,
overrideAccess: true,
@@ -239,6 +248,8 @@ export default async function WikiDetailPage({
{lockdown && }
+ {page.draft && }
+
diff --git a/src/app/(frontend)/wiki/actions.ts b/src/app/(frontend)/wiki/actions.ts
index 6039ec25..89585d21 100644
--- a/src/app/(frontend)/wiki/actions.ts
+++ b/src/app/(frontend)/wiki/actions.ts
@@ -87,6 +87,7 @@ export async function createWikiPage(input: {
tags?: string[];
body: string;
summary?: string;
+ draft?: boolean;
}): Promise> {
try {
const { payload, user } = await authenticate();
@@ -134,6 +135,7 @@ export async function updateWikiPage(input: {
category?: string;
tags?: string[];
summary?: string;
+ draft?: boolean;
}): Promise> {
try {
const { payload, user } = await authenticate();
diff --git a/src/app/(frontend)/wiki/category/[category]/page.tsx b/src/app/(frontend)/wiki/category/[category]/page.tsx
index 6da35b86..2a2dc96c 100644
--- a/src/app/(frontend)/wiki/category/[category]/page.tsx
+++ b/src/app/(frontend)/wiki/category/[category]/page.tsx
@@ -3,12 +3,14 @@ import { headers as nextHeaders } from "next/headers";
import Link from "next/link";
import { notFound } from "next/navigation";
import { getPayload } from "payload";
+import type { Where } from "payload";
import { ArrowLeftIcon, BookOpenIcon } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Card, CardContent } from "@/components/ui/card";
import { WikiRow } from "@/components/frontend/wiki/WikiRow";
import { CATEGORY_META, WIKI_CATEGORIES, type WikiCategory } from "@/lib/wiki/categories";
import { collectTagCounts, mapWikiPageToEntry } from "@/lib/wiki/indexView";
+import { canViewAllDrafts, draftVisibilityWhere } from "@/lib/wiki/service";
export const metadata = {
title: "Wiki category: Polaris Task Force",
@@ -27,9 +29,16 @@ export default async function WikiCategoryPage({
const headers = await nextHeaders();
const payload = await getPayload({ config });
+ const { user } = await payload.auth({ headers, canSetHeaders: false });
+ const draftClause = draftVisibilityWhere(user, await canViewAllDrafts(payload, user));
+ const where = (
+ draftClause
+ ? { AND: [{ category: { equals: category } }, draftClause] }
+ : { category: { equals: category } }
+ ) as Where;
const pagesResult = await payload.find({
collection: "wiki-pages",
- where: { category: { equals: category } },
+ where,
sort: "-updatedAt",
limit: 200,
depth: 1,
diff --git a/src/app/(frontend)/wiki/new/page.tsx b/src/app/(frontend)/wiki/new/page.tsx
index bf7de01b..45bac05f 100644
--- a/src/app/(frontend)/wiki/new/page.tsx
+++ b/src/app/(frontend)/wiki/new/page.tsx
@@ -4,7 +4,12 @@ import { headers as nextHeaders } from "next/headers";
import { getPayload } from "payload";
import { ArrowLeftIcon, FilePlus2Icon } from "lucide-react";
import { WikiEditor } from "@/components/frontend/wiki/WikiEditor";
-import { listTemplates, templateMap } from "@/lib/wiki/service";
+import {
+ canViewAllDrafts,
+ draftVisibilityWhere,
+ listTemplates,
+ templateMap,
+} from "@/lib/wiki/service";
import hasRoles from "@/utils/access-control/hasRoles";
export const metadata = {
@@ -45,6 +50,7 @@ export default async function NewWikiPage({
listTemplates(payload),
payload.find({
collection: "wiki-pages",
+ where: draftVisibilityWhere(user, await canViewAllDrafts(payload, user)) ?? {},
sort: "title",
limit: 200,
depth: 0,
diff --git a/src/app/(frontend)/wiki/page.tsx b/src/app/(frontend)/wiki/page.tsx
index e1369675..5138cf60 100644
--- a/src/app/(frontend)/wiki/page.tsx
+++ b/src/app/(frontend)/wiki/page.tsx
@@ -5,6 +5,7 @@ import { getPayload } from "payload";
import { BookOpenIcon } from "lucide-react";
import { WikiIndex } from "@/components/frontend/wiki/WikiIndex";
import { mapWikiPageToEntry } from "@/lib/wiki/indexView";
+import { canViewAllDrafts, draftVisibilityWhere } from "@/lib/wiki/service";
import { hasIntelligenceQualification } from "@/utils/access-control/hasIntelligenceQualification";
export const metadata = {
@@ -19,9 +20,12 @@ export default async function WikiPage() {
canSetHeaders: false,
});
+ const canViewAll = await canViewAllDrafts(payload, user);
+ const where = draftVisibilityWhere(user, canViewAll);
const [pagesResult, isModerator] = await Promise.all([
payload.find({
collection: "wiki-pages",
+ where: where ?? {},
sort: "-updatedAt",
limit: 200,
depth: 1,
diff --git a/src/collections/wiki/WikiPages.ts b/src/collections/wiki/WikiPages.ts
index 379b2496..e5f04473 100644
--- a/src/collections/wiki/WikiPages.ts
+++ b/src/collections/wiki/WikiPages.ts
@@ -1,4 +1,4 @@
-import { CollectionConfig } from "payload";
+import { CollectionConfig, Where } from "payload";
import { hasIntelligenceQualification } from "@/utils/access-control/hasIntelligenceQualification";
import hasRoles from "@/utils/access-control/hasRoles";
@@ -26,7 +26,20 @@ export const WikiPages: CollectionConfig = {
pagination: { defaultLimit: 50 },
},
access: {
- read: ({ req }) => !!req.user,
+ // Return a `where` clause (not a per-document boolean) so Payload filters
+ // drafts out of `find` results for everyone except their creator, wiki
+ // moderators and admins. Payload also applies it to findByID.
+ read: async ({ req }) => {
+ if (!req.user) return false;
+ if (await hasIntelligenceQualification(req.payload, req.user)) return true;
+ if (hasRoles(["admin"], req.user)) return true;
+ return {
+ OR: [
+ { draft: { equals: false } },
+ { draft: { equals: true }, createdBy: { equals: req.user.id } },
+ ],
+ } as Where;
+ },
create: ({ req, data }) => {
if (!req.user) return false;
if (isDocumentationCategory(data?.category)) {
@@ -58,6 +71,24 @@ export const WikiPages: CollectionConfig = {
return true;
}
}
+ // One-way publish: a published page (draft=false) may never be set back
+ // to draft. A draft may be published (draft=true -> false).
+ if (data?.draft === true && typeof id === "number") {
+ try {
+ const existing = (await req.payload.findByID({
+ collection: "wiki-pages",
+ id,
+ depth: 0,
+ overrideAccess: true,
+ })) as unknown as { draft?: boolean } | null;
+ if (existing && existing.draft === false) {
+ return false;
+ }
+ } catch {
+ // Unknown id: let the operation proceed and fail validation later.
+ return true;
+ }
+ }
return true;
},
delete: async ({ req }) => {
@@ -161,5 +192,28 @@ export const WikiPages: CollectionConfig = {
},
],
},
+ {
+ name: "draft",
+ type: "checkbox",
+ defaultValue: false,
+ admin: {
+ description:
+ "Private to the author. Drafts are hidden from everyone except the creator, wiki moderators, and admins.",
+ },
+ },
+ {
+ name: "createdBy",
+ type: "relationship",
+ relationTo: "users",
+ admin: {
+ hidden: true,
+ },
+ // Authorship is set once by the service layer (overrideAccess) and never
+ // by a client, so nobody can reassign a draft's owner through the API.
+ access: {
+ create: () => false,
+ update: () => false,
+ },
+ },
],
};
diff --git a/src/components/frontend/wiki/DraftBanner.tsx b/src/components/frontend/wiki/DraftBanner.tsx
new file mode 100644
index 00000000..f1de3850
--- /dev/null
+++ b/src/components/frontend/wiki/DraftBanner.tsx
@@ -0,0 +1,19 @@
+import { FileTextIcon } from "lucide-react";
+import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
+
+/**
+ * Shown on the detail page of a draft wiki article. Drafts are private to their
+ * author (plus wiki moderators and admins), so this reminds editors they are
+ * still working in public and that others cannot see the page yet.
+ */
+export function DraftBanner() {
+ return (
+
+
+ Draft
+
+ This article is a draft and only visible to you, wiki moderators, and admins.
+
+
+ );
+}
diff --git a/src/components/frontend/wiki/WikiEditor.tsx b/src/components/frontend/wiki/WikiEditor.tsx
index 691590af..7b6eab68 100644
--- a/src/components/frontend/wiki/WikiEditor.tsx
+++ b/src/components/frontend/wiki/WikiEditor.tsx
@@ -7,6 +7,7 @@ import { toast } from "sonner";
import { cn } from "@/lib/utils";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
+import { Switch } from "@/components/ui/switch";
import { Label } from "@/components/ui/label";
import {
Select,
@@ -34,6 +35,7 @@ export interface WikiEditorInitial {
readonly category: WikiCategory;
readonly tags: readonly string[];
readonly body: string;
+ readonly draft: boolean;
}
interface WikiEditorProps {
@@ -86,6 +88,7 @@ export function WikiEditor({
const [tags, setTags] = useState(initial?.tags.join(", ") ?? "");
const [summary, setSummary] = useState("");
const [body, setBody] = useState(initial?.body ?? "");
+ const [draft, setDraft] = useState(initial?.draft ?? false);
const [pending, setPending] = useState(false);
const [linkDialogOpen, setLinkDialogOpen] = useState(false);
const [showStandards, setShowStandards] = useState(() => {
@@ -129,6 +132,7 @@ export function WikiEditor({
category,
tags: parsedTags,
body,
+ draft,
...(trimmedSummary ? { summary: trimmedSummary } : {}),
})
: initial
@@ -138,6 +142,7 @@ export function WikiEditor({
category,
tags: parsedTags,
body,
+ draft,
...(trimmedSummary ? { summary: trimmedSummary } : {}),
})
: { success: false, error: "This page could not be loaded for editing." };
@@ -225,6 +230,23 @@ export function WikiEditor({
disabled={pending}
/>
+
+
+
+
+
+ {mode === "create"
+ ? "Check to keep this page private to you until you are ready to publish it."
+ : "Uncheck to publish. A published article cannot be set back to draft."}
+