Litestone guide / Access
Gates
A gate is a floor on the caller: the minimum level on a 0 to 9 ladder that a model requires for each of read, create, update and delete. It is answered from the session alone, before any SQL runs.
model User { id Int (4) }
model Report {
id Int (autoincrement())
title String
(2.4.4.6)
}
const reader = db.$setAuth({ id: 1, role: 'user', verifiedAt: null }) // signed in, not verified: VISITOR (1)
const member = db.$setAuth({ id: 2, role: 'user' }) // USER (4)
await member.report.create({ data: { title: 'Q1' } })
try { await reader.report.findMany(); throw new Error('a visitor read the reports') } catch (e) { if (e.name !== 'AccessDeniedError') throw e }
const rows = await member.report.findMany()
if (rows.length !== 1) throw new Error('a member should read')
Four positions are Read.Create.Update.Delete, and a position left off takes the one before it, so @@gate(4) is 4.4.4.4.
What else it can do
- Levels run
STRANGER0,VISITOR1,READER2,CREATOR3,USER4,ADMINISTRATOR5,OWNER6,SYSADMIN7,SYSTEM8 andLOCKED9 @@gate(0)says a model is public on purpose- Once a schema declares any access rule, a model with no gate and no
@@allowor@@denyis system-only - A declared gate installs the standard level resolver itself, so none is silently inert
GatePlugin({ getLevel })replaces the resolver with your own, answered synchronously- A standing the app does not model is absent and is no objection;
nullmeans not yet reached and grades down 8meansasSystem()and nothing else, and9is not passed by anything- A gate refuses the whole query, and names the model, even four
includes deep - A row policy reads the same grade as
auth().level
In depth: Access control § Gates, § Levels, § The default resolver, § @@gate syntax