A change that may match nothing
Policy checks a change on each record it changes. A change to every
listed record that matches nothing changes nothing, so nothing is checked:
the action succeeds, writes nothing and says so. A caller who may not
make the change still can't make it to any record, and learns nothing
they couldn't read. So a bulk change needs no permission check of its
own. A role check written by hand is refused: the role would decide what
is stored, and is proved like a stored value
(policy.output_field_unproved).
Here archiving several of a company's projects at once is one query and
a loop of updates. A viewer who lists a project gets 403 and nothing is
archived, since the action is all-or-nothing. A viewer who lists none, or
only projects that aren't there, gets {"archived": 0}:
use work/entities { Company, Project }/// The listed projects that are in the company and not yet archived.query listed_projects(company_id: Company.id, project_ids: List<Project.id>) -> List<Project> { from: Project where: Project.company_id == company_id where: Project.id in project_ids where: Project.archived == false order: [Project.name asc] limit: 100}
use work/entities { Company, Project }use work/types { ArchivedCount, ProjectsToArchive }use work/queries { listed_projects }/// Archives each listed project of the company that isn't archived yet.action archive_projects(company_id: Company.id, input: ProjectsToArchive) -> ArchivedCount { var found: List<Project> = listed_projects(company_id, input.project_ids) for project in found { Project.update(project) { set: { archived: true } } } return ArchivedCount { archived: list.count(found) }}
use work/entities { Company, CompanyMembership, Member, Project }use work/types { ArchivedCount, CompanyRole, ProjectsToArchive }use work/actions { add_member, found_company, register }use work/queries { listed_projects }test "each project archived is checked, and an archive of none checks nothing" { var ann: Member = attempt register("ann") var bob: Member = attempt register("bob") var northwind: Company = found_company("Northwind", Principal.user(ann)) var viewer: CompanyMembership = attempt add_member(northwind.id, bob.id, CompanyRole.viewer) assert viewer.role == CompanyRole.viewer var launch: Project = make.Project { company_id: northwind.id name: "Launch" archived: false customer_id: none } var by_bob: Response = request { method: HttpMethod.post path: "/projects/{company_id}/archive" params: { company_id: northwind.id } as: Principal.user(bob) input: ProjectsToArchive { project_ids: [launch.id] } } assert by_bob.status == 403 assert list.count(listed_projects(northwind.id, [launch.id])) == 1 var none_by_bob: Response = request { method: HttpMethod.post path: "/projects/{company_id}/archive" params: { company_id: northwind.id } as: Principal.user(bob) input: ProjectsToArchive { project_ids: [] } } assert none_by_bob.status == 200 assert none_by_bob.output == ArchivedCount { archived: 0 } var by_ann: Response = request { method: HttpMethod.post path: "/projects/{company_id}/archive" params: { company_id: northwind.id } as: Principal.user(ann) input: ProjectsToArchive { project_ids: [launch.id] } } assert by_ann.output == ArchivedCount { archived: 1 }}
Markdown for agents: /docs/a-change-that-may-match-nothing.md.Built from Jadpo's main branch at commit 9080dc3.