@jskit-ai/workspaces-core 0.2.51 → 0.2.52
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/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
- `createStandaloneRoleContext({ resources, config })` (`@jskit-ai/workspaces-core/server/standaloneRoleContext`) gives code outside a JSKIT server, such as a migration or seed script that owns its tables through `createStandaloneResources`, `workspaces.core`'s `roleContext`: the same function, built by the same code (`createWorkspaceDirectory`, which `createWorkspacesRuntime` now uses too) from the application's runtime config, on the handles of the script's standalone runtime, which owns `ROLE_CONTEXT_TABLES` (the workspaces and users tables, exported beside it) with its own tables: `createStandaloneResources({ knex, feature, owns: [...ROLE_CONTEXT_TABLES, ...seedTables], systemScope })`. It takes the same arguments, answers the same context and refuses the same calls, with the script's own system context; resources that lack one of those tables are a `TypeError` naming them. On a server's Knex client `createStandaloneResources` refuses those tables (`DuplicateOwnerError`): code there takes `workspaces.core`'s.
|
|
5
6
|
- **Breaking:** a provided `workspaces.invite-mailer` sends as `APP_ENV` says (`createAppEnvironmentInviteMailer`, replacing the simulated mailer): production sends each invitation; staging sends it only to `STAGING_EMAIL_TO`, its message marked with the address it was for; development, test and staging without `STAGING_EMAIL_TO` log it and answer `status: "simulated"` (`workspaces.invite.simulated`, "Outside production the invitation email was logged, not sent. Copy the invite link to share it."). A role grant no action requires refuses to start in development and test and is logged in staging and production, by `APP_ENV`, not `NODE_ENV`.
|
|
6
7
|
- `workspaces.core` gains `roleContext({ workspaceId, userId, role, reason }, { context: systemContext(reason), trx? })`, the context of installer and seed work done in one workspace as a user in a role of the role catalog that they are not given there: required records a workspace gets for the platform's owner, who is normally no member of a self-created workspace, or a seed's starter data. It has exactly a member context's shape (`actor`, `workspace`, `membership`, `permissions`, the `workspace_user` visibility context and `requestMeta.resolvedWorkspaceContext`) with the membership `{ workspaceId, userId, roleSid, status: "active" }`, which is never saved and replaces any membership the user has there, and the role's permissions, so a permission check and a row policy decide as for a member in that role; it also records `reason`, which a row policy reads as `caller.reason` and which grants nothing. It is no system context and no job: request row limits apply and other owners' hidden fields stay hidden. Only a system context makes one, with a non-empty reason; any other context, a missing reason, ids that are no record ids and a role the catalog lacks are a TypeError before any statement, a missing or deleted workspace is 404 `workspaces.workspace.notFound`, a user id naming no one 404 `users.profile.notFound`, and an application without workspaces 403 `workspaces.workspace.contextDisabled`. It reads the user, the workspace and its settings, and writes nothing (no settings, no owner membership). The directory service's new `resolveRoleWorkspaceContextById(user, workspaceId, roleSid, options)` builds it. `memberContext` is unchanged and still means real membership only.
|
|
7
8
|
- An action run in a context that already carries the resolved context of the action's workspace (`requestMeta.resolvedWorkspaceContext` with that workspace's slug, as a member context and a role context carry it) keeps it: the workspace action context contributor no longer reads the workspace, the membership and the settings again, so a role context's unsaved membership survives into the action, and a member context's action no longer repeats its reads.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/workspaces-core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.52",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "jskit test"
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
"exports": {
|
|
9
9
|
"./server/WorkspacesFeature": "./src/server/WorkspacesFeature.js",
|
|
10
10
|
"./server/WorkspacesIntegrationsProvider": "./src/server/WorkspacesIntegrationsProvider.js",
|
|
11
|
+
"./server/standaloneRoleContext": "./src/server/standaloneRoleContext.js",
|
|
11
12
|
"./server/validators/routeParamsValidator": "./src/server/common/validators/routeParamsValidator.js",
|
|
12
13
|
"./server/support/workspaceRouteInput": "./src/server/support/workspaceRouteInput.js",
|
|
13
14
|
"./server/support/workspaceCrud": "./src/server/support/workspaceCrud.js",
|
|
@@ -25,7 +26,7 @@
|
|
|
25
26
|
"dependencies": {
|
|
26
27
|
"@jskit-ai/resource-core": "0.1.218",
|
|
27
28
|
"@jskit-ai/resource-crud-core": "0.1.220",
|
|
28
|
-
"@jskit-ai/users-core": "0.2.
|
|
29
|
+
"@jskit-ai/users-core": "0.2.52",
|
|
29
30
|
"json-rest-schema": "^1.0.20"
|
|
30
31
|
},
|
|
31
32
|
"description": "Workspace tenancy runtime plus HTTP routes, role catalog, and workspace config scaffolding.",
|
|
@@ -99,6 +100,10 @@
|
|
|
99
100
|
"subpath": "./server/WorkspacesFeature",
|
|
100
101
|
"summary": "Exports WorkspacesFeature, which provides workspaces.core (directory, members, pending invitations and settings services, their actions and routes), workspaces (the workspaces' records, with findBySlug and lock) and workspaces.memberships (the memberships' records, with addMember and activeMemberIds), and the optional tokens' defaults."
|
|
101
102
|
},
|
|
103
|
+
{
|
|
104
|
+
"subpath": "./server/standaloneRoleContext",
|
|
105
|
+
"summary": "Exports createStandaloneRoleContext({ resources, config }), workspaces.core's roleContext for code outside a JSKIT server (a migration or seed script), built by the same code on the handles of its standalone runtime, and ROLE_CONTEXT_TABLES, the workspaces and users tables that runtime owns."
|
|
106
|
+
},
|
|
102
107
|
{
|
|
103
108
|
"subpath": "./server/WorkspacesIntegrationsProvider",
|
|
104
109
|
"summary": "Exports WorkspacesIntegrationsProvider, which adds workspace context, visibility, provisioning, realtime audiences and invitation context to auth, users and actions."
|
|
@@ -215,6 +220,6 @@
|
|
|
215
220
|
"@jskit-ai/database-runtime": "0.1.276",
|
|
216
221
|
"@jskit-ai/http-runtime": "0.2.27",
|
|
217
222
|
"@jskit-ai/kernel": "0.2.27",
|
|
218
|
-
"@jskit-ai/resource-runtime": "0.2.
|
|
223
|
+
"@jskit-ai/resource-runtime": "0.2.39"
|
|
219
224
|
}
|
|
220
225
|
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { createUsersExtensions } from "@jskit-ai/users-core/server/usersExtensions";
|
|
2
|
+
import { USERS_IDENTITY_TABLES, createUsersIdentity } from "@jskit-ai/users-core/server/usersIdentity";
|
|
3
|
+
import { buildWorkspaceName } from "./common/services/workspaceDirectoryService.js";
|
|
4
|
+
import { noWorkspaceCreatedParticipant } from "./workspaceDirectory/noWorkspaceCreatedParticipant.js";
|
|
5
|
+
import { WORKSPACES_TABLES, createWorkspaceDirectory, createWorkspaceRecords } from "./workspacesRuntime.js";
|
|
6
|
+
|
|
7
|
+
// The tables a standalone runtime owns for createStandaloneRoleContext: a workspace names its owner, and a membership
|
|
8
|
+
// its user, so the users tables come with the workspaces tables.
|
|
9
|
+
const ROLE_CONTEXT_TABLES = Object.freeze([...USERS_IDENTITY_TABLES, ...WORKSPACES_TABLES]);
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* `workspaces.core`'s `roleContext` for code outside a JSKIT server, such as a migration or seed script, on the
|
|
13
|
+
* handles of its standalone runtime: the same function the server's capability holds, built the same way
|
|
14
|
+
* (createWorkspaceDirectory) from the application's runtime config. So it takes the same arguments, answers the same
|
|
15
|
+
* context and refuses the same calls: `roleContext({ workspaceId, userId, role, reason }, { context:
|
|
16
|
+
* systemContext(reason), trx })`, the system context and `trx` being the script's own. The script owns
|
|
17
|
+
* ROLE_CONTEXT_TABLES, with its own tables, through createStandaloneResources (which refuses them on the Knex client
|
|
18
|
+
* of a JSKIT server: code inside a server takes `workspaces.core`'s):
|
|
19
|
+
*
|
|
20
|
+
* const { resources, systemContext } = await createStandaloneResources({
|
|
21
|
+
* knex, feature: "seed", owns: [...ROLE_CONTEXT_TABLES, ...seedTables], systemScope: "seeds every workspace"
|
|
22
|
+
* });
|
|
23
|
+
* const roleContext = createStandaloneRoleContext({ resources, config });
|
|
24
|
+
* @param {object} options
|
|
25
|
+
* @param {Record<string, object>} options.resources The handles of a standalone runtime that owns ROLE_CONTEXT_TABLES.
|
|
26
|
+
* @param {import("@jskit-ai/kernel/server/types").RuntimeConfig} options.config The application's runtime config, as
|
|
27
|
+
* the server's `runtime.config` holds it: the tenancy mode, the role catalog and the workspace settings' defaults.
|
|
28
|
+
* @returns {import("./types.js").WorkspacesCore["roleContext"]}
|
|
29
|
+
*/
|
|
30
|
+
function createStandaloneRoleContext({ resources, config }) {
|
|
31
|
+
const missing = ROLE_CONTEXT_TABLES.filter((table) => !resources?.[table.name]).map((table) => table.name);
|
|
32
|
+
if (missing.length > 0) {
|
|
33
|
+
throw new TypeError(
|
|
34
|
+
"createStandaloneRoleContext() takes the resources of a standalone runtime that owns ROLE_CONTEXT_TABLES " +
|
|
35
|
+
"(createStandaloneResources({ knex, feature, owns: [...ROLE_CONTEXT_TABLES, ...yourTables], systemScope })). " +
|
|
36
|
+
`Missing: ${missing.join(", ")}.`
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
const { profiles } = createUsersIdentity({ resources, extensions: createUsersExtensions() });
|
|
40
|
+
const records = createWorkspaceRecords(resources);
|
|
41
|
+
return createWorkspaceDirectory({
|
|
42
|
+
config,
|
|
43
|
+
personalWorkspaceName: buildWorkspaceName,
|
|
44
|
+
resources,
|
|
45
|
+
userProfiles: profiles,
|
|
46
|
+
workspaceCreatedParticipant: noWorkspaceCreatedParticipant,
|
|
47
|
+
workspaces: records.workspaces,
|
|
48
|
+
workspaceMemberships: records.memberships
|
|
49
|
+
}).roleContext;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export { ROLE_CONTEXT_TABLES, createStandaloneRoleContext };
|
|
@@ -70,6 +70,59 @@ function createWorkspaceRecords(resources) {
|
|
|
70
70
|
});
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
+
/**
|
|
74
|
+
* The workspace directory service and the member and role contexts of `workspaces.core`, built from the runtime
|
|
75
|
+
* config, on the workspaces tables' handles and records and the users' profiles: the server's capability is built on
|
|
76
|
+
* it, and so is a standalone runtime's role context (see createStandaloneRoleContext), so both resolve the same
|
|
77
|
+
* contexts by the same reads and refuse the same calls.
|
|
78
|
+
* @param {object} dependencies
|
|
79
|
+
* @param {import("@jskit-ai/kernel/server/types").RuntimeConfig} dependencies.config
|
|
80
|
+
* @param {import("./types.js").PersonalWorkspaceName} dependencies.personalWorkspaceName
|
|
81
|
+
* @param {import("./types.js").WorkspacesResources} dependencies.resources The handles of the tables `workspaces.core` owns.
|
|
82
|
+
* @param {import("@jskit-ai/users-core/server/types").UsersProfiles} dependencies.userProfiles
|
|
83
|
+
* @param {import("./types.js").WorkspaceCreatedParticipant} dependencies.workspaceCreatedParticipant
|
|
84
|
+
* @param {import("./types.js").Workspaces} dependencies.workspaces
|
|
85
|
+
* @param {import("./types.js").WorkspacesMemberships} dependencies.workspaceMemberships
|
|
86
|
+
*/
|
|
87
|
+
function createWorkspaceDirectory({
|
|
88
|
+
config,
|
|
89
|
+
personalWorkspaceName,
|
|
90
|
+
resources,
|
|
91
|
+
userProfiles,
|
|
92
|
+
workspaceCreatedParticipant,
|
|
93
|
+
workspaces: workspacesRepository,
|
|
94
|
+
workspaceMemberships: workspaceMembershipsRepository
|
|
95
|
+
}) {
|
|
96
|
+
const tenancyProfile = resolveTenancyProfile(config);
|
|
97
|
+
const roleCatalog = createWorkspaceRoleCatalog(config);
|
|
98
|
+
const workspaceSettingsRepository = createWorkspaceSettingsRepository({
|
|
99
|
+
workspaceSettings: resources.workspaceSettings,
|
|
100
|
+
defaultInvitesEnabled: requireBoolean(
|
|
101
|
+
config?.workspaceSettings?.defaults?.invitesEnabled,
|
|
102
|
+
"runtime.config.workspaceSettings.defaults.invitesEnabled"
|
|
103
|
+
),
|
|
104
|
+
defaultColorSettings: requireWorkspaceColorDefaults(config?.workspaceSettings?.defaults)
|
|
105
|
+
});
|
|
106
|
+
const workspaceService = createWorkspaceService({
|
|
107
|
+
tenancyProfile,
|
|
108
|
+
roleCatalog,
|
|
109
|
+
personalWorkspaceName,
|
|
110
|
+
createdParticipant: workspaceCreatedParticipant,
|
|
111
|
+
workspacesRepository,
|
|
112
|
+
workspaceMembershipsRepository,
|
|
113
|
+
workspaceSettingsRepository
|
|
114
|
+
});
|
|
115
|
+
const memberContexts = createWorkspaceMemberContexts({ workspaceService, userProfiles, roleCatalog });
|
|
116
|
+
return Object.freeze({
|
|
117
|
+
tenancyProfile,
|
|
118
|
+
roleCatalog,
|
|
119
|
+
workspaceSettingsRepository,
|
|
120
|
+
workspaceService,
|
|
121
|
+
memberContext: memberContexts.memberContext,
|
|
122
|
+
roleContext: memberContexts.roleContext
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
73
126
|
/**
|
|
74
127
|
* The `workspaces.core` capability: the workspace services, the invite and
|
|
75
128
|
* settings repositories, `memberContext` and `roleContext`, built from the runtime config.
|
|
@@ -103,31 +156,22 @@ function createWorkspacesRuntime({
|
|
|
103
156
|
workspaces: workspacesRepository,
|
|
104
157
|
workspaceMemberships: workspaceMembershipsRepository
|
|
105
158
|
}) {
|
|
106
|
-
const
|
|
159
|
+
const directory = createWorkspaceDirectory({
|
|
160
|
+
config,
|
|
161
|
+
personalWorkspaceName,
|
|
162
|
+
resources,
|
|
163
|
+
userProfiles,
|
|
164
|
+
workspaceCreatedParticipant,
|
|
165
|
+
workspaces: workspacesRepository,
|
|
166
|
+
workspaceMemberships: workspaceMembershipsRepository
|
|
167
|
+
});
|
|
168
|
+
const { tenancyProfile, roleCatalog, workspaceSettingsRepository, workspaceService } = directory;
|
|
107
169
|
const invitationsPolicy = resolveWorkspaceInvitationsPolicy({ appConfig: config, tenancyProfile });
|
|
108
170
|
const invitationsEnabled = invitationsPolicy.enabled === true;
|
|
109
|
-
const roleCatalog = createWorkspaceRoleCatalog(config);
|
|
110
|
-
const workspaceSettingsRepository = createWorkspaceSettingsRepository({
|
|
111
|
-
workspaceSettings: resources.workspaceSettings,
|
|
112
|
-
defaultInvitesEnabled: requireBoolean(
|
|
113
|
-
config?.workspaceSettings?.defaults?.invitesEnabled,
|
|
114
|
-
"runtime.config.workspaceSettings.defaults.invitesEnabled"
|
|
115
|
-
),
|
|
116
|
-
defaultColorSettings: requireWorkspaceColorDefaults(config?.workspaceSettings?.defaults)
|
|
117
|
-
});
|
|
118
171
|
const workspaceInvitesRepository = createWorkspaceInvitesRepository({
|
|
119
172
|
workspaceInvites: resources.workspaceInvites,
|
|
120
173
|
workspaces: workspacesRepository
|
|
121
174
|
});
|
|
122
|
-
const workspaceService = createWorkspaceService({
|
|
123
|
-
tenancyProfile,
|
|
124
|
-
roleCatalog,
|
|
125
|
-
personalWorkspaceName,
|
|
126
|
-
createdParticipant: workspaceCreatedParticipant,
|
|
127
|
-
workspacesRepository,
|
|
128
|
-
workspaceMembershipsRepository,
|
|
129
|
-
workspaceSettingsRepository
|
|
130
|
-
});
|
|
131
175
|
const workspacePendingInvitationsService = createWorkspacePendingInvitationsService({
|
|
132
176
|
workspaceInvitesRepository,
|
|
133
177
|
workspaceMembershipsRepository,
|
|
@@ -159,16 +203,15 @@ function createWorkspacesRuntime({
|
|
|
159
203
|
workspaceInvitationsEnabled: invitationsEnabled,
|
|
160
204
|
roleCatalog
|
|
161
205
|
});
|
|
162
|
-
const memberContexts = createWorkspaceMemberContexts({ workspaceService, userProfiles, roleCatalog });
|
|
163
206
|
|
|
164
207
|
return Object.freeze({
|
|
165
208
|
enabled: tenancyProfile.workspace.enabled === true,
|
|
166
209
|
invitationsEnabled,
|
|
167
210
|
selfCreateEnabled: tenancyProfile.workspace.allowSelfCreate === true,
|
|
168
211
|
// The context of one member of one workspace, for background work done as that member.
|
|
169
|
-
memberContext:
|
|
212
|
+
memberContext: directory.memberContext,
|
|
170
213
|
// The context of a user acting in one workspace in a role they are not given there, for installer and seed work.
|
|
171
|
-
roleContext:
|
|
214
|
+
roleContext: directory.roleContext,
|
|
172
215
|
// The workspace route support the assistant runtime reads.
|
|
173
216
|
scope: Object.freeze({
|
|
174
217
|
params: workspaceSlugParamsValidator,
|
|
@@ -189,4 +232,4 @@ function createWorkspacesRuntime({
|
|
|
189
232
|
});
|
|
190
233
|
}
|
|
191
234
|
|
|
192
|
-
export { WORKSPACES_TABLES, createWorkspaceRecords, createWorkspacesRuntime };
|
|
235
|
+
export { WORKSPACES_TABLES, createWorkspaceDirectory, createWorkspaceRecords, createWorkspacesRuntime };
|
|
@@ -16,6 +16,8 @@ test("workspaces-core exports are explicit and aligned with production usage", (
|
|
|
16
16
|
requiredExports: [
|
|
17
17
|
"./server/WorkspacesFeature",
|
|
18
18
|
"./server/WorkspacesIntegrationsProvider",
|
|
19
|
+
// A script outside a server (a migration or seed runner) takes workspaces.core's roleContext from it.
|
|
20
|
+
"./server/standaloneRoleContext",
|
|
19
21
|
"./server/validators/routeParamsValidator",
|
|
20
22
|
"./server/support/workspaceRouteInput",
|
|
21
23
|
// Workspace CRUD features take their scope, workspace id and route paths from these.
|
|
@@ -6,7 +6,7 @@ import { defineTableResource } from "@jskit-ai/resource-core/shared/tableResourc
|
|
|
6
6
|
import { AppError } from "@jskit-ai/kernel/server/runtime/errors";
|
|
7
7
|
import { resolveActorId } from "@jskit-ai/kernel/server/actions/actionContributorHelpers";
|
|
8
8
|
import { silentLogger } from "@jskit-ai/kernel/server/runtime/logger";
|
|
9
|
-
import { checkForeignKeys, isSystemContext } from "@jskit-ai/resource-runtime/server";
|
|
9
|
+
import { checkForeignKeys, createStandaloneResources, isSystemContext } from "@jskit-ai/resource-runtime/server";
|
|
10
10
|
import { startTestFeatures } from "@jskit-ai/resource-runtime/test";
|
|
11
11
|
import { userProfileTableResource } from "@jskit-ai/users-core/shared/resources/userProfileResource";
|
|
12
12
|
import { userSettingsTableResource } from "@jskit-ai/users-core/shared/resources/userSettingsResource";
|
|
@@ -18,7 +18,9 @@ import usersUpdatedAt from "../../users-core/migrations/users_core_profile_updat
|
|
|
18
18
|
import workspacesInitial from "../migrations/workspaces_core_initial.cjs";
|
|
19
19
|
import workspaceSettingsMigration from "../migrations/workspaces_core_workspace_settings_single_name_source.cjs";
|
|
20
20
|
import workspaceColorMigration from "../migrations/workspaces_core_workspaces_drop_color.cjs";
|
|
21
|
-
import { createWorkspaceRecords } from "../src/server/WorkspacesFeature.js";
|
|
21
|
+
import { createWorkspaceRecords, createWorkspacesRuntime } from "../src/server/WorkspacesFeature.js";
|
|
22
|
+
import { ROLE_CONTEXT_TABLES, createStandaloneRoleContext } from "../src/server/standaloneRoleContext.js";
|
|
23
|
+
import { noInviteAcceptanceParticipant } from "../src/server/workspacePendingInvitations/noInviteAcceptanceParticipant.js";
|
|
22
24
|
import { WORKSPACES_TABLES } from "../src/server/workspacesRuntime.js";
|
|
23
25
|
import { createRepository as createInvitesRepository } from "../src/server/common/repositories/workspaceInvitesRepository.js";
|
|
24
26
|
import { createRepository as createMemberPagesRepository } from "../src/server/common/repositories/workspaceMemberPagesRepository.js";
|
|
@@ -97,6 +99,7 @@ describe(`users and workspaces repositories on migrated ${databaseLabel}`, () =>
|
|
|
97
99
|
let invites;
|
|
98
100
|
let memberPages;
|
|
99
101
|
let settings;
|
|
102
|
+
let workspaceResources;
|
|
100
103
|
let usersExtensions;
|
|
101
104
|
let profileProjector;
|
|
102
105
|
let ownsTables = false;
|
|
@@ -134,6 +137,7 @@ describe(`users and workspaces repositories on migrated ${databaseLabel}`, () =>
|
|
|
134
137
|
}));
|
|
135
138
|
({ profiles, userSettings, profileProjector, usersExtensions } = capabilities);
|
|
136
139
|
const resources = await runtime.own("workspaces.core", WORKSPACES_TABLES);
|
|
140
|
+
workspaceResources = resources;
|
|
137
141
|
membershipTable = resources.workspaceMemberships;
|
|
138
142
|
workspaceTable = resources.workspaces;
|
|
139
143
|
({ workspaces, memberships } = createWorkspaceRecords(resources));
|
|
@@ -754,6 +758,118 @@ describe(`users and workspaces repositories on migrated ${databaseLabel}`, () =>
|
|
|
754
758
|
await assert.rejects(noTenancyRoleContext(asked, system), refuses([403, "workspaces.workspace.contextDisabled"], "an application without workspaces"));
|
|
755
759
|
});
|
|
756
760
|
|
|
761
|
+
test("createStandaloneRoleContext, a script's roleContext outside a server, answers the server's context for the same inputs and refuses what the server's refuses", async () => {
|
|
762
|
+
// The runtime config both build from, as the server's runtime.config holds it.
|
|
763
|
+
const configOf = (tenancyMode) => ({
|
|
764
|
+
appPublicUrl: "http://localhost:5173",
|
|
765
|
+
tenancyMode,
|
|
766
|
+
workspaceMembers: { defaults: { inviteExpiresInMs: 86400000 } },
|
|
767
|
+
workspaceSettings: { defaults: { invitesEnabled: false } },
|
|
768
|
+
roleCatalog: {
|
|
769
|
+
roles: {
|
|
770
|
+
owner: { permissions: ["*"] },
|
|
771
|
+
admin: { assignable: true, permissions: ["workspace.members.view", "crud.notes.patch"] },
|
|
772
|
+
member: { assignable: true, permissions: ["crud.notes.list"] }
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
});
|
|
776
|
+
// workspaces.core's roleContext as WorkspacesFeature builds it, on the handles the feature owns.
|
|
777
|
+
const serverRoleContext = (tenancyMode) => createWorkspacesRuntime({
|
|
778
|
+
appRoot: process.cwd(),
|
|
779
|
+
config: configOf(tenancyMode),
|
|
780
|
+
env: {},
|
|
781
|
+
inviteMailer: unconfiguredWorkspaceInviteMailer,
|
|
782
|
+
inviteEmailTemplate: renderDefaultWorkspaceInviteEmail,
|
|
783
|
+
inviteAcceptanceParticipant: noInviteAcceptanceParticipant,
|
|
784
|
+
personalWorkspaceName: buildWorkspaceName,
|
|
785
|
+
resources: workspaceResources,
|
|
786
|
+
userProfiles: profiles,
|
|
787
|
+
userSettings,
|
|
788
|
+
workspaceCreatedParticipant: noWorkspaceCreatedParticipant,
|
|
789
|
+
workspaces,
|
|
790
|
+
workspaceMemberships: memberships
|
|
791
|
+
}).roleContext;
|
|
792
|
+
const server = serverRoleContext("workspaces");
|
|
793
|
+
// The script's own: a standalone runtime of its own on the same Knex, with no server runtime behind it, that owns
|
|
794
|
+
// the role context's tables and gives the script its system context.
|
|
795
|
+
const script = await createStandaloneResources({
|
|
796
|
+
knex, feature: "test.seed-script", owns: ROLE_CONTEXT_TABLES, systemScope: "seeds every workspace's starter records"
|
|
797
|
+
});
|
|
798
|
+
const standalone = createStandaloneRoleContext({ resources: script.resources, config: configOf("workspaces") });
|
|
799
|
+
// A runtime that owns some of those tables, or none, is refused, naming the ones it lacks.
|
|
800
|
+
assert.throws(() => createStandaloneRoleContext({ resources: { userProfiles: script.resources.userProfiles }, config: configOf("workspaces") }),
|
|
801
|
+
(error) => error instanceof TypeError && /Missing: userSettings, workspaces, workspaceMemberships, workspaceInvites, workspaceSettings\.$/u.test(error.message));
|
|
802
|
+
assert.throws(() => createStandaloneRoleContext({ config: configOf("workspaces") }), /owns ROLE_CONTEXT_TABLES/u);
|
|
803
|
+
|
|
804
|
+
const owner = await createProfile("Ada");
|
|
805
|
+
const member = await createProfile("Grace");
|
|
806
|
+
const outsider = await createProfile("Linus");
|
|
807
|
+
const workspace = await workspaces.insert({ slug: "team", name: "Team", ownerUserId: owner.id });
|
|
808
|
+
await memberships.ensureOwnerMembership(workspace.id, owner.id);
|
|
809
|
+
await memberships.addMember({ workspaceId: workspace.id, userId: member.id });
|
|
810
|
+
// The workspace's first request writes its settings; one no request has opened has none.
|
|
811
|
+
await memberDirectory().directory.resolveWorkspaceContextForUserBySlug(owner, workspace.slug);
|
|
812
|
+
const unopened = await workspaces.insert({ slug: "new", name: "New", ownerUserId: owner.id });
|
|
813
|
+
const deleted = await workspaces.insert({ slug: "gone", name: "Gone", ownerUserId: owner.id });
|
|
814
|
+
await workspaces.updateById(deleted.id, { deletedAt: new Date().toISOString() });
|
|
815
|
+
// The script's system context, from its standalone runtime's system scope.
|
|
816
|
+
const systemContext = script.systemContext("seed each workspace's starter records");
|
|
817
|
+
const system = { context: systemContext };
|
|
818
|
+
const reason = "seed the workspace's starter records";
|
|
819
|
+
const acting = { workspaceId: workspace.id, userId: owner.id, role: "admin", reason };
|
|
820
|
+
|
|
821
|
+
// A context, or a refusal as its class, status, code and message, and the statements the call sent.
|
|
822
|
+
const outcomeOf = async (roleContext, asked, options) => {
|
|
823
|
+
const { result, statements } = await statementsOf(() => roleContext(asked, options).then(
|
|
824
|
+
(context) => ({ context }),
|
|
825
|
+
(error) => ({ refusal: { name: error.name, typeError: error instanceof TypeError, status: error.status, code: error.code, message: error.message } })
|
|
826
|
+
));
|
|
827
|
+
return { ...result, statements };
|
|
828
|
+
};
|
|
829
|
+
const cases = [
|
|
830
|
+
["the outsider as admin", { ...acting, userId: outsider.id }, system],
|
|
831
|
+
["the member as admin", { ...acting, userId: member.id }, system],
|
|
832
|
+
["the owner as member", { ...acting, role: "member" }, system],
|
|
833
|
+
["the owner as OWNER where no request opened the workspace", { ...acting, workspaceId: unopened.id, role: "OWNER" }, system],
|
|
834
|
+
["no options", acting, undefined],
|
|
835
|
+
["a signed-in request's context", acting, { context: { actor: { id: owner.id } } }],
|
|
836
|
+
["a workspace job's context", acting, { context: systemContext.forWorkspace(workspace.id) }],
|
|
837
|
+
["a blank reason", { ...acting, reason: " " }, system],
|
|
838
|
+
["userId \"abc\"", { ...acting, userId: "abc" }, system],
|
|
839
|
+
["role \"superuser\"", { ...acting, role: "superuser" }, system],
|
|
840
|
+
["a workspace id naming no workspace", { ...acting, workspaceId: "999999" }, system],
|
|
841
|
+
["a deleted workspace", { ...acting, workspaceId: deleted.id }, system],
|
|
842
|
+
["a user id naming no one", { ...acting, userId: "999999" }, system]
|
|
843
|
+
];
|
|
844
|
+
const answered = [];
|
|
845
|
+
for (const [label, asked, options] of cases) {
|
|
846
|
+
const fromServer = await outcomeOf(server, asked, options);
|
|
847
|
+
const fromStandalone = await outcomeOf(standalone, asked, options);
|
|
848
|
+
const { statements: serverStatements, ...serverAnswer } = fromServer;
|
|
849
|
+
const { statements: standaloneStatements, ...standaloneAnswer } = fromStandalone;
|
|
850
|
+
assert.deepEqual(standaloneAnswer, serverAnswer, label);
|
|
851
|
+
if (serverAnswer.refusal?.typeError) {
|
|
852
|
+
assert.deepEqual([standaloneStatements, serverStatements], [0, 0], `${label} is refused before any statement`);
|
|
853
|
+
}
|
|
854
|
+
answered.push(serverAnswer.context ? "context" : `${serverAnswer.refusal.status ?? "TypeError"}`);
|
|
855
|
+
}
|
|
856
|
+
assert.deepEqual(answered, ["context", "context", "context", "context", "TypeError", "TypeError", "TypeError", "TypeError", "TypeError", "TypeError", "404", "404", "404"]);
|
|
857
|
+
|
|
858
|
+
// In the caller's transaction both read what the transaction wrote.
|
|
859
|
+
await memberships.withTransaction(system, async (txOptions) => {
|
|
860
|
+
const joiner = await createProfile("Barbara", txOptions);
|
|
861
|
+
const asked = { ...acting, userId: joiner.id, role: "member" };
|
|
862
|
+
const fromStandalone = await standalone(asked, txOptions);
|
|
863
|
+
assert.equal(fromStandalone.actor.id, joiner.id);
|
|
864
|
+
assert.deepEqual(fromStandalone, await server(asked, txOptions));
|
|
865
|
+
});
|
|
866
|
+
|
|
867
|
+
// An application without workspaces refuses both.
|
|
868
|
+
const noTenancy = await outcomeOf(createStandaloneRoleContext({ resources: script.resources, config: configOf("none") }), acting, system);
|
|
869
|
+
assert.deepEqual(noTenancy.refusal, (await outcomeOf(serverRoleContext("none"), acting, system)).refusal);
|
|
870
|
+
assert.deepEqual([noTenancy.refusal.status, noTenancy.refusal.code], [403, "workspaces.workspace.contextDisabled"]);
|
|
871
|
+
});
|
|
872
|
+
|
|
757
873
|
test("workspaces.lock and memberships.lockByWorkspaceIdAndUserId take lockMode share, and refuse another mode before any statement", async () => {
|
|
758
874
|
const owner = await createProfile("Ada");
|
|
759
875
|
const workspace = await workspaces.insert({ slug: "shared", name: "Shared", ownerUserId: owner.id });
|