Let owners edit records while admins manage everything
Combine ownership rules with an admin override so ordinary users stay scoped while administrators can intervene.
This generated recipe is review-ready source material. Use it from the runtime where it appears in the sidebar and search results.
Combine ownership rules with an admin override so ordinary users stay scoped while administrators can intervene.
Use this when
- let users edit their own records
- allow admins to manage every document
- combine ownership with admin overrides
- restrict records to owners unless admin
Dyrected concepts
access, relationship, beforeChange
Additional packages: No additional packages.
Decisions and cautions
Use this recipe only when its runtime matches the project you are documenting or building. Cloud recipes must stay inside the managed content backend boundary. Self-hosted recipes may use the server runtime, database, hooks, and infrastructure you control.
Complete recipe
This is the canonical source compiled and behavior-tested by @dyrected/knowledge.
import { defineCollection, defineRelationshipField, defineTextField } from "@dyrected/core";
const ownerConstraint = (user?: { sub?: string; roles?: string[] }) => {
if (user?.roles?.includes("admin")) return true;
return user?.sub ? { owner: { equals: user.sub } } : false;
};
export const Tickets = defineCollection({
slug: "tickets",
access: {
read: ({ user }) => ownerConstraint(user),
create: ({ user }) => Boolean(user?.sub),
update: ({ user }) => ownerConstraint(user),
delete: ({ user }) => ownerConstraint(user),
},
hooks: {
beforeChange: [
({ data, operation, user }) => {
if (operation !== "create") return data;
if (!user?.sub) throw new Error("Authentication is required.");
return { ...data, owner: user.sub };
},
],
},
fields: [
defineTextField({ name: "subject", label: "Subject", required: true }),
defineRelationshipField({
name: "owner",
label: "Owner",
relationTo: "users",
required: true,
admin: { readOnly: true },
}),
],
});