@quad-education/sdk 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -2
- package/dist/contract-tests/apps.d.ts +187 -0
- package/dist/contract-tests/apps.js +73 -1
- package/dist/contract-tests/harness.d.ts +1 -1
- package/dist/contract-tests/harness.js +3 -0
- package/dist/contract-tests/suite.d.ts +2 -0
- package/dist/contract-tests/suite.js +154 -2
- package/dist/src/contract/types.d.ts +7 -1
- package/dist/src/contract/types.js +1 -1
- package/dist/src/engine/engine.d.ts +24 -2
- package/dist/src/engine/engine.js +99 -13
- package/dist/src/engine/state.d.ts +5 -0
- package/dist/src/testing/index.d.ts +7 -0
- package/dist/src/testing/index.js +7 -0
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -12,8 +12,8 @@ const sdk = await connect()
|
|
|
12
12
|
const { items } = await sdk.school.students.list({ where: { yearLevel: 'Y9' } })
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
-
Modules: `auth` (who is signed in,
|
|
16
|
-
`appAdmins()`, D133), `school` (canonical school data, filtered by your scopes and the user's
|
|
15
|
+
Modules: `auth` (who is signed in, whether the school made them an app admin, and which of the
|
|
16
|
+
app's own roles they hold: `me()`, `appAdmins()`, D133, D143), `school` (canonical school data, filtered by your scopes and the user's
|
|
17
17
|
visibility), `db` (your app's own data), `notify`, `clock`. Slice B adds `tasks`,
|
|
18
18
|
`schedule`, `cron` and `audit`.
|
|
19
19
|
|
|
@@ -96,6 +96,8 @@ The types allow `orderBy` and ranges only on indexed fields, so most mistakes ar
|
|
|
96
96
|
|
|
97
97
|
## Versions
|
|
98
98
|
|
|
99
|
+
- **0.8.0 (D142, D143, 24 Nov 2026).** Two access rules and app roles (a contract change across all the `@quad-education/*` packages, which move together). The schema's rules gain `'appAdmin'` (the installation's app admins, for the `staff` key) and `listedIn('entity')` or `listedIn('entity', 'field')` (the staff, students or parents a table of the app's points at); both reach every record, and who changes that table is the app's own rules' business. `accessWarnings(schema)` says when the people a rule keeps out could add themselves, and `quad gen` prints it. `auth.me()` gains `appRoles` for a user: the app's own roles (manifest `roles`) the school gave them, in the manifest's spelling. A person's rules are those of their school role and of every app role they hold. The engine gains the platform-only `setAppRoles(appId, { personId: roles })` and `schema(appId)`, and the testing kit `appRoles` and `setAppRoles`. `SDK_VERSION`, left at `0.6.0` in 0.7.0, is `0.8.0`.
|
|
100
|
+
- **0.7.0 (D132, 30 Oct 2026).** Apps are free in design; no change to the SDK's calls.
|
|
99
101
|
- **0.6.0 (D133, 30 Oct 2026).** App admins: `auth.me()` gains `appAdmin` for a user (staff the school put in charge of the app), and `auth.appAdmins()` lists them for staff and jobs (FORBIDDEN for a student or parent). The engine gains the platform-only `setAppAdmins(appId, personIds)`, the testing kit `createTestPlatform({ appAdmins })` and `setAppAdmins`, and the contract tests the `setAppAdmins` control. A contract change; every `@quad-education/*` package moves to 0.6.0.
|
|
100
102
|
- **0.4.0 (D103, 21 Oct 2026).** A small canonical model, and no field-level coverage anywhere an app can see it (a contract change across all the `@quad-education/*` packages, which move together). The model is 13 entities and 56 fields: the foundation (`schools`, `students`, `staff`, `parents`, `parentStudentLinks`, `cohorts`, `cohortMemberships`) and the extended ones some school systems supply (`periods`, `rooms`, `lessons`, `sessionAttendance`, `medicalAlerts`, `medicalNotes`). **Removed:** `enrolments`, `courses`, `courseEnrolments`, `subjects`, `calendarEvents` and `sdk.school.calendar`, `assignments`, `submissions`, `results`, `studentAbsences`, `attendanceSummaries`, `behaviours`, `achievements`, `learningSupportNeeds`, `medicalEvents`, `employeeAbsences`, `applicants` and `pastoralNotes`; and fields: `students.preferredName` and the seven sensitive demographic fields, `staff.positionTitle` and `phone`, a parent's title, mobile, address and language, `livesWith`, `primaryContact`, `cohorts.parentCohortId` and `subjectId`, the register's `comment` and `minutesLate`, a medical alert's `severity` and `actionPlan`, and a note's `title`, `conditionId` and `eventId`. **Added:** `students.gender`, `parents.phone`, `cohorts.subject` (text, such as "English"), the `form` cohort kind, and the capability map (`CAPABILITIES`, `capabilityOf`, `capabilitiesOf`, `entityKind` in `@quad-education/canonical`). **Changed:** apps no longer declare the fields they read, so the manifest `data` block is accepted and ignored (`manifestWarnings` says to remove it), the engine strips nothing, and `core` is gone; scopes are checked directly. Ids, references and names are required and every other field is typed optional, because a school's system may not supply it. A parent's contact flags (`receivesCorrespondence`, `canConsent`, `contactRestricted`) are optional too, and where a school's system does not supply one the parent is treated as contactable (D17 amended by D103). The `lms` backing goes, and `fieldTier`, `coreFields` and `varyingFields` with it.
|
|
101
103
|
- **0.3.0 (D101, 20 Oct 2026).** Every app runs in a school, so what 0.2.0 added for apps with no school is removed (a removal across all the `@quad-education/*` packages, which move together). Gone from the main entry: the `account` and `guest` principals, `ActingAs`, the account directory and `staticDirectory`, the `account` and `guest` results of `auth.me()`, the `NOT_AVAILABLE` error code (`sdk.school` is always available: the fictional school in dev and the Sandbox, the real school in a school instance), the `{ account }` recipient, and `{ role }` taking any string (it is a school role again). Gone from `@quad-education/sdk/client`: `createHttpTransport`, `connectHttp`, the live connection (`connectLive`, `getLiveStatus`, `signIn`, `continueAsGuest`, `SignInRequired`) and the live branch of `connect()`, which now only talks to the shell. Gone from `@quad-education/sdk/engine` (platform-side only): `school: false`, `accounts`, `accountsFor`, a per-app `timezone`, `attachApp`, `hasApp`, `setAccounts` and `isStandalone`. Gone from `@quad-education/sdk/contract-tests`: the `STANDALONE_APP`, `ACCOUNT_APP` and `TEST_ACCOUNTS` fixtures and the `school` option. `@quad-education/sdk-react` loses `SignInChoices` and `SignInActions`; `@quad-education/schema` loses the manifest's `guests`, the reserved role `guest` and the SDK-version rule for guests. The `subject` rule now applies to every app. **Kept:** the `createdBy, createdAt` implicit index (a list of one's own records can be sorted), and the engine's state behind an `AppStore`, so a school instance's data API can run the same rules over DynamoDB; its `inbox`, `runs`, `auditLog`, `effectsSnapshot` and `runPrincipal` still return promises. Guest access inside a school instance is wanted, and comes back as a contract change when it is designed. Every earlier version, 0.2.0 included, is withdrawn from npm after this one is out.
|
|
@@ -214,6 +214,193 @@ export declare const ACCESS_APP: {
|
|
|
214
214
|
manifest: Manifest;
|
|
215
215
|
schema: typeof ACCESS_SCHEMA;
|
|
216
216
|
};
|
|
217
|
+
/**
|
|
218
|
+
* D143: an app with roles of its own that the school assigns. Staff and students use it as themselves, a Wellbeing
|
|
219
|
+
* lead reads every note, and a Coach (anyone the school chooses, even a parent, whom the app does not otherwise
|
|
220
|
+
* serve) keeps plans.
|
|
221
|
+
*/
|
|
222
|
+
export declare const APP_ROLES_SCHEMA: {
|
|
223
|
+
readonly note: {
|
|
224
|
+
readonly fields: {
|
|
225
|
+
readonly body: {
|
|
226
|
+
readonly kind: "text";
|
|
227
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
228
|
+
};
|
|
229
|
+
};
|
|
230
|
+
readonly indexes: readonly [];
|
|
231
|
+
readonly subject?: "body";
|
|
232
|
+
readonly access?: {
|
|
233
|
+
readonly staff: {
|
|
234
|
+
readonly read: "own";
|
|
235
|
+
readonly create: "own";
|
|
236
|
+
};
|
|
237
|
+
readonly student: {
|
|
238
|
+
readonly read: "own";
|
|
239
|
+
readonly create: "own";
|
|
240
|
+
};
|
|
241
|
+
readonly 'Wellbeing lead': {
|
|
242
|
+
readonly read: "all";
|
|
243
|
+
};
|
|
244
|
+
};
|
|
245
|
+
};
|
|
246
|
+
readonly plan: {
|
|
247
|
+
readonly fields: {
|
|
248
|
+
readonly title: {
|
|
249
|
+
readonly kind: "text";
|
|
250
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
251
|
+
};
|
|
252
|
+
};
|
|
253
|
+
readonly indexes: readonly [];
|
|
254
|
+
readonly subject?: "title";
|
|
255
|
+
readonly access?: {
|
|
256
|
+
readonly Coach: {
|
|
257
|
+
readonly read: "all";
|
|
258
|
+
readonly create: "own";
|
|
259
|
+
readonly delete: "own";
|
|
260
|
+
};
|
|
261
|
+
};
|
|
262
|
+
};
|
|
263
|
+
};
|
|
264
|
+
export declare const APP_ROLES_APP: {
|
|
265
|
+
manifest: Manifest;
|
|
266
|
+
schema: typeof APP_ROLES_SCHEMA;
|
|
267
|
+
};
|
|
268
|
+
/**
|
|
269
|
+
* D142: rules that follow the app admin flag and a table of people the app keeps. `leads` lists staff and only app
|
|
270
|
+
* admins change it; `monitors` lists students and any staff member changes it; `signups` is a list anyone on staff
|
|
271
|
+
* can add themselves to, which is allowed.
|
|
272
|
+
*/
|
|
273
|
+
export declare const LISTED_SCHEMA: {
|
|
274
|
+
readonly leads: {
|
|
275
|
+
readonly fields: {
|
|
276
|
+
readonly staffId: {
|
|
277
|
+
readonly kind: "ref";
|
|
278
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
279
|
+
} & {
|
|
280
|
+
to: "school.staff";
|
|
281
|
+
};
|
|
282
|
+
};
|
|
283
|
+
readonly indexes: readonly [];
|
|
284
|
+
readonly subject?: "staffId";
|
|
285
|
+
readonly access?: {
|
|
286
|
+
readonly staff: {
|
|
287
|
+
readonly read: "all";
|
|
288
|
+
readonly create: "appAdmin";
|
|
289
|
+
readonly update: "appAdmin";
|
|
290
|
+
readonly delete: "appAdmin";
|
|
291
|
+
};
|
|
292
|
+
};
|
|
293
|
+
};
|
|
294
|
+
readonly checkin: {
|
|
295
|
+
readonly fields: {
|
|
296
|
+
readonly mood: {
|
|
297
|
+
readonly kind: "text";
|
|
298
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
299
|
+
};
|
|
300
|
+
};
|
|
301
|
+
readonly indexes: readonly [];
|
|
302
|
+
readonly subject?: "mood";
|
|
303
|
+
readonly access?: {
|
|
304
|
+
readonly staff: {
|
|
305
|
+
readonly read: readonly ["own", "listed:leads"];
|
|
306
|
+
readonly create: "own";
|
|
307
|
+
readonly delete: readonly ["own", "appAdmin"];
|
|
308
|
+
};
|
|
309
|
+
};
|
|
310
|
+
};
|
|
311
|
+
readonly setting: {
|
|
312
|
+
readonly fields: {
|
|
313
|
+
readonly label: {
|
|
314
|
+
readonly kind: "text";
|
|
315
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
316
|
+
};
|
|
317
|
+
};
|
|
318
|
+
readonly indexes: readonly [];
|
|
319
|
+
readonly subject?: "label";
|
|
320
|
+
readonly access?: {
|
|
321
|
+
readonly staff: {
|
|
322
|
+
readonly read: "all";
|
|
323
|
+
readonly create: "appAdmin";
|
|
324
|
+
readonly update: "appAdmin";
|
|
325
|
+
readonly delete: "appAdmin";
|
|
326
|
+
};
|
|
327
|
+
};
|
|
328
|
+
};
|
|
329
|
+
readonly monitors: {
|
|
330
|
+
readonly fields: {
|
|
331
|
+
readonly studentId: {
|
|
332
|
+
readonly kind: "ref";
|
|
333
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
334
|
+
} & {
|
|
335
|
+
to: "school.students";
|
|
336
|
+
};
|
|
337
|
+
readonly addedBy: {
|
|
338
|
+
readonly kind: "ref";
|
|
339
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
340
|
+
} & {
|
|
341
|
+
to: "school.staff";
|
|
342
|
+
} & {
|
|
343
|
+
readonly optional: true;
|
|
344
|
+
};
|
|
345
|
+
};
|
|
346
|
+
readonly indexes: readonly [];
|
|
347
|
+
readonly subject?: "studentId" | "addedBy";
|
|
348
|
+
readonly access?: import("@quad-education/schema").AccessPolicy;
|
|
349
|
+
};
|
|
350
|
+
readonly loan: {
|
|
351
|
+
readonly fields: {
|
|
352
|
+
readonly title: {
|
|
353
|
+
readonly kind: "text";
|
|
354
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
355
|
+
};
|
|
356
|
+
};
|
|
357
|
+
readonly indexes: readonly [];
|
|
358
|
+
readonly subject?: "title";
|
|
359
|
+
readonly access?: {
|
|
360
|
+
readonly staff: {
|
|
361
|
+
readonly read: "all";
|
|
362
|
+
readonly create: "all";
|
|
363
|
+
};
|
|
364
|
+
readonly student: {
|
|
365
|
+
readonly read: readonly ["own", "listed:monitors"];
|
|
366
|
+
readonly create: "own";
|
|
367
|
+
};
|
|
368
|
+
};
|
|
369
|
+
};
|
|
370
|
+
readonly signups: {
|
|
371
|
+
readonly fields: {
|
|
372
|
+
readonly staffId: {
|
|
373
|
+
readonly kind: "ref";
|
|
374
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
375
|
+
} & {
|
|
376
|
+
to: "school.staff";
|
|
377
|
+
};
|
|
378
|
+
};
|
|
379
|
+
readonly indexes: readonly [];
|
|
380
|
+
readonly subject?: "staffId";
|
|
381
|
+
readonly access?: import("@quad-education/schema").AccessPolicy;
|
|
382
|
+
};
|
|
383
|
+
readonly roster: {
|
|
384
|
+
readonly fields: {
|
|
385
|
+
readonly day: {
|
|
386
|
+
readonly kind: "text";
|
|
387
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
388
|
+
};
|
|
389
|
+
};
|
|
390
|
+
readonly indexes: readonly [];
|
|
391
|
+
readonly subject?: "day";
|
|
392
|
+
readonly access?: {
|
|
393
|
+
readonly staff: {
|
|
394
|
+
readonly read: "listed:signups";
|
|
395
|
+
readonly create: "listed:signups";
|
|
396
|
+
};
|
|
397
|
+
};
|
|
398
|
+
};
|
|
399
|
+
};
|
|
400
|
+
export declare const LISTED_APP: {
|
|
401
|
+
manifest: Manifest;
|
|
402
|
+
schema: typeof LISTED_SCHEMA;
|
|
403
|
+
};
|
|
217
404
|
/** A staff-only log that test jobs write to, so tests can see what ran and as whom. */
|
|
218
405
|
export declare const JOBS_SCHEMA: {
|
|
219
406
|
/** What a job could see and do, recorded by the job itself (D7 run-as tests). */
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { boolean, defineSchema, entity, enumOf, number, ref, text, } from '@quad-education/schema';
|
|
1
|
+
import { boolean, defineSchema, entity, enumOf, listedIn, number, ref, text, } from '@quad-education/schema';
|
|
2
2
|
import { staffOf } from "../src/contract/recipients.js";
|
|
3
3
|
import { defineJob } from "../src/jobs/index.js";
|
|
4
4
|
/** The schema the contract suite's test apps use. */
|
|
@@ -149,6 +149,78 @@ export const ACCESS_APP = {
|
|
|
149
149
|
manifest: { ...TEST_APP.manifest, id: 'contract-access', name: 'Access control app' },
|
|
150
150
|
schema: ACCESS_SCHEMA,
|
|
151
151
|
};
|
|
152
|
+
/**
|
|
153
|
+
* D143: an app with roles of its own that the school assigns. Staff and students use it as themselves, a Wellbeing
|
|
154
|
+
* lead reads every note, and a Coach (anyone the school chooses, even a parent, whom the app does not otherwise
|
|
155
|
+
* serve) keeps plans.
|
|
156
|
+
*/
|
|
157
|
+
export const APP_ROLES_SCHEMA = defineSchema({
|
|
158
|
+
note: entity({
|
|
159
|
+
fields: { body: text() },
|
|
160
|
+
access: {
|
|
161
|
+
staff: { read: 'own', create: 'own' },
|
|
162
|
+
student: { read: 'own', create: 'own' },
|
|
163
|
+
'Wellbeing lead': { read: 'all' },
|
|
164
|
+
},
|
|
165
|
+
}),
|
|
166
|
+
plan: entity({
|
|
167
|
+
fields: { title: text() },
|
|
168
|
+
access: { Coach: { read: 'all', create: 'own', delete: 'own' } },
|
|
169
|
+
}),
|
|
170
|
+
});
|
|
171
|
+
export const APP_ROLES_APP = {
|
|
172
|
+
manifest: {
|
|
173
|
+
...TEST_APP.manifest,
|
|
174
|
+
manifestVersion: 1,
|
|
175
|
+
id: 'contract-app-roles',
|
|
176
|
+
name: 'School-assigned roles app',
|
|
177
|
+
roles: ['Wellbeing lead', 'Coach'],
|
|
178
|
+
schoolRoles: ['staff', 'student'],
|
|
179
|
+
scopes: [],
|
|
180
|
+
notifications: [],
|
|
181
|
+
},
|
|
182
|
+
schema: APP_ROLES_SCHEMA,
|
|
183
|
+
};
|
|
184
|
+
/**
|
|
185
|
+
* D142: rules that follow the app admin flag and a table of people the app keeps. `leads` lists staff and only app
|
|
186
|
+
* admins change it; `monitors` lists students and any staff member changes it; `signups` is a list anyone on staff
|
|
187
|
+
* can add themselves to, which is allowed.
|
|
188
|
+
*/
|
|
189
|
+
export const LISTED_SCHEMA = defineSchema({
|
|
190
|
+
leads: entity({
|
|
191
|
+
fields: { staffId: ref('school.staff') },
|
|
192
|
+
access: { staff: { read: 'all', create: 'appAdmin', update: 'appAdmin', delete: 'appAdmin' } },
|
|
193
|
+
}),
|
|
194
|
+
checkin: entity({
|
|
195
|
+
fields: { mood: text() },
|
|
196
|
+
access: {
|
|
197
|
+
staff: { read: ['own', listedIn('leads')], create: 'own', delete: ['own', 'appAdmin'] },
|
|
198
|
+
},
|
|
199
|
+
}),
|
|
200
|
+
setting: entity({
|
|
201
|
+
fields: { label: text() },
|
|
202
|
+
access: { staff: { read: 'all', create: 'appAdmin', update: 'appAdmin', delete: 'appAdmin' } },
|
|
203
|
+
}),
|
|
204
|
+
monitors: entity({
|
|
205
|
+
fields: { studentId: ref('school.students'), addedBy: ref('school.staff', { optional: true }) },
|
|
206
|
+
}),
|
|
207
|
+
loan: entity({
|
|
208
|
+
fields: { title: text() },
|
|
209
|
+
access: {
|
|
210
|
+
staff: { read: 'all', create: 'all' },
|
|
211
|
+
student: { read: ['own', listedIn('monitors')], create: 'own' },
|
|
212
|
+
},
|
|
213
|
+
}),
|
|
214
|
+
signups: entity({ fields: { staffId: ref('school.staff') } }),
|
|
215
|
+
roster: entity({
|
|
216
|
+
fields: { day: text() },
|
|
217
|
+
access: { staff: { read: listedIn('signups'), create: listedIn('signups') } },
|
|
218
|
+
}),
|
|
219
|
+
});
|
|
220
|
+
export const LISTED_APP = {
|
|
221
|
+
manifest: { ...TEST_APP.manifest, id: 'contract-listed', name: 'Listed access app' },
|
|
222
|
+
schema: LISTED_SCHEMA,
|
|
223
|
+
};
|
|
152
224
|
// ---- Slice B: tasks, jobs, audit ----
|
|
153
225
|
/** A staff-only log that test jobs write to, so tests can see what ran and as whom. */
|
|
154
226
|
export const JOBS_SCHEMA = defineSchema({
|
|
@@ -2,7 +2,7 @@ import type { Principal } from '@quad-education/canonical';
|
|
|
2
2
|
import type { ContractEnv, TestApp } from './suite.ts';
|
|
3
3
|
import type { QuadSdk } from '../src/contract/types.ts';
|
|
4
4
|
import type { Engine } from '../src/engine/index.ts';
|
|
5
|
-
type SliceBControl = Pick<ContractEnv['control'], 'advanceClock' | 'runDueJobs' | 'redeliver' | 'runs' | 'inbox' | 'auditLog' | 'setAppAdmins'>;
|
|
5
|
+
type SliceBControl = Pick<ContractEnv['control'], 'advanceClock' | 'runDueJobs' | 'redeliver' | 'runs' | 'inbox' | 'auditLog' | 'setAppAdmins' | 'setAppRoles'>;
|
|
6
6
|
/**
|
|
7
7
|
* The job-running and inspection controls, shared by both contract runners. Each job runs
|
|
8
8
|
* as its run's principal through whatever transport `connect` uses.
|
|
@@ -9,6 +9,9 @@ export function sliceBControls(engine, app, connect, onClock = async () => { })
|
|
|
9
9
|
async setAppAdmins(personIds) {
|
|
10
10
|
engine().setAppAdmins(appId(), personIds);
|
|
11
11
|
},
|
|
12
|
+
async setAppRoles(assignments) {
|
|
13
|
+
engine().setAppRoles(appId(), assignments);
|
|
14
|
+
},
|
|
12
15
|
async advanceClock(ms) {
|
|
13
16
|
engine().advanceClock(ms);
|
|
14
17
|
await onClock();
|
|
@@ -52,6 +52,8 @@ export type ContractEnv = {
|
|
|
52
52
|
})[]>;
|
|
53
53
|
/** The installation's app admins, as the school sets them (D133): person ids, replacing any before. */
|
|
54
54
|
setAppAdmins(personIds: readonly string[]): Promise<void>;
|
|
55
|
+
/** Who holds the app's own roles, as the school assigns them (D143): person id to role names, replacing any before. */
|
|
56
|
+
setAppRoles(assignments: Readonly<Record<string, readonly string[]>>): Promise<void>;
|
|
55
57
|
};
|
|
56
58
|
dispose(): Promise<void>;
|
|
57
59
|
};
|
|
@@ -4,7 +4,7 @@ import { eligibleParents } from '@quad-education/canonical';
|
|
|
4
4
|
import { suggestedPeople, withDerived } from '@quad-education/fictional-school';
|
|
5
5
|
import stored from '@quad-education/fictional-school/dataset' with { type: 'json' };
|
|
6
6
|
import { parentsOf, staffOf, studentsOf } from "../src/index.js";
|
|
7
|
-
import { ACCESS_APP, BASIC_APP, JOBS_APP, ROLES_APP, TEST_APP, MEDICAL_APP, slowGate, } from "./apps.js";
|
|
7
|
+
import { ACCESS_APP, APP_ROLES_APP, LISTED_APP, BASIC_APP, JOBS_APP, ROLES_APP, TEST_APP, MEDICAL_APP, slowGate, } from "./apps.js";
|
|
8
8
|
import { expectError, ids, listAll } from "./helpers.js";
|
|
9
9
|
// The fictional school is the oracle: tests compare what the SDK returns with what the
|
|
10
10
|
// dataset says should be visible. They never read an implementation's storage.
|
|
@@ -55,6 +55,7 @@ export function defineContractSuite(name, factory) {
|
|
|
55
55
|
kind: 'user',
|
|
56
56
|
role: 'staff',
|
|
57
57
|
appAdmin: false,
|
|
58
|
+
appRoles: [],
|
|
58
59
|
person: {
|
|
59
60
|
id: record.id,
|
|
60
61
|
givenName: record.givenName,
|
|
@@ -63,7 +64,7 @@ export function defineContractSuite(name, factory) {
|
|
|
63
64
|
},
|
|
64
65
|
});
|
|
65
66
|
expect(sdk.appId).toBe(TEST_APP.manifest.id);
|
|
66
|
-
expect(sdk.version).toBe('0.
|
|
67
|
+
expect(sdk.version).toBe('0.8.0');
|
|
67
68
|
});
|
|
68
69
|
it('knows parents and students', async () => {
|
|
69
70
|
expect((await (await env.connect(STUDENT)).auth.me()).role).toBe('student');
|
|
@@ -122,6 +123,154 @@ export function defineContractSuite(name, factory) {
|
|
|
122
123
|
});
|
|
123
124
|
});
|
|
124
125
|
});
|
|
126
|
+
// D143: the school gives people the app's own roles. The app is told which ones the person holds, and the rules
|
|
127
|
+
// of each apply to whoever holds it, beside the rules of their school role.
|
|
128
|
+
describe('auth and db access: school-assigned app roles (D143)', () => {
|
|
129
|
+
useApp(APP_ROLES_APP);
|
|
130
|
+
const OTHER = persona('secondary-teacher');
|
|
131
|
+
it('holds none until the school assigns one, and says so in me()', async () => {
|
|
132
|
+
expect((await (await env.connect(STAFF)).auth.me()).appRoles).toEqual([]);
|
|
133
|
+
expect((await (await env.connect(STUDENT)).auth.me()).appRoles).toEqual([]);
|
|
134
|
+
});
|
|
135
|
+
it("names the roles a person holds, in the manifest's spelling, and no others", async () => {
|
|
136
|
+
await env.control.setAppRoles({
|
|
137
|
+
[STAFF.personId]: ['coach', 'WELLBEING LEAD', 'Principal'],
|
|
138
|
+
[OTHER.personId]: ['Coach'],
|
|
139
|
+
});
|
|
140
|
+
expect((await (await env.connect(STAFF)).auth.me()).appRoles).toEqual([
|
|
141
|
+
'Wellbeing lead',
|
|
142
|
+
'Coach',
|
|
143
|
+
]);
|
|
144
|
+
expect((await (await env.connect(OTHER)).auth.me()).appRoles).toEqual(['Coach']);
|
|
145
|
+
expect((await (await env.connect(STAFF)).auth.me()).role).toBe('staff');
|
|
146
|
+
});
|
|
147
|
+
it('adds the rules of a role held to those of the school role', async () => {
|
|
148
|
+
const staff = await env.connect(STAFF);
|
|
149
|
+
const other = await env.connect(OTHER);
|
|
150
|
+
const mine = await staff.db.entity('note').create({ body: 'mine' });
|
|
151
|
+
const theirs = await other.db.entity('note').create({ body: 'theirs' });
|
|
152
|
+
expect(ids(await listAll(staff.db.entity('note')))).toEqual([mine.id]);
|
|
153
|
+
await env.control.setAppRoles({ [STAFF.personId]: ['Wellbeing lead'] });
|
|
154
|
+
expect(ids(await listAll(staff.db.entity('note')))).toEqual(ids([mine, theirs]));
|
|
155
|
+
// The school role's rules still apply: a lead writes their own notes as staff do.
|
|
156
|
+
await staff.db.entity('note').create({ body: 'another' });
|
|
157
|
+
expect(ids(await listAll(other.db.entity('note')))).toEqual([theirs.id]);
|
|
158
|
+
await env.control.setAppRoles({});
|
|
159
|
+
expect(ids(await listAll(staff.db.entity('note')))).not.toContain(theirs.id);
|
|
160
|
+
});
|
|
161
|
+
it('applies a role to whoever holds it, a student included', async () => {
|
|
162
|
+
const staff = await env.connect(STAFF);
|
|
163
|
+
const note = await staff.db.entity('note').create({ body: 'from staff' });
|
|
164
|
+
const student = await env.connect(STUDENT);
|
|
165
|
+
expect(await listAll(student.db.entity('note'))).toEqual([]);
|
|
166
|
+
await env.control.setAppRoles({ [STUDENT.personId]: ['Wellbeing lead'] });
|
|
167
|
+
expect(ids(await listAll(student.db.entity('note')))).toEqual([note.id]);
|
|
168
|
+
});
|
|
169
|
+
it('serves someone the school gave a role, whatever their school role', async () => {
|
|
170
|
+
await expectError((await env.connect(SIBLINGS_PARENT)).auth.me(), 'FORBIDDEN');
|
|
171
|
+
await env.control.setAppRoles({ [SIBLINGS_PARENT.personId]: ['Coach'] });
|
|
172
|
+
const parent = await env.connect(SIBLINGS_PARENT);
|
|
173
|
+
expect(await parent.auth.me()).toMatchObject({ role: 'parent', appRoles: ['Coach'] });
|
|
174
|
+
const plan = await parent.db.entity('plan').create({ title: 'Saturday' });
|
|
175
|
+
expect(ids(await listAll(parent.db.entity('plan')))).toEqual([plan.id]);
|
|
176
|
+
// Only what the role's rules say: a parent has no rules of their own for notes.
|
|
177
|
+
await expectError(parent.db.entity('note').list(), 'NOT_PERMITTED');
|
|
178
|
+
// Staff without the role have no rules for plans.
|
|
179
|
+
await expectError((await env.connect(STAFF)).db.entity('plan').list(), 'NOT_PERMITTED');
|
|
180
|
+
});
|
|
181
|
+
it('says nothing about roles for the system principal', async () => {
|
|
182
|
+
await env.control.setAppRoles({ [STAFF.personId]: ['Coach'] });
|
|
183
|
+
expect(await (await env.connect({ kind: 'system', appId: APP_ROLES_APP.manifest.id })).auth.me()).toEqual({ kind: 'system', appId: APP_ROLES_APP.manifest.id });
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
// D142: an app decides who reaches its rows with two more rules. `appAdmin` follows the school's choice of who
|
|
187
|
+
// looks after the app; `listed:` follows a table of people the app keeps, changed by whoever its own rules allow.
|
|
188
|
+
describe('db access: app admins and listed people (D142)', () => {
|
|
189
|
+
useApp(LISTED_APP);
|
|
190
|
+
const ADMIN = SUPERVISOR;
|
|
191
|
+
const OTHER = persona('secondary-teacher');
|
|
192
|
+
const SYSTEM_HERE = { kind: 'system', appId: LISTED_APP.manifest.id };
|
|
193
|
+
const OTHER_STUDENT = {
|
|
194
|
+
kind: 'user',
|
|
195
|
+
role: 'student',
|
|
196
|
+
personId: ds.students.find((s) => s.id !== STUDENT.personId).id,
|
|
197
|
+
};
|
|
198
|
+
beforeEach(async () => {
|
|
199
|
+
await env.control.setAppAdmins([ADMIN.personId]);
|
|
200
|
+
});
|
|
201
|
+
it("'appAdmin' lets only the app's admins write, while others read", async () => {
|
|
202
|
+
const admin = await env.connect(ADMIN);
|
|
203
|
+
const staff = await env.connect(STAFF);
|
|
204
|
+
const made = await admin.db.entity('setting').create({ label: 'Term 4' });
|
|
205
|
+
expect((await staff.db.entity('setting').get(made.id)).label).toBe('Term 4');
|
|
206
|
+
await expectError(staff.db.entity('setting').create({ label: 'mine' }), 'NOT_PERMITTED');
|
|
207
|
+
await expectError(staff.db.entity('setting').update(made.id, { label: 'changed' }), 'NOT_PERMITTED');
|
|
208
|
+
await expectError(staff.db.entity('setting').delete(made.id), 'NOT_PERMITTED');
|
|
209
|
+
expect((await admin.db.entity('setting').update(made.id, { label: 'Term 1' })).label).toBe('Term 1');
|
|
210
|
+
});
|
|
211
|
+
it('stops following someone the school no longer names as an admin', async () => {
|
|
212
|
+
const staff = await env.connect(STAFF);
|
|
213
|
+
await env.control.setAppAdmins([STAFF.personId]);
|
|
214
|
+
const made = await staff.db.entity('setting').create({ label: 'while admin' });
|
|
215
|
+
await env.control.setAppAdmins([]);
|
|
216
|
+
await expectError(staff.db.entity('setting').delete(made.id), 'NOT_PERMITTED');
|
|
217
|
+
});
|
|
218
|
+
it("'listed' gives the people a table points at every row, and nobody else", async () => {
|
|
219
|
+
const staff = await env.connect(STAFF);
|
|
220
|
+
const other = await env.connect(OTHER);
|
|
221
|
+
const admin = await env.connect(ADMIN);
|
|
222
|
+
const mine = await staff.db.entity('checkin').create({ mood: 'good' });
|
|
223
|
+
await other.db.entity('checkin').create({ mood: 'low' });
|
|
224
|
+
expect(ids(await listAll(other.db.entity('checkin')))).not.toContain(mine.id);
|
|
225
|
+
await expectError(other.db.entity('checkin').get(mine.id), 'NOT_FOUND');
|
|
226
|
+
const listing = await admin.db.entity('leads').create({ staffId: OTHER.personId });
|
|
227
|
+
expect(await listAll(other.db.entity('checkin'))).toHaveLength(2);
|
|
228
|
+
expect((await other.db.entity('checkin').get(mine.id)).mood).toBe('good');
|
|
229
|
+
// Being listed adds reading here, and nothing the rules don't say: deleting is still for the writer.
|
|
230
|
+
await expectError(other.db.entity('checkin').delete(mine.id), 'NOT_PERMITTED');
|
|
231
|
+
// Someone not listed is unchanged.
|
|
232
|
+
expect(await listAll(staff.db.entity('checkin'))).toHaveLength(1);
|
|
233
|
+
await admin.db.entity('leads').delete(listing.id);
|
|
234
|
+
expect(ids(await listAll(other.db.entity('checkin')))).not.toContain(mine.id);
|
|
235
|
+
});
|
|
236
|
+
it("lets only the people the table's own rules allow change who is listed", async () => {
|
|
237
|
+
const other = await env.connect(OTHER);
|
|
238
|
+
await expectError(other.db.entity('leads').create({ staffId: OTHER.personId }), 'NOT_PERMITTED');
|
|
239
|
+
expect(await listAll(other.db.entity('leads'))).toEqual([]);
|
|
240
|
+
});
|
|
241
|
+
it('lists students too, in a table staff keep', async () => {
|
|
242
|
+
const staff = await env.connect(STAFF);
|
|
243
|
+
const student = await env.connect(STUDENT);
|
|
244
|
+
const another = await env.connect(OTHER_STUDENT);
|
|
245
|
+
const theirs = await another.db.entity('loan').create({ title: 'Atlas' });
|
|
246
|
+
await student.db.entity('loan').create({ title: 'Novel' });
|
|
247
|
+
expect(ids(await listAll(student.db.entity('loan')))).not.toContain(theirs.id);
|
|
248
|
+
await staff.db.entity('monitors').create({ studentId: STUDENT.personId });
|
|
249
|
+
expect(ids(await listAll(student.db.entity('loan')))).toContain(theirs.id);
|
|
250
|
+
expect(ids(await listAll(another.db.entity('loan')))).toEqual([theirs.id]);
|
|
251
|
+
// A student is never listed by a field that holds staff.
|
|
252
|
+
await staff.db
|
|
253
|
+
.entity('monitors')
|
|
254
|
+
.create({ studentId: OTHER_STUDENT.personId, addedBy: STAFF.personId });
|
|
255
|
+
expect(await listAll(another.db.entity('loan'))).toHaveLength(2);
|
|
256
|
+
});
|
|
257
|
+
it('allows a list people add themselves to: a sign-up sheet', async () => {
|
|
258
|
+
const staff = await env.connect(STAFF);
|
|
259
|
+
await expectError(staff.db.entity('roster').create({ day: 'Monday' }), 'NOT_PERMITTED');
|
|
260
|
+
expect(await listAll(staff.db.entity('roster'))).toEqual([]);
|
|
261
|
+
await staff.db.entity('signups').create({ staffId: STAFF.personId });
|
|
262
|
+
const made = await staff.db.entity('roster').create({ day: 'Monday' });
|
|
263
|
+
expect(ids(await listAll(staff.db.entity('roster')))).toEqual([made.id]);
|
|
264
|
+
expect(await listAll((await env.connect(OTHER)).db.entity('roster'))).toEqual([]);
|
|
265
|
+
});
|
|
266
|
+
it('leaves jobs as they were: the system principal reaches every row', async () => {
|
|
267
|
+
const staff = await env.connect(STAFF);
|
|
268
|
+
const mine = await staff.db.entity('checkin').create({ mood: 'okay' });
|
|
269
|
+
const system = await env.connect(SYSTEM_HERE);
|
|
270
|
+
expect(ids(await listAll(system.db.entity('checkin')))).toEqual([mine.id]);
|
|
271
|
+
await system.db.entity('setting').create({ label: 'from a job' });
|
|
272
|
+
});
|
|
273
|
+
});
|
|
125
274
|
describe('school: visibility (D4)', () => {
|
|
126
275
|
useApp(TEST_APP);
|
|
127
276
|
it('staff see every student', async () => {
|
|
@@ -238,6 +387,9 @@ export function defineContractSuite(name, factory) {
|
|
|
238
387
|
const student = await env.connect(STUDENT);
|
|
239
388
|
expect((await staff.auth.me()).role).toBe('staff');
|
|
240
389
|
expect((await student.auth.me()).role).toBe('student');
|
|
390
|
+
// Everyone with the school role holds the app role of the same name (D143).
|
|
391
|
+
expect((await staff.auth.me()).appRoles).toEqual(['Staff']);
|
|
392
|
+
expect((await student.auth.me()).appRoles).toEqual(['Student']);
|
|
241
393
|
await expectError(env.connect(SIBLINGS_PARENT).then((p) => p.auth.me()), 'FORBIDDEN');
|
|
242
394
|
});
|
|
243
395
|
it('follows the access rules written for the app role, whatever the case of the key', async () => {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { CanonicalRecord, ContactPurpose, Entities, EntityName, RecordWithTiers, Role, TiersFor } from '@quad-education/canonical';
|
|
2
2
|
import type { AppEntityDef, AppInput, AppSchema, FieldValue, SystemFields } from '@quad-education/schema';
|
|
3
3
|
import type { Recipient } from './recipients.ts';
|
|
4
|
-
export declare const SDK_VERSION = "0.
|
|
4
|
+
export declare const SDK_VERSION = "0.8.0";
|
|
5
5
|
/**
|
|
6
6
|
* Describes the current app to the type system. `quad gen` augments it from
|
|
7
7
|
* app.manifest.json and schema/ (D23); without that, nothing is readable.
|
|
@@ -81,6 +81,12 @@ export type Me = {
|
|
|
81
81
|
readonly kind: 'user';
|
|
82
82
|
readonly role: Role;
|
|
83
83
|
readonly appAdmin: boolean;
|
|
84
|
+
/**
|
|
85
|
+
* The app's own roles this person holds (D143): names from the manifest's `roles`, as the manifest spells them,
|
|
86
|
+
* assigned by the school. Empty for most people. A role named like the person's school role (an app role
|
|
87
|
+
* `Staff` for a staff member) is held by everyone with that school role.
|
|
88
|
+
*/
|
|
89
|
+
readonly appRoles: readonly string[];
|
|
84
90
|
readonly person: PersonSummary;
|
|
85
91
|
} | {
|
|
86
92
|
readonly kind: 'system';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export const SDK_VERSION = '0.
|
|
1
|
+
export const SDK_VERSION = '0.8.0';
|
|
@@ -20,6 +20,7 @@ export declare class Engine implements Kernel {
|
|
|
20
20
|
private byId;
|
|
21
21
|
/** Each app's admins, as person ids (D133). The platform sets them, as a school does; apps only read them. */
|
|
22
22
|
private readonly admins;
|
|
23
|
+
private readonly roles;
|
|
23
24
|
private school;
|
|
24
25
|
readonly store: AppStore;
|
|
25
26
|
private constructor();
|
|
@@ -40,12 +41,25 @@ export declare class Engine implements Kernel {
|
|
|
40
41
|
schema: AppSchema;
|
|
41
42
|
}): Promise<void>;
|
|
42
43
|
manifest(appId: string): Manifest;
|
|
44
|
+
/** The installed app's own tables (D144): what the Sandbox's School data view lists beside the school's. */
|
|
45
|
+
schema(appId: string): AppSchema;
|
|
43
46
|
/**
|
|
44
47
|
* Who the school has put in charge of an app (D133): staff person ids, replacing any before. Platform only (the
|
|
45
48
|
* Sandbox's School data view, a test platform); no SDK method writes it. Ids that aren't staff in the school served at
|
|
46
49
|
* the time of a read are ignored, so someone who leaves drops out.
|
|
47
50
|
*/
|
|
48
51
|
setAppAdmins(appId: string, personIds: readonly string[]): void;
|
|
52
|
+
/**
|
|
53
|
+
* The app's own roles each person holds (D143), as a school assigns them: person id to role names, replacing any
|
|
54
|
+
* before. Platform only (the Sandbox's School data view, a test platform); no SDK method writes it. A name the
|
|
55
|
+
* manifest's `roles` doesn't have is ignored, and so is a person who isn't in the school served at the time of a read.
|
|
56
|
+
*/
|
|
57
|
+
setAppRoles(appId: string, assignments: Readonly<Record<string, readonly string[]>>): void;
|
|
58
|
+
/**
|
|
59
|
+
* The app roles a person holds, in the manifest's spelling and order: the ones assigned to them, and any named like
|
|
60
|
+
* their school role, which everyone with that school role previews (V2s D84).
|
|
61
|
+
*/
|
|
62
|
+
private heldAppRoles;
|
|
49
63
|
/** The app's admins who are staff in the school served now, in name order. */
|
|
50
64
|
private appAdminRecords;
|
|
51
65
|
/** Clear an app's data and outbox (the shell's reset button). */
|
|
@@ -139,8 +153,16 @@ export declare class Engine implements Kernel {
|
|
|
139
153
|
private parentChildren;
|
|
140
154
|
private dbEntity;
|
|
141
155
|
nowIso(): string;
|
|
142
|
-
/**
|
|
156
|
+
/**
|
|
157
|
+
* The rules for an operation: 'system' (full access), or every rule of the person's school role and of the app
|
|
158
|
+
* roles they hold (D143); empty means denied.
|
|
159
|
+
*/
|
|
143
160
|
private rules;
|
|
161
|
+
/**
|
|
162
|
+
* Which `appAdmin` and `listed:` rules the principal passes (D142). A listing is a row of the named table whose
|
|
163
|
+
* person field holds this person; the lookup ignores that table's own read rules, so tables may name each other.
|
|
164
|
+
*/
|
|
165
|
+
private resolveGrants;
|
|
144
166
|
/** Whether the current user may see a student, under the school's visibility policy (D4). */
|
|
145
167
|
private seesStudent;
|
|
146
168
|
private matches;
|
|
@@ -175,4 +197,4 @@ export declare class Engine implements Kernel {
|
|
|
175
197
|
/** D19: one CIO-visible entry per call that returns pastoral or medical fields. */
|
|
176
198
|
private auditSensitiveRead;
|
|
177
199
|
}
|
|
178
|
-
export declare const ENGINE_SDK_VERSION = "0.
|
|
200
|
+
export declare const ENGINE_SDK_VERSION = "0.8.0";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AUDITED_TIERS, isDerived, isWindowed, BASE_FIELDS, ENTITIES, eligibleParents, fieldVisible, filterFields, grantedTiers, principalTiers, visibleRecords, visibleStudentIds, } from '@quad-education/canonical';
|
|
2
|
-
import { DEFAULT_ACCESS, parseManifest, schemaHash, servedRoles, } from '@quad-education/schema';
|
|
2
|
+
import { DEFAULT_ACCESS, listedField, parseListed, parseManifest, schemaHash, servedRoles, } from '@quad-education/schema';
|
|
3
3
|
import { QuadError } from "../contract/errors.js";
|
|
4
4
|
import { SDK_VERSION } from "../contract/types.js";
|
|
5
5
|
import { clockNow } from "../protocol.js";
|
|
@@ -28,6 +28,7 @@ export class Engine {
|
|
|
28
28
|
byId = new Map();
|
|
29
29
|
/** Each app's admins, as person ids (D133). The platform sets them, as a school does; apps only read them. */
|
|
30
30
|
admins = new Map();
|
|
31
|
+
roles = new Map();
|
|
31
32
|
school;
|
|
32
33
|
store;
|
|
33
34
|
constructor(dataset, store) {
|
|
@@ -89,6 +90,10 @@ export class Engine {
|
|
|
89
90
|
manifest(appId) {
|
|
90
91
|
return this.appFor(appId).manifest;
|
|
91
92
|
}
|
|
93
|
+
/** The installed app's own tables (D144): what the Sandbox's School data view lists beside the school's. */
|
|
94
|
+
schema(appId) {
|
|
95
|
+
return this.appFor(appId).schema;
|
|
96
|
+
}
|
|
92
97
|
/**
|
|
93
98
|
* Who the school has put in charge of an app (D133): staff person ids, replacing any before. Platform only (the
|
|
94
99
|
* Sandbox's School data view, a test platform); no SDK method writes it. Ids that aren't staff in the school served at
|
|
@@ -98,6 +103,25 @@ export class Engine {
|
|
|
98
103
|
this.appFor(appId);
|
|
99
104
|
this.admins.set(appId, [...new Set(personIds)]);
|
|
100
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* The app's own roles each person holds (D143), as a school assigns them: person id to role names, replacing any
|
|
108
|
+
* before. Platform only (the Sandbox's School data view, a test platform); no SDK method writes it. A name the
|
|
109
|
+
* manifest's `roles` doesn't have is ignored, and so is a person who isn't in the school served at the time of a read.
|
|
110
|
+
*/
|
|
111
|
+
setAppRoles(appId, assignments) {
|
|
112
|
+
this.appFor(appId);
|
|
113
|
+
this.roles.set(appId, new Map(Object.entries(assignments).map(([id, r]) => [id, [...r]])));
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* The app roles a person holds, in the manifest's spelling and order: the ones assigned to them, and any named like
|
|
117
|
+
* their school role, which everyone with that school role previews (V2s D84).
|
|
118
|
+
*/
|
|
119
|
+
heldAppRoles(app, principal) {
|
|
120
|
+
if (app.manifest.manifestVersion !== 1)
|
|
121
|
+
return [];
|
|
122
|
+
const assigned = (this.roles.get(app.manifest.id)?.get(principal.personId) ?? []).map((r) => r.toLowerCase());
|
|
123
|
+
return app.manifest.roles.filter((r) => r.toLowerCase() === principal.role || assigned.includes(r.toLowerCase()));
|
|
124
|
+
}
|
|
101
125
|
/** The app's admins who are staff in the school served now, in name order. */
|
|
102
126
|
appAdminRecords(appId) {
|
|
103
127
|
return (this.admins.get(appId) ?? [])
|
|
@@ -234,6 +258,8 @@ export class Engine {
|
|
|
234
258
|
async handle(appId, principal, method, params) {
|
|
235
259
|
const ctx = this.context(appId, principal);
|
|
236
260
|
const p = (params ?? {});
|
|
261
|
+
if (method.startsWith('db.'))
|
|
262
|
+
await this.resolveGrants(ctx);
|
|
237
263
|
switch (method) {
|
|
238
264
|
case 'auth.me':
|
|
239
265
|
return this.me(ctx);
|
|
@@ -309,8 +335,10 @@ export class Engine {
|
|
|
309
335
|
if (!person || person.entity !== PEOPLE[principal.role]) {
|
|
310
336
|
throw new QuadError('FORBIDDEN', `no ${principal.role} with id ${principal.personId}`);
|
|
311
337
|
}
|
|
312
|
-
// A school persona stands in for an app role of the same name, ignoring case (V2s D84)
|
|
313
|
-
|
|
338
|
+
// A school persona stands in for an app role of the same name, ignoring case (V2s D84), and anyone the school
|
|
339
|
+
// gave one of the app's roles is served whatever their school role (D143).
|
|
340
|
+
if (!servedRoles(app.manifest).includes(principal.role) &&
|
|
341
|
+
this.heldAppRoles(app, principal).length === 0) {
|
|
314
342
|
throw new QuadError('FORBIDDEN', `${app.manifest.name} isn't available to ${principal.role} users`);
|
|
315
343
|
}
|
|
316
344
|
return { app, principal, actorId: principal.personId };
|
|
@@ -322,7 +350,13 @@ export class Engine {
|
|
|
322
350
|
const r = this.byId.get(principal.personId).record;
|
|
323
351
|
const appAdmin = principal.role === 'staff' &&
|
|
324
352
|
this.appAdminRecords(ctx.app.manifest.id).some((a) => a.id === r.id);
|
|
325
|
-
return {
|
|
353
|
+
return {
|
|
354
|
+
kind: 'user',
|
|
355
|
+
role: principal.role,
|
|
356
|
+
appAdmin,
|
|
357
|
+
appRoles: this.heldAppRoles(ctx.app, principal),
|
|
358
|
+
person: personSummary(r),
|
|
359
|
+
};
|
|
326
360
|
}
|
|
327
361
|
appAdmins(ctx) {
|
|
328
362
|
if (ctx.principal.kind === 'user' && ctx.principal.role !== 'staff') {
|
|
@@ -451,18 +485,62 @@ export class Engine {
|
|
|
451
485
|
nowIso() {
|
|
452
486
|
return new Date(this.nowMs()).toISOString();
|
|
453
487
|
}
|
|
454
|
-
/**
|
|
488
|
+
/**
|
|
489
|
+
* The rules for an operation: 'system' (full access), or every rule of the person's school role and of the app
|
|
490
|
+
* roles they hold (D143); empty means denied.
|
|
491
|
+
*/
|
|
455
492
|
rules(ctx, def, op) {
|
|
456
493
|
if (ctx.principal.kind === 'system')
|
|
457
494
|
return 'system';
|
|
458
495
|
const policy = def.access ?? DEFAULT_ACCESS;
|
|
459
|
-
//
|
|
460
|
-
const
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
const
|
|
465
|
-
|
|
496
|
+
// A key may be written in any case: an app role `Staff` is previewed by the staff persona.
|
|
497
|
+
const names = new Set([
|
|
498
|
+
ctx.principal.role,
|
|
499
|
+
...this.heldAppRoles(ctx.app, ctx.principal).map((r) => r.toLowerCase()),
|
|
500
|
+
]);
|
|
501
|
+
const rules = new Set();
|
|
502
|
+
for (const [key, ops] of Object.entries(policy)) {
|
|
503
|
+
if (!names.has(key.toLowerCase()))
|
|
504
|
+
continue;
|
|
505
|
+
const value = ops?.[op];
|
|
506
|
+
for (const rule of value === undefined ? [] : Array.isArray(value) ? value : [value])
|
|
507
|
+
rules.add(rule);
|
|
508
|
+
}
|
|
509
|
+
return [...rules];
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* Which `appAdmin` and `listed:` rules the principal passes (D142). A listing is a row of the named table whose
|
|
513
|
+
* person field holds this person; the lookup ignores that table's own read rules, so tables may name each other.
|
|
514
|
+
*/
|
|
515
|
+
async resolveGrants(ctx) {
|
|
516
|
+
const grants = new Set();
|
|
517
|
+
ctx.grants = grants;
|
|
518
|
+
const { principal, app } = ctx;
|
|
519
|
+
if (principal.kind === 'system')
|
|
520
|
+
return;
|
|
521
|
+
const appId = app.manifest.id;
|
|
522
|
+
if (principal.role === 'staff' &&
|
|
523
|
+
this.appAdminRecords(appId).some((a) => a.id === principal.personId))
|
|
524
|
+
grants.add('appAdmin');
|
|
525
|
+
const rules = new Set();
|
|
526
|
+
for (const def of Object.values(app.schema))
|
|
527
|
+
for (const ops of Object.values(def.access ?? {}))
|
|
528
|
+
for (const value of Object.values(ops ?? {}))
|
|
529
|
+
for (const rule of Array.isArray(value) ? value : [value])
|
|
530
|
+
if (parseListed(rule))
|
|
531
|
+
rules.add(rule);
|
|
532
|
+
for (const rule of rules) {
|
|
533
|
+
const listed = parseListed(rule);
|
|
534
|
+
const found = listedField(app.schema, listed, principal.role);
|
|
535
|
+
if (typeof found === 'string')
|
|
536
|
+
continue;
|
|
537
|
+
const rows = await this.store.candidates(appId, listed.entity, {
|
|
538
|
+
where: { [found.field]: principal.personId },
|
|
539
|
+
indexes: app.schema[listed.entity].indexes,
|
|
540
|
+
});
|
|
541
|
+
if (rows.some((r) => r[found.field] === principal.personId))
|
|
542
|
+
grants.add(rule);
|
|
543
|
+
}
|
|
466
544
|
}
|
|
467
545
|
/** Whether the current user may see a student, under the school's visibility policy (D4). */
|
|
468
546
|
seesStudent(ctx, studentId) {
|
|
@@ -477,6 +555,8 @@ export class Engine {
|
|
|
477
555
|
if (rules === 'system')
|
|
478
556
|
return true;
|
|
479
557
|
return rules.some((rule) => rule === 'all' ||
|
|
558
|
+
// `appAdmin` and `listed:` cover every record for whoever passes them (D142).
|
|
559
|
+
ctx.grants?.has(rule) === true ||
|
|
480
560
|
(rule === 'own' && record.createdBy === ctx.actorId) ||
|
|
481
561
|
(rule === 'subject' &&
|
|
482
562
|
def.subject !== undefined &&
|
|
@@ -635,7 +715,13 @@ export class Engine {
|
|
|
635
715
|
}
|
|
636
716
|
// Access first, then filters and pages: results and cursors only ever cover readable records.
|
|
637
717
|
const candidates = await this.store.candidates(ctx.app.manifest.id, entity.name, {
|
|
638
|
-
|
|
718
|
+
// A rule the caller doesn't pass adds nothing, so only the ones that could match decide the hint.
|
|
719
|
+
...(rules !== 'system' &&
|
|
720
|
+
rules
|
|
721
|
+
.filter((r) => r === 'own' || r === 'all' || r === 'subject' || ctx.grants?.has(r))
|
|
722
|
+
.every((r) => r === 'own')
|
|
723
|
+
? { ownedBy: ctx.actorId }
|
|
724
|
+
: {}),
|
|
639
725
|
where: query.where,
|
|
640
726
|
orderBy: query.orderBy,
|
|
641
727
|
indexes: entity.def.indexes,
|
|
@@ -62,6 +62,11 @@ export type Ctx = {
|
|
|
62
62
|
actorId: string;
|
|
63
63
|
/** Students this principal can see, computed once per call. */
|
|
64
64
|
students?: 'all' | Set<string>;
|
|
65
|
+
/**
|
|
66
|
+
* The `appAdmin` and `listed:` rules this principal passes (D142), worked out once per db call, before any row is
|
|
67
|
+
* checked: a listing is a lookup in the app's store, and the row check is synchronous.
|
|
68
|
+
*/
|
|
69
|
+
grants?: ReadonlySet<string>;
|
|
65
70
|
};
|
|
66
71
|
export declare const isStaffOrSystem: (ctx: Ctx) => boolean;
|
|
67
72
|
export declare const roleOfPrincipal: (ctx: Ctx) => Role | "system";
|
|
@@ -21,6 +21,11 @@ export type TestPlatform = {
|
|
|
21
21
|
* None by default, so a test says who is an admin.
|
|
22
22
|
*/
|
|
23
23
|
setAppAdmins(who: readonly PersonWho[]): void;
|
|
24
|
+
/**
|
|
25
|
+
* Give people the app's own roles, as a school does (D143): a suggested person's rule or anyone's id, to the names
|
|
26
|
+
* from the manifest's `roles`. It replaces any given before; nobody holds one unless a test says so.
|
|
27
|
+
*/
|
|
28
|
+
setAppRoles(roles: Readonly<Partial<Record<PersonWho, readonly string[]>>>): void;
|
|
24
29
|
setClock(iso: string | null): Promise<void>;
|
|
25
30
|
close(): void;
|
|
26
31
|
};
|
|
@@ -31,6 +36,8 @@ export declare function createTestPlatform(app: {
|
|
|
31
36
|
now?: string;
|
|
32
37
|
/** The app's admins from the start (D133); see `TestPlatform.setAppAdmins`. */
|
|
33
38
|
appAdmins?: readonly PersonWho[];
|
|
39
|
+
/** Who holds the app's own roles from the start (D143); see `TestPlatform.setAppRoles`. */
|
|
40
|
+
appRoles?: Readonly<Partial<Record<PersonWho, readonly string[]>>>;
|
|
34
41
|
}): Promise<TestPlatform>;
|
|
35
42
|
/** A suggestion rule, such as `parent-of-siblings` (see `SUGGESTION_RULES`), or a person's id. */
|
|
36
43
|
export type PersonWho = SuggestionRule | (string & {});
|
|
@@ -21,6 +21,12 @@ export async function createTestPlatform(app) {
|
|
|
21
21
|
}));
|
|
22
22
|
if (app.appAdmins)
|
|
23
23
|
setAppAdmins(app.appAdmins);
|
|
24
|
+
const setAppRoles = (roles) => engine.setAppRoles(appId, Object.fromEntries(Object.entries(roles).flatMap(([who, names]) => {
|
|
25
|
+
const p = toPrincipal(dataset, who);
|
|
26
|
+
return p.kind === 'user' && names ? [[p.personId, names]] : [];
|
|
27
|
+
})));
|
|
28
|
+
if (app.appRoles)
|
|
29
|
+
setAppRoles(app.appRoles);
|
|
24
30
|
return {
|
|
25
31
|
engine,
|
|
26
32
|
appId,
|
|
@@ -30,6 +36,7 @@ export async function createTestPlatform(app) {
|
|
|
30
36
|
},
|
|
31
37
|
system: () => connectInProcess(engine, appId, { kind: 'system', appId }),
|
|
32
38
|
setAppAdmins,
|
|
39
|
+
setAppRoles,
|
|
33
40
|
setClock: (iso) => engine.setClock(iso),
|
|
34
41
|
close: () => engine.close(),
|
|
35
42
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quad-education/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "The @quad-education/sdk contract, its in-browser engine, transports and contract tests",
|
|
6
6
|
"license": "SEE LICENSE IN LICENSE.md",
|
|
@@ -47,9 +47,9 @@
|
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"fake-indexeddb": "^6.2.5",
|
|
50
|
-
"@quad-education/canonical": "^0.
|
|
51
|
-
"@quad-education/fictional-school": "^0.
|
|
52
|
-
"@quad-education/schema": "^0.
|
|
50
|
+
"@quad-education/canonical": "^0.8.0",
|
|
51
|
+
"@quad-education/fictional-school": "^0.8.0",
|
|
52
|
+
"@quad-education/schema": "^0.8.0"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
55
|
"vitest": "^5.0.3"
|