Fields shown by role
Policy filters records by their read: grants, so a list can carry
something only some callers may see. Keep that part on a record of its
own whose read: names only them, read it with a query of its own, and
give the output a field T? for it: everyone who may see the record gets
it, and everyone else gets none. Here a company's projects are listed
to all its members, and each project's customer, with their email, only to
owners and editors.
The Company, its CompanyRole and the memberships that hold roles are as
in Policy, end to end. A customer is a record of its
own, which viewers may not read at all:
use work/entities { Company }use work/types { CompanyRole, CustomerName }/// Who a project is for. Only a company's owners and editors see its/// customers.entity Customer { id: Uuid company_id: Company.id { on_delete: OnDelete.cascade } name: CustomerName email: Email policy: { create: [CompanyRole.owner, CompanyRole.editor] read: [CompanyRole.owner, CompanyRole.editor] }}
The row holds the customer's fields as T?, so it is one shape for
everyone, as every output is:
use work/entities { Customer, Project }/// A project in a company's list. Its customer's name and email are there/// only for those who may see the company's customers, and `none` for/// everyone else.type ProjectRow = Output { pick: [Project.id, Project.name] customer_name: Customer.name? contact_email: Customer.email?}
The customers come from a query of their own. For a viewer, policy keeps every customer out of it, so it gives an empty list rather than failing:
use work/entities { Company, Customer }use work/types { CustomerCard }/// The company's customers the caller may see: none for a viewer.query customers_of(company_id: Company.id) -> List<CustomerCard> { from: Customer where: Customer.company_id == company_id order: [Customer.name asc] limit: every}
The action puts the two together, by each project's customer_id. It
never asks for the caller's role: what policy let the caller read decides
what each row holds.
use work/entities { Company, Customer }use work/types { CustomerCard, ProjectRow, ProjectSource }use work/queries { customers_of, projects_of }/// A company's projects, each with its customer for those who may see it.action project_list(company_id: Company.id) -> List<ProjectRow> { var projects: List<ProjectSource> = projects_of(company_id) var customers: List<CustomerCard> = customers_of(company_id) var mut by_id: Map<Customer.id, CustomerCard> = [] for customer in customers { by_id = map.with(by_id, customer.id, customer) } var mut rows: List<ProjectRow> = [] for project in projects { var customer: CustomerCard? = match project.customer_id { some(customer_id) -> map.get(by_id, customer_id) none -> none } rows = list.append( rows, ProjectRow { id: project.id name: project.name customer_name: match customer { some(found) -> found.name none -> none } contact_email: match customer { some(found) -> found.email none -> none } } ) } return rows}
use work/entities { Company, CompanyMembership, Customer, Member, Project }use work/types { CompanyRole, ProjectRow }use work/actions { add_member, found_company, register }test "a viewer sees each project without its customer" { 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 first_customer: Customer = make.Customer { company_id: northwind.id name: "Acme" email: "buyer@first_customer.example" } var launch: Project = make.Project { company_id: northwind.id name: "Launch" archived: false customer_id: first_customer.id } var for_ann: Response = request { method: HttpMethod.get path: "/projects/{company_id}" params: { company_id: northwind.id } as: Principal.user(ann) } assert for_ann.output == [ ProjectRow { id: launch.id name: "Launch" customer_name: "Acme" contact_email: "buyer@first_customer.example" } ] var for_bob: Response = request { method: HttpMethod.get path: "/projects/{company_id}" params: { company_id: northwind.id } as: Principal.user(bob) } assert for_bob.output == [ ProjectRow { id: launch.id name: "Launch" customer_name: none contact_email: none } ]}
A field restricted on the project itself, with its own policy:,
can't be shown this way: a query whose callers include someone who may
not read one of its output fields doesn't compile
(policy.output_field_unproved). Put what only some roles see on a
record of its own, as Customer is here.
Markdown for agents: /docs/fields-shown-by-role.md.Built from Jadpo's main branch at commit 9080dc3.