Each recipe is a complete policy file that you can check and save as it stands, given the example schema and groups of the same names. Save an org policy without repo_id, and a repo policy with the repo_id of its repo. Before you save one, simulate or diff it as Testing a policy shows.
The recipes assume the org’s default Editor policy, which lets every member of the Editors group read and write records in every repo. Most recipes narrow it with denies, because a deny wins over every allow.
Read-only members
A member who only reads needs no policy: put them in the Viewers group and leave them out of Editors. Name Viewers when you invite or add them, because a member who joins in no group joins the org’s default group, which starts as Editors. Viewer reads everything that Editor shows and changes nothing.
To keep some members from changing anything even when another group gives them Editor, put them in a group and deny the writes that Editor allows. An org deny applies in every repo, and no repo policy can undo it:
import { orgPolicy, deny, group, models, repo } from "@forge/policies";
export const readOnly = orgPolicy({
name: "Read only",
description: "Members of Read only read everything that Editor shows and change nothing.",
principals: [group("Read only")],
statements: [
deny({
actions: ["create", "update", "delete", "tag", "untag"],
resource: models,
}),
deny({
actions: [
"createBranches",
"deleteBranches",
"restoreBranches",
"commit",
"discard",
"manageCheckpoints",
"createChangeRequests",
"comment",
"closeChangeRequests",
"merge",
"uploadAttachments",
"manageTags",
],
resource: repo,
}),
],
});Restrict a repo to a group
A repo that only Finance sees needs a deny on read on the repo for everyone else. The repo’s policy managers can own it as a repo policy, saved to that repo:
import { repoPolicy, deny, everyone, repo } from "@forge/policies";
export const financeOnly = repoPolicy({
name: "Finance only",
description: "Only Finance sees this repo.",
principals: [everyone],
statements: [
deny({
actions: ["read"],
resource: repo,
when: ({ user }) => !user.groups.includes("Finance"),
}),
],
});To keep the rule out of the repo’s own hands, write it as an org policy that lists the repo. The repo’s policy managers can’t change an org policy:
import { orgPolicy, deny, everyone, repo } from "@forge/policies";
export const tahoeLibraryFinanceOnly = orgPolicy({
name: "Tahoe Library is Finance's",
principals: [everyone],
repos: ["Tahoe Library"],
statements: [
deny({
actions: ["read"],
resource: repo,
when: ({ user }) => !user.groups.includes("Finance"),
}),
],
});A deny applies to everyone that it names, whatever other groups they’re in. Write the exception into the condition, as !user.groups.includes("Finance") does, rather than naming every other group.
Hide a model
To hide an extension’s model across the org, deny read on it at org level. A user who can’t read a record can’t write it either:
import { orgPolicy, deny, everyone, model } from "@forge/policies";
export const compensationIsHr = orgPolicy({
name: "Compensation is HR's",
principals: [everyone],
statements: [
deny({
description: "Outside HR, nobody sees compensation.",
actions: ["read"],
resource: model("people.hr.Compensation"),
when: ({ user }) => !user.groups.includes("HR"),
}),
],
});An org policy can’t name a model that a repo defines itself. Hide such a model with a repo policy in its repo:
import { repoPolicy, deny, group, model } from "@forge/policies";
export const noRosterForContractors = repoPolicy({
name: "No roster for contractors",
principals: [group("Contractors")],
statements: [deny({ actions: ["read"], resource: model("ProjectMember") })],
});A hidden model’s records drop out of every list, search, count, diff, and CI finding. The model itself still shows in the schema. To hide the schema too, also deny read on schema(...) with the same name.
Records that a user owns or works on
To show people only their own records, deny read on the rest. A record whose owner field is null stays hidden, because the comparison with a missing value is unknown and a deny that meets unknown matches:
import { repoPolicy, deny, everyone, model } from "@forge/policies";
export const ownInvoices = repoPolicy({
name: "Own invoices",
principals: [everyone],
statements: [
deny({
description: "Outside Finance, people see only the invoices that they submitted.",
actions: ["read"],
resource: model("integrations.procore.Invoice"),
when: ({ record: invoice, user }) =>
!user.groups.includes("Finance") && invoice.submitted_by !== user.id,
}),
],
});When membership lives in a model of its own, read it with exists. Then reach the records below with canRead, so they follow the same rule:
import { repoPolicy, deny, canRead, exists, group, model } from "@forge/policies";
export const fieldStaff = repoPolicy({
name: "Field staff",
principals: [group("Field")],
statements: [
deny({
description: "Field staff see only the projects that they manage or work on.",
actions: ["read"],
resource: model("integrations.procore.Project"),
when: ({ record: project, user }) =>
project.project_manager !== user.id &&
!exists("ProjectMember", (member) => member.project === project.id && member.user === user.id),
}),
deny({
description: "Field staff see invoices on the projects that they can read.",
actions: ["read"],
resource: model("integrations.procore.Invoice"),
when: ({ record: invoice }) => !canRead(invoice.project),
}),
],
});Separation of duties
To keep anyone from approving their own work, deny the approval when the approver submitted the invoice or doesn’t record themselves as the approver:
import { repoPolicy, deny, everyone, model } from "@forge/policies";
export const approvals = repoPolicy({
name: "Approvals",
principals: [everyone],
statements: [
deny({
description: "Nobody approves an invoice that they submitted, and an approver approves in their own name.",
actions: ["update"],
resource: model("integrations.procore.Invoice"),
when: ({ previous, next, user }) =>
previous.status !== "approved" &&
next.status === "approved" &&
(previous.submitted_by === user.id || next.approved_by !== user.id),
}),
],
});An invoice without a submitter can’t be approved under this rule, because previous.submitted_by === user.id is unknown there. To allow that case on purpose, test for it with previous.submitted_by !== null.
Lock records by status
To freeze a record once it reaches a status, deny updates and deletes when the record before the write has it. Read previous, so that the rule locks the record once it’s paid and still lets someone mark it paid:
import { orgPolicy, deny, everyone, model } from "@forge/policies";
export const guardrails = orgPolicy({
name: "Guardrails",
principals: [everyone],
statements: [
deny({
description: "Paid invoices are locked.",
actions: ["update", "delete"],
resource: model("integrations.procore.Invoice"),
when: ({ previous }) => previous.status === "paid",
}),
],
});The rule also locks an invoice without a status, because previous.status === "paid" is unknown there and a deny that meets unknown matches. When invoices can lack a status, guard the field: previous.status !== null && previous.status === "paid".
An org deny like this one binds everyone but owners, so save org denies for guardrails that nobody else crosses.
Direction of a status change
To allow only some status changes, list the moves that are allowed and deny every other change of the field. changed keeps the rule from matching updates that leave the status alone:
import { repoPolicy, changed, deny, everyone, model } from "@forge/policies";
export const invoiceStatus = repoPolicy({
name: "Invoice status",
principals: [everyone],
statements: [
deny({
description: "An invoice moves from draft to submitted to approved to paid, and an approver can send a submitted invoice back to draft.",
actions: ["update"],
resource: model("integrations.procore.Invoice"),
when: ({ previous, next, user }) =>
changed("status") &&
!(
(previous.status === "draft" && next.status === "submitted") ||
(previous.status === "submitted" && next.status === "approved") ||
(previous.status === "approved" && next.status === "paid") ||
(previous.status === "submitted" && next.status === "draft" && user.groups.includes("PMs"))
),
}),
],
});Contractors limited to one repo
Editor applies to the Editors group, and a repo policy can’t widen what the org allows, so a member of Editors works in every repo. To keep contractors to one repo, leave them out of Editors:
- Create a group, such as Contractors, and invite each contractor into it and into no other group. An invite that names a group skips the org’s default group.
- Save the two Contractors policies below. One reads the org, and the other works in the one repo. They’re separate because a policy that lists
reposcan’t nameorg.
import { orgPolicy, allow, group, org } from "@forge/policies";
export const contractorsOrg = orgPolicy({
name: "Contractors in the org",
principals: [group("Contractors")],
statements: [allow({ actions: ["read", "listRepos"], resource: org })],
});import { orgPolicy, allow, group, models, repo, schemas } from "@forge/policies";
export const contractorsHarbor = orgPolicy({
name: "Contractors in Harbor Clinic",
principals: [group("Contractors")],
repos: ["Harbor Clinic"],
statements: [
allow({
actions: ["read", "readHistory", "createBranches", "commit", "discard", "createChangeRequests", "comment"],
resource: repo,
}),
allow({ actions: ["read"], resource: schemas }),
allow({ actions: ["read", "create", "update"], resource: models }),
],
});Contractors list only the repos that they can read, so other repos stay hidden. A contractor who also joins Editors works in every repo.
Models that grant access
A model that conditions read to decide access, such as ProjectMember or a project’s project_manager field, is a privilege. Whoever can write it can grant themselves what it unlocks: a member who adds themselves to a project reads its invoices. The checker doesn’t warn about it, so deny its writes to everyone who shouldn’t hold that power:
import { repoPolicy, deny, everyone, model } from "@forge/policies";
export const projectsAndPeople = repoPolicy({
name: "Projects and people",
principals: [everyone],
statements: [
deny({
description: "Only PMs create projects, and only a project's PM changes it.",
actions: ["create", "update", "delete"],
resource: model("integrations.procore.Project"),
when: ({ record: project, user }) =>
!user.groups.includes("PMs") || project.project_manager !== user.id,
}),
deny({
description: "Only a project's PM changes who works on it.",
actions: ["create", "update", "delete"],
resource: model("ProjectMember"),
when: ({ record: member, user }) => member.project.project_manager !== user.id,
}),
],
});The first deny runs on the project before and after an update, so a PM can’t hand a project to someone else, and nobody can make themselves its PM. To let PMs reassign projects, compare previous.project_manager alone.
The same holds for groups. Joining a group grants what the group’s policies grant, so manageGroups on org is as powerful as managePolicies.
Let a group use an extension
Editor doesn’t allow calling tools or running workflows. Grant them per group, for one extension or for all:
import { orgPolicy, allow, group, tools, workflow } from "@forge/policies";
export const pmsUseProcore = orgPolicy({
name: "PMs use procore",
principals: [group("PMs")],
statements: [
allow({ actions: ["call"], resource: tools("integrations.procore") }),
allow({
actions: ["run", "signal", "readRuns"],
resource: workflow("integrations.procore/close_month"),
}),
],
});A tool runs as the user who calls it, so it reads and writes only what that user’s policies allow.
Change what an extension does on its own
An extension’s schedules, triggers, webhooks, and mailboxes run as the extension itself, and the guests of its public shares act as it too. All of them work under its built-in policy, and policies for everyone, groups, and users never apply to them. To narrow or widen what they do, name the extension as the policy’s principal. That isn’t extension(...) as a statement’s resource, which is the extension’s source in the repo that defines it.
This policy keeps integrations.procore from deleting records, and lets the guests of its public shares call one of its tools:
import { orgPolicy, allow, deny, extension, models, tool } from "@forge/policies";
export const procoreOnItsOwn = orgPolicy({
name: "Procore on its own",
principals: [extension("integrations.procore")],
statements: [
deny({
description: "The extension never deletes records.",
actions: ["delete"],
resource: models,
}),
allow({
description: "Guests of its public shares call quote.",
actions: ["call"],
resource: tool("integrations.procore/quote"),
when: ({ request }) => request.principal === "guest",
}),
],
});The deny binds the extension’s guests too, because they act as the extension. A member who calls one of its tools acts as themselves, so this policy doesn’t apply to that call. Simulations decide only for members, so check the file, save it, and then run the schedule or open the share to see it decide.
Delegate a repo’s policies
To let a group manage one repo’s policies without touching the rest of the org, allow managePolicies on repo in an org policy that lists the repo:
import { orgPolicy, allow, group, repo } from "@forge/policies";
export const harborPolicyManagers = orgPolicy({
name: "Harbor Clinic policy managers",
principals: [group("PMs")],
repos: ["Harbor Clinic"],
statements: [allow({ actions: ["managePolicies"], resource: repo })],
});They read and change Harbor Clinic’s repo policies. Because repo policies only deny, they can narrow access in the repo and never widen it past what the org allows. To simulate, explain, and diff their changes, they also need readPolicies on org (along with readMembers, which Editor already grants); only an org policy without repos grants readPolicies, and it reads every policy in the org.