Skip to content

Policies and security

Security is not declared in the resource. The resource only says that it is scoped (policy: { scope: "organization" }); the server resolves the where, the permissions, and the roles.

The runtime denies a resource when:

  • the resource has no policy, or
  • policies.ts has no entry for its model, or
  • scope returns null.

Two more rules matter in practice:

  • An entry without scope is treated as an empty filter, so it allows everything. Always declare scope.
  • scope must return a where object or null. Returning undefined (for example, a forgotten return) is not a denial and is easy to miss.
import { definePolicies, hasRole, type CurrentUser } from "@dashild/core";
export const policies = definePolicies<CurrentUser>({
resources: {
Customer: {
scope: (ctx) => ({ organizationId: ctx.organizationId }),
can: {
create: (ctx) => hasRole(ctx, "ADMIN", "SALES"),
update: (ctx) => hasRole(ctx, "ADMIN", "SALES"),
delete: (ctx) => hasRole(ctx, "ADMIN"),
},
},
},
});

definePolicies requires the resources container: the model map alone is not a valid definition, and doctor looks for that property when it checks policies.ts.

scope is applied to top-level Prisma operations: reads (findMany, findFirst, findUnique, count, aggregate, groupBy), writes (update, updateMany, upsert), and deletes. It is also applied to relation managers in the panel.

It does not cover:

  • create / createMany — an insert cannot be constrained by a read filter, which is why creating a scoped row requires an explicit can.create.
  • Rows loaded through an include: Prisma resolves them in the same query, so they are not filtered by the related model’s policy. Query the related model directly (or through runtime.client(user)) when that matters.

The runtime does not copy a tenant id from the scope into inserted data. Your server-side create flow must set tenant ownership from the authenticated PolicyContext (or another trusted server-side source), and must not trust a tenant id sent by the browser.

PolicyContext carries user, organizationId, and roles. The values come from organizationOf / rolesOf when those functions are defined in policies.ts; otherwise they come from user.organizationId / user.roles. It is one or the other, not a fallback chain: if organizationOf returns null, the context has null, even when user.organizationId is set.

Returning them from getCurrentUser is enough for most apps:

async function getCurrentUser() {
const session = await auth.api.getSession({ headers: await headers() });
if (!session) return null;
const user = await prisma.user.findUnique({
where: { id: session.user.id },
select: { id: true, email: true, name: true, organizationId: true, roles: true },
});
return user;
}

Use hasRole(ctx, "ADMIN", "SALES") for readable predicates. A scoped resource cannot create unless can.create is explicit, because the runtime cannot infer which organization the new row belongs to.

  • list, view, update, and delete are allowed when there is no predicate for them.
  • create is denied when the scope is not empty and there is no can.create.
  • A declared action with requires is denied unless the policy has a predicate with that name.

These are permission-predicate defaults, not a replacement for authentication, resource policy resolution, input validation, or the tenant scope. The scope limits which existing rows can be found or changed; it does not choose the organization for a newly inserted row.

Actions can require a permission declared here:

table: {
actions: [{ name: "refund", requires: "refund" }],
}
Customer: {
scope: (ctx) => ({ organizationId: ctx.organizationId }),
can: { refund: (ctx) => hasRole(ctx, "ADMIN") },
}

The UI hides actions whose requires is false, and the server also denies them.

withDashildScope returns a Prisma extension with the active scope applied; the runtime mounts it with prisma.$extends. It lives in the server entry point:

import { withDashildScope } from "@dashild/core/server";

For custom server code, prefer runtime.client(user):

const client = runtime.client(user) as typeof prisma;
const customers = await client.customer.findMany();

runtime.client is typed as unknown so @dashild/core does not depend on your generated Prisma client; cast it once, as above. If your client does not support $extends, the runtime returns the plain Prisma client.

For a direct extension:

const client = prisma.$extends(
withDashildScope({ resources, policies, user }),
);

The resource file never contains the where; that is the point.

  • policies.ts is server-only by convention: nothing enforces it at build time, so never import it from a client component.
  • The client cannot influence the policy: server actions resolve it themselves.
  • Roles are strings; the app decides their names and how they are assigned.