@contember/engine-http 1.2.0-alpha.3 → 1.2.0-alpha.6
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/dist/src/content/ContentApiMiddlewareFactory.d.ts.map +1 -1
- package/dist/src/content/ContentApiMiddlewareFactory.js +13 -9
- package/dist/src/content/ContentApiMiddlewareFactory.js.map +1 -1
- package/dist/src/content/ContentGraphQLContextFactory.d.ts +2 -2
- package/dist/src/content/ContentGraphQLContextFactory.d.ts.map +1 -1
- package/dist/src/content/ContentGraphQLContextFactory.js +2 -5
- package/dist/src/content/ContentGraphQLContextFactory.js.map +1 -1
- package/dist/src/content/ProjectMembershipFetcher.d.ts +3 -2
- package/dist/src/content/ProjectMembershipFetcher.d.ts.map +1 -1
- package/dist/src/content/ProjectMembershipFetcher.js.map +1 -1
- package/dist/src/content/ProjectMembershipResolver.d.ts +4 -3
- package/dist/src/content/ProjectMembershipResolver.d.ts.map +1 -1
- package/dist/src/content/ProjectMembershipResolver.js +23 -6
- package/dist/src/content/ProjectMembershipResolver.js.map +1 -1
- package/dist/src/graphql/execution.js +2 -2
- package/dist/src/graphql/execution.js.map +1 -1
- package/dist/src/system/SystemGraphQLContextFactory.d.ts +2 -2
- package/dist/src/system/SystemGraphQLContextFactory.d.ts.map +1 -1
- package/dist/src/transfer/ContentSchemaTransferMappingFactory.d.ts.map +1 -1
- package/dist/src/transfer/ContentSchemaTransferMappingFactory.js +2 -0
- package/dist/src/transfer/ContentSchemaTransferMappingFactory.js.map +1 -1
- package/dist/src/transfer/ExportApiMiddlewareFactory.d.ts.map +1 -1
- package/dist/src/transfer/ExportApiMiddlewareFactory.js +18 -4
- package/dist/src/transfer/ExportApiMiddlewareFactory.js.map +1 -1
- package/dist/src/transfer/ImportApiMiddlewareFactory.d.ts.map +1 -1
- package/dist/src/transfer/ImportApiMiddlewareFactory.js +1 -5
- package/dist/src/transfer/ImportApiMiddlewareFactory.js.map +1 -1
- package/dist/src/transfer/ImportExecutor.d.ts +3 -1
- package/dist/src/transfer/ImportExecutor.d.ts.map +1 -1
- package/dist/src/transfer/ImportExecutor.js +27 -5
- package/dist/src/transfer/ImportExecutor.js.map +1 -1
- package/dist/src/transfer/SystemSchemaTransferMappingFactory.d.ts +1 -0
- package/dist/src/transfer/SystemSchemaTransferMappingFactory.d.ts.map +1 -1
- package/dist/src/transfer/SystemSchemaTransferMappingFactory.js +60 -28
- package/dist/src/transfer/SystemSchemaTransferMappingFactory.js.map +1 -1
- package/dist/src/tsconfig.tsbuildinfo +1 -1
- package/dist/tests/cases/unit/projectMembershipResolver.test.js +20 -6
- package/dist/tests/cases/unit/projectMembershipResolver.test.js.map +1 -1
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/package.json +10 -10
- package/src/content/ContentApiMiddlewareFactory.ts +2 -1
- package/src/content/ContentGraphQLContextFactory.ts +4 -7
- package/src/content/ProjectMembershipFetcher.ts +3 -2
- package/src/content/ProjectMembershipResolver.ts +36 -12
- package/src/graphql/execution.ts +2 -2
- package/src/system/SystemGraphQLContextFactory.ts +2 -2
- package/src/transfer/ContentSchemaTransferMappingFactory.ts +2 -0
- package/src/transfer/ExportApiMiddlewareFactory.ts +22 -5
- package/src/transfer/ImportApiMiddlewareFactory.ts +1 -5
- package/src/transfer/ImportExecutor.ts +33 -6
- package/src/transfer/SystemSchemaTransferMappingFactory.ts +78 -29
- package/tests/cases/unit/projectMembershipResolver.test.ts +23 -10
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contember/engine-http",
|
|
3
|
-
"version": "1.2.0-alpha.
|
|
3
|
+
"version": "1.2.0-alpha.6",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"main": "dist/src/index.js",
|
|
6
6
|
"typings": "dist/src/index.d.ts",
|
|
@@ -9,15 +9,15 @@
|
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
11
|
"@apollographql/graphql-playground-html": "^1.6.24",
|
|
12
|
-
"@contember/database": "^1.2.0-alpha.
|
|
13
|
-
"@contember/engine-content-api": "^1.2.0-alpha.
|
|
14
|
-
"@contember/engine-plugins": "^1.2.0-alpha.
|
|
15
|
-
"@contember/engine-system-api": "^1.2.0-alpha.
|
|
16
|
-
"@contember/engine-tenant-api": "^1.2.0-alpha.
|
|
17
|
-
"@contember/graphql-utils": "^1.2.0-alpha.
|
|
18
|
-
"@contember/schema": "^1.2.0-alpha.
|
|
19
|
-
"@contember/schema-utils": "^1.2.0-alpha.
|
|
20
|
-
"@contember/typesafe": "^1.2.0-alpha.
|
|
12
|
+
"@contember/database": "^1.2.0-alpha.6",
|
|
13
|
+
"@contember/engine-content-api": "^1.2.0-alpha.6",
|
|
14
|
+
"@contember/engine-plugins": "^1.2.0-alpha.6",
|
|
15
|
+
"@contember/engine-system-api": "^1.2.0-alpha.6",
|
|
16
|
+
"@contember/engine-tenant-api": "^1.2.0-alpha.6",
|
|
17
|
+
"@contember/graphql-utils": "^1.2.0-alpha.6",
|
|
18
|
+
"@contember/schema": "^1.2.0-alpha.6",
|
|
19
|
+
"@contember/schema-utils": "^1.2.0-alpha.6",
|
|
20
|
+
"@contember/typesafe": "^1.2.0-alpha.6",
|
|
21
21
|
"@graphql-tools/schema": "^8.3.5",
|
|
22
22
|
"@types/lru-cache": "^7.10.10",
|
|
23
23
|
"accepts": "^1.3.7",
|
|
@@ -74,7 +74,8 @@ export class ContentApiMiddlewareFactory {
|
|
|
74
74
|
acl: schema.acl,
|
|
75
75
|
projectSlug: project.slug,
|
|
76
76
|
identity: {
|
|
77
|
-
|
|
77
|
+
identityId: authResult.identityId,
|
|
78
|
+
personId: authResult.personId ?? undefined,
|
|
78
79
|
roles: authResult.roles,
|
|
79
80
|
},
|
|
80
81
|
}))
|
|
@@ -4,12 +4,12 @@ import { Context, createAclVariables, ExecutionContainerFactory } from '@contemb
|
|
|
4
4
|
import { setupSystemVariables } from '@contember/engine-system-api'
|
|
5
5
|
import { Client } from '@contember/database'
|
|
6
6
|
import { Acl, Schema } from '@contember/schema'
|
|
7
|
-
import { Membership } from '@contember/engine-tenant-api'
|
|
8
7
|
|
|
9
8
|
import { AuthResult, Timer } from '../common'
|
|
10
9
|
import { KoaContext } from '../koa'
|
|
11
10
|
import { Providers } from '../providers'
|
|
12
11
|
import { GraphQLKoaState } from '../graphql'
|
|
12
|
+
import { ParsedMembership } from '@contember/schema-utils'
|
|
13
13
|
|
|
14
14
|
export type ExtendedGraphqlContext = Context & { identityId: string; koaContext: KoaContext<GraphQLKoaState> }
|
|
15
15
|
|
|
@@ -24,22 +24,19 @@ export class ContentGraphQLContextFactory {
|
|
|
24
24
|
schema: Schema
|
|
25
25
|
permissions: Acl.Permissions
|
|
26
26
|
authResult: AuthResult
|
|
27
|
-
memberships: readonly
|
|
27
|
+
memberships: readonly ParsedMembership[]
|
|
28
28
|
timer: Timer
|
|
29
29
|
koaContext: KoaContext<GraphQLKoaState>
|
|
30
30
|
}): ExtendedGraphqlContext {
|
|
31
31
|
const partialContext = {
|
|
32
32
|
db,
|
|
33
33
|
identityId: authResult.identityId,
|
|
34
|
-
identityVariables: createAclVariables(schema.acl,
|
|
35
|
-
...authResult,
|
|
36
|
-
memberships,
|
|
37
|
-
}),
|
|
34
|
+
identityVariables: createAclVariables(schema.acl, memberships),
|
|
38
35
|
}
|
|
39
36
|
let identityId = authResult.identityId
|
|
40
37
|
if (
|
|
41
38
|
authResult.assumedIdentityId &&
|
|
42
|
-
|
|
39
|
+
memberships.some(it => schema.acl.roles[it.role].system?.assumeIdentity)
|
|
43
40
|
) {
|
|
44
41
|
identityId = authResult.assumedIdentityId
|
|
45
42
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { DatabaseContext,
|
|
1
|
+
import { DatabaseContext, ProjectMemberManager } from '@contember/engine-tenant-api'
|
|
2
|
+
import { Acl } from '@contember/schema'
|
|
2
3
|
|
|
3
4
|
export class ProjectMembershipFetcher {
|
|
4
5
|
constructor(
|
|
@@ -7,7 +8,7 @@ export class ProjectMembershipFetcher {
|
|
|
7
8
|
) {
|
|
8
9
|
}
|
|
9
10
|
|
|
10
|
-
public async fetchMemberships(projectSlug: string, identity: { id: string; roles?: readonly string[] }): Promise<readonly Membership[]> {
|
|
11
|
+
public async fetchMemberships(projectSlug: string, identity: { id: string; roles?: readonly string[] }): Promise<readonly Acl.Membership[]> {
|
|
11
12
|
return await this.projectMemberManager.getProjectMemberships(
|
|
12
13
|
this.databaseContext,
|
|
13
14
|
{ slug: projectSlug },
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MembershipMatcher } from '@contember/engine-tenant-api'
|
|
2
2
|
import * as Typesafe from '@contember/typesafe'
|
|
3
3
|
import { Acl } from '@contember/schema'
|
|
4
4
|
import { HttpError } from '../common'
|
|
5
5
|
import { ProjectMembershipFetcher } from './ProjectMembershipFetcher'
|
|
6
|
+
import { MembershipResolver, ParsedMembership } from '@contember/schema-utils'
|
|
6
7
|
|
|
7
8
|
const assumeMembershipHeader = 'x-contember-assume-membership'
|
|
8
9
|
|
|
@@ -21,25 +22,47 @@ export class ProjectMembershipResolver {
|
|
|
21
22
|
acl: Acl.Schema
|
|
22
23
|
request: HeaderAccessor
|
|
23
24
|
projectSlug: string
|
|
24
|
-
identity: {
|
|
25
|
-
}): Promise<readonly
|
|
25
|
+
identity: { identityId: string; personId?: string; roles?: readonly string[] }
|
|
26
|
+
}): Promise<readonly ParsedMembership[]> {
|
|
26
27
|
|
|
27
|
-
const explicitMemberships = await this.projectMembershipFetcher.fetchMemberships(projectSlug,
|
|
28
|
+
const explicitMemberships = await this.projectMembershipFetcher.fetchMemberships(projectSlug, {
|
|
29
|
+
id: identity.identityId,
|
|
30
|
+
roles: identity.roles,
|
|
31
|
+
})
|
|
28
32
|
|
|
29
33
|
const implicitRoles = Object.entries(acl.roles).filter(([, role]) => role.implicit).map(([name]) => name)
|
|
30
34
|
|
|
31
|
-
|
|
35
|
+
const throwNotAllowed = () => {
|
|
32
36
|
const errorMessage = this.debug
|
|
33
37
|
? `You are not allowed to access project ${projectSlug}`
|
|
34
38
|
: `Project ${projectSlug} NOT found`
|
|
35
39
|
throw new HttpError(errorMessage, 404)
|
|
36
40
|
}
|
|
37
41
|
|
|
42
|
+
if (explicitMemberships.length === 0 && implicitRoles.length === 0) {
|
|
43
|
+
throwNotAllowed()
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const membershipResolver = new MembershipResolver()
|
|
47
|
+
|
|
38
48
|
const assumedMemberships = this.readAssumedMemberships(request)
|
|
39
|
-
if (assumedMemberships
|
|
49
|
+
if (assumedMemberships !== null) {
|
|
50
|
+
|
|
51
|
+
if (assumedMemberships.length === 0) {
|
|
52
|
+
throwNotAllowed()
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const parsedMemberships = membershipResolver.resolve(acl, assumedMemberships, identity)
|
|
56
|
+
if (parsedMemberships.errors.length > 0) {
|
|
57
|
+
throw new HttpError(
|
|
58
|
+
`Invalid memberships in ${assumeMembershipHeader} header:\n` +
|
|
59
|
+
parsedMemberships.errors.map(it => JSON.stringify(it)).join('\n'),
|
|
60
|
+
400,
|
|
61
|
+
)
|
|
62
|
+
}
|
|
40
63
|
this.verifyAssumedRoles(explicitMemberships, acl, assumedMemberships)
|
|
41
64
|
|
|
42
|
-
return
|
|
65
|
+
return parsedMemberships.memberships
|
|
43
66
|
}
|
|
44
67
|
|
|
45
68
|
|
|
@@ -47,17 +70,18 @@ export class ProjectMembershipResolver {
|
|
|
47
70
|
const implicitRolesToAssign = implicitRoles.filter(it => !explicitProjectRoles.includes(it))
|
|
48
71
|
|
|
49
72
|
return [
|
|
50
|
-
|
|
73
|
+
// intentionally ignoring validation errors of stored memberships
|
|
74
|
+
...membershipResolver.resolve(acl, explicitMemberships, identity).memberships,
|
|
51
75
|
...implicitRolesToAssign.map(it => ({ role: it, variables: [] })),
|
|
52
76
|
]
|
|
53
77
|
}
|
|
54
78
|
|
|
55
|
-
private readAssumedMemberships(req: HeaderAccessor): readonly Membership[] {
|
|
79
|
+
private readAssumedMemberships(req: HeaderAccessor): null | readonly Acl.Membership[] {
|
|
56
80
|
const value = req.get(assumeMembershipHeader).trim()
|
|
57
81
|
if (value === '') {
|
|
58
|
-
return
|
|
82
|
+
return null
|
|
59
83
|
}
|
|
60
|
-
let parsedValue: { memberships: readonly Membership[] }
|
|
84
|
+
let parsedValue: { memberships: readonly Acl.Membership[] }
|
|
61
85
|
try {
|
|
62
86
|
parsedValue = assumeMembershipValueType(JSON.parse(value))
|
|
63
87
|
} catch (e: any) {
|
|
@@ -66,7 +90,7 @@ export class ProjectMembershipResolver {
|
|
|
66
90
|
return parsedValue.memberships
|
|
67
91
|
}
|
|
68
92
|
|
|
69
|
-
private verifyAssumedRoles(explicitMemberships: readonly Membership[], acl: Acl.Schema, assumedMemberships: readonly Membership[]) {
|
|
93
|
+
private verifyAssumedRoles(explicitMemberships: readonly Acl.Membership[], acl: Acl.Schema, assumedMemberships: readonly Acl.Membership[]) {
|
|
70
94
|
const membershipMatcher = new MembershipMatcher(explicitMemberships.map(it => ({
|
|
71
95
|
...it,
|
|
72
96
|
matchRule: acl.roles[it.role].content?.assumeMembership ?? {},
|
package/src/graphql/execution.ts
CHANGED
|
@@ -38,11 +38,11 @@ export const createGraphQLQueryHandler = <Context>({
|
|
|
38
38
|
let schemaValidated = false
|
|
39
39
|
const hitCache = new LRUCache<string, true>({
|
|
40
40
|
max: hitCacheMax,
|
|
41
|
-
|
|
41
|
+
ttl: hitCacheMaxAgeSeconds * 1000,
|
|
42
42
|
})
|
|
43
43
|
const documentCache = new LRUCache<string, DocumentNode>({
|
|
44
44
|
max: documentCacheMax,
|
|
45
|
-
|
|
45
|
+
ttl: documentCacheMaxAgeSeconds * 1000,
|
|
46
46
|
})
|
|
47
47
|
let lastPrune = Date.now()
|
|
48
48
|
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { Identity, SystemContainer } from '@contember/engine-system-api'
|
|
2
2
|
import { SystemGraphQLContext } from './SystemGraphQLHandlerFactory'
|
|
3
3
|
import { AuthMiddlewareState, AuthResult } from '../common'
|
|
4
|
-
import { Membership } from '@contember/engine-tenant-api'
|
|
5
4
|
import { KoaContext } from '../koa'
|
|
6
5
|
import { GraphQLKoaState } from '../graphql'
|
|
7
6
|
import { ProjectContainer } from '../ProjectContainer'
|
|
7
|
+
import { Acl } from '@contember/schema'
|
|
8
8
|
|
|
9
9
|
export class SystemGraphQLContextFactory {
|
|
10
10
|
public async create({ authResult, memberships, koaContext, projectContainer, systemContainer, onClearCache }: {
|
|
11
11
|
authResult: AuthResult
|
|
12
|
-
memberships: readonly Membership[]
|
|
12
|
+
memberships: readonly Acl.Membership[]
|
|
13
13
|
koaContext: KoaContext<GraphQLKoaState & AuthMiddlewareState>
|
|
14
14
|
projectContainer: ProjectContainer
|
|
15
15
|
systemContainer: SystemContainer
|
|
@@ -12,7 +12,9 @@ export class ContentSchemaTransferMappingFactory {
|
|
|
12
12
|
name: entity.tableName,
|
|
13
13
|
columns: this.buildDbColumnSchemaMap(contentSchema.model, entity),
|
|
14
14
|
}
|
|
15
|
+
}
|
|
15
16
|
|
|
17
|
+
for (const entity of Object.values(contentSchema.model.entities)) {
|
|
16
18
|
for (const joiningTable of this.collectManyHasManyOwned(contentSchema.model, entity)) {
|
|
17
19
|
tables[joiningTable.name] = joiningTable
|
|
18
20
|
}
|
|
@@ -2,7 +2,6 @@ import { KoaMiddleware, KoaRequestState } from '../koa'
|
|
|
2
2
|
import { AuthResult, HttpError, TimerMiddlewareState } from '../common'
|
|
3
3
|
import { ProjectGroupResolver, ProjectInfoMiddlewareState } from '../project-common'
|
|
4
4
|
import { Readable } from 'stream'
|
|
5
|
-
import { TenantRole } from '@contember/engine-tenant-api'
|
|
6
5
|
import { toBuffer } from './CommandStream'
|
|
7
6
|
import { ExportExecutor, ExportRequest } from './ExportExecutor'
|
|
8
7
|
import { ParseError } from '@contember/typesafe'
|
|
@@ -32,10 +31,6 @@ export class ExportApiMiddlewareFactory {
|
|
|
32
31
|
const authResult = await groupContainer.authenticator.authenticate({ request, timer })
|
|
33
32
|
koaContext.state.authResult = authResult
|
|
34
33
|
|
|
35
|
-
if (!authResult.roles.includes(TenantRole.SUPER_ADMIN) && !authResult.roles.includes(TenantRole.PROJECT_ADMIN)) {
|
|
36
|
-
throw new HttpError(`Not allowed`, 403)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
34
|
let exportRequest: ExportRequest
|
|
40
35
|
|
|
41
36
|
try {
|
|
@@ -64,6 +59,28 @@ export class ExportApiMiddlewareFactory {
|
|
|
64
59
|
throw new HttpError(`Project ${project.slug} NOT found`, 400)
|
|
65
60
|
}
|
|
66
61
|
|
|
62
|
+
const systemContext = projectContainer.systemDatabaseContextFactory.create()
|
|
63
|
+
const schema = await projectContainer.contentSchemaResolver.getSchema(systemContext, project.slug)
|
|
64
|
+
const memberships = await timer('MembershipFetch', () => groupContainer.projectMembershipResolver.resolveMemberships({
|
|
65
|
+
request: { get: () => '' },
|
|
66
|
+
acl: schema.acl,
|
|
67
|
+
projectSlug: project.slug,
|
|
68
|
+
identity: {
|
|
69
|
+
identityId: authResult.identityId,
|
|
70
|
+
roles: authResult.roles,
|
|
71
|
+
},
|
|
72
|
+
}))
|
|
73
|
+
|
|
74
|
+
const projectRoles = memberships.map(it => it.role)
|
|
75
|
+
|
|
76
|
+
if (!projectRoles.some(role => schema.acl.roles[role]?.content?.export)) {
|
|
77
|
+
throw new HttpError(`Not allowed`, 403)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (project.system && !projectRoles.some(role => schema.acl.roles[role]?.system?.export)) {
|
|
81
|
+
throw new HttpError(`Not allowed`, 403)
|
|
82
|
+
}
|
|
83
|
+
|
|
67
84
|
projectContainers[project.slug] = projectContainer
|
|
68
85
|
}
|
|
69
86
|
|
|
@@ -30,10 +30,6 @@ export class ImportApiMiddlewareFactory {
|
|
|
30
30
|
const authResult = await groupContainer.authenticator.authenticate({ request, timer })
|
|
31
31
|
koaContext.state.authResult = authResult
|
|
32
32
|
|
|
33
|
-
if (!authResult.roles.includes(TenantRole.SUPER_ADMIN) && !authResult.roles.includes(TenantRole.PROJECT_ADMIN)) {
|
|
34
|
-
throw new HttpError(`Not allowed`, 403)
|
|
35
|
-
}
|
|
36
|
-
|
|
37
33
|
if (request.headers['content-type'] !== 'application/x-ndjson') {
|
|
38
34
|
throw new HttpError(`Unsupported content type`, 400)
|
|
39
35
|
}
|
|
@@ -47,7 +43,7 @@ export class ImportApiMiddlewareFactory {
|
|
|
47
43
|
|
|
48
44
|
response.status = 200
|
|
49
45
|
response.headers['Content-Type'] = 'application/x-ndjson'
|
|
50
|
-
response.body = Readable.from(toBuffer(this.importExecutor.import(groupContainer, commands), 0))
|
|
46
|
+
response.body = Readable.from(toBuffer(this.importExecutor.import(groupContainer, authResult, commands), 0))
|
|
51
47
|
}
|
|
52
48
|
}
|
|
53
49
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { asyncIterableTransaction, Client, Connection, ConstraintHelper, wrapIdentifier } from '@contember/database'
|
|
2
|
-
import { Model } from '@contember/schema'
|
|
2
|
+
import { Model, Schema } from '@contember/schema'
|
|
3
3
|
import * as Typesafe from '@contember/typesafe'
|
|
4
4
|
import { ParseError } from '@contember/typesafe'
|
|
5
5
|
import { Command, CommandArgsMap, CommandName } from './Command'
|
|
@@ -9,6 +9,7 @@ import { DbColumnSchema, TransferMapping, TransferTableMapping } from './Transfe
|
|
|
9
9
|
import { Logger } from '@contember/engine-common'
|
|
10
10
|
import { ContentSchemaTransferMappingFactory } from './ContentSchemaTransferMappingFactory'
|
|
11
11
|
import { SystemSchemaTransferMappingFactory } from './SystemSchemaTransferMappingFactory'
|
|
12
|
+
import { AuthResult, HttpError } from '../common/index'
|
|
12
13
|
|
|
13
14
|
type Cell = boolean | number | string | null
|
|
14
15
|
type Row = readonly Cell[]
|
|
@@ -74,13 +75,13 @@ export class ImportExecutor {
|
|
|
74
75
|
) {
|
|
75
76
|
}
|
|
76
77
|
|
|
77
|
-
async* import(groupContainer: ProjectGroupContainer, commands: AsyncIterable<Command>): AsyncIterable<ImportProgress> {
|
|
78
|
+
async* import(groupContainer: ProjectGroupContainer, authResult: AuthResult, commands: AsyncIterable<Command>): AsyncIterable<ImportProgress> {
|
|
78
79
|
try {
|
|
79
80
|
const it = await CommandIterator.create(commands)
|
|
80
81
|
|
|
81
82
|
const final = yield* this.match(it, {
|
|
82
|
-
importContentSchemaBegin: it => this.importContentSchemaBegin(groupContainer, it),
|
|
83
|
-
importSystemSchemaBegin: it => this.importSystemSchemaBegin(groupContainer, it),
|
|
83
|
+
importContentSchemaBegin: it => this.importContentSchemaBegin(groupContainer, authResult, it),
|
|
84
|
+
importSystemSchemaBegin: it => this.importSystemSchemaBegin(groupContainer, authResult, it),
|
|
84
85
|
})
|
|
85
86
|
|
|
86
87
|
if (final !== null) {
|
|
@@ -114,7 +115,7 @@ export class ImportExecutor {
|
|
|
114
115
|
return next
|
|
115
116
|
}
|
|
116
117
|
|
|
117
|
-
private async* importContentSchemaBegin(groupContainer: ProjectGroupContainer, it: CommandIterator<'importContentSchemaBegin'>) {
|
|
118
|
+
private async* importContentSchemaBegin(groupContainer: ProjectGroupContainer, authResult: AuthResult, it: CommandIterator<'importContentSchemaBegin'>) {
|
|
118
119
|
const [options] = it.commandArgs
|
|
119
120
|
const projectContainer = await this.createProjectContainer(groupContainer, options.project)
|
|
120
121
|
const systemDatabaseContext = projectContainer.systemDatabaseContextFactory.create()
|
|
@@ -126,6 +127,7 @@ export class ImportExecutor {
|
|
|
126
127
|
throw new ImportError(`Incompatible schema version (import version ${options.schemaVersion} does not match server version ${contentSchema.version})`)
|
|
127
128
|
}
|
|
128
129
|
|
|
130
|
+
await this.requireImportAccess(groupContainer, contentSchema, authResult, options.project, 'content')
|
|
129
131
|
const contentDatabaseClient = projectContainer.connection.createClient(stage.schema, {})
|
|
130
132
|
const mapping = this.contentSchemaTransferMappingFactory.createContentSchemaMapping(contentSchema)
|
|
131
133
|
|
|
@@ -151,9 +153,16 @@ export class ImportExecutor {
|
|
|
151
153
|
})
|
|
152
154
|
}
|
|
153
155
|
|
|
154
|
-
private async* importSystemSchemaBegin(groupContainer: ProjectGroupContainer, it: CommandIterator<'importSystemSchemaBegin'>) {
|
|
156
|
+
private async* importSystemSchemaBegin(groupContainer: ProjectGroupContainer, authResult: AuthResult, it: CommandIterator<'importSystemSchemaBegin'>) {
|
|
155
157
|
const [options] = it.commandArgs
|
|
156
158
|
const projectContainer = await this.createProjectContainer(groupContainer, options.project)
|
|
159
|
+
const schema = await groupContainer.projectSchemaResolver.getSchema(options.project)
|
|
160
|
+
|
|
161
|
+
if (schema === undefined) {
|
|
162
|
+
throw new ImportError(`Project ${options.project} does not have schema`)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
await this.requireImportAccess(groupContainer, schema, authResult, options.project, 'system')
|
|
157
166
|
const systemDatabaseContext = projectContainer.systemDatabaseContextFactory.create()
|
|
158
167
|
const mapping = this.systemSchemaTransferMappingFactory.build()
|
|
159
168
|
|
|
@@ -251,6 +260,24 @@ export class ImportExecutor {
|
|
|
251
260
|
return projectContainer
|
|
252
261
|
}
|
|
253
262
|
|
|
263
|
+
private async requireImportAccess(groupContainer: ProjectGroupContainer, schema: Schema, authResult: AuthResult, projectSlug: string, kind: 'content' | 'system') {
|
|
264
|
+
const memberships = await groupContainer.projectMembershipResolver.resolveMemberships({
|
|
265
|
+
request: { get: () => '' },
|
|
266
|
+
acl: schema.acl,
|
|
267
|
+
projectSlug: projectSlug,
|
|
268
|
+
identity: {
|
|
269
|
+
identityId: authResult.identityId,
|
|
270
|
+
roles: authResult.roles,
|
|
271
|
+
},
|
|
272
|
+
})
|
|
273
|
+
|
|
274
|
+
const projectRoles = memberships.map(it => it.role)
|
|
275
|
+
|
|
276
|
+
if (!projectRoles.some(role => schema.acl.roles[role]?.[kind]?.import)) {
|
|
277
|
+
throw new HttpError(`You are not allowed to import ${kind} in ${projectSlug}`, 403)
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
254
281
|
private async fetchStageBySlug(systemDatabaseContext: DatabaseContext, projectSlug: string, stageSlug: string) {
|
|
255
282
|
const stage = await systemDatabaseContext.queryHandler.fetch(new StageBySlugQuery(stageSlug))
|
|
256
283
|
|
|
@@ -9,27 +9,87 @@ export class SystemSchemaTransferMappingFactory {
|
|
|
9
9
|
return {
|
|
10
10
|
tables: this.associateByName([
|
|
11
11
|
this.buildEventDataMapping(),
|
|
12
|
-
|
|
13
|
-
name: 'stage',
|
|
14
|
-
columns: this.associateByName([
|
|
15
|
-
{ name: 'id', type: Model.ColumnType.Uuid },
|
|
16
|
-
{ name: 'name', type: Model.ColumnType.String },
|
|
17
|
-
{ name: 'slug', type: Model.ColumnType.String }, // TODO: validate?
|
|
18
|
-
{ name: 'schema', type: Model.ColumnType.String },
|
|
19
|
-
]),
|
|
20
|
-
},
|
|
21
|
-
{
|
|
22
|
-
name: 'stage_transaction',
|
|
23
|
-
columns: this.associateByName([
|
|
24
|
-
{ name: 'transaction_id', type: Model.ColumnType.Uuid },
|
|
25
|
-
{ name: 'stage_id', type: Model.ColumnType.Uuid },
|
|
26
|
-
{ name: 'applied_at', type: Model.ColumnType.DateTime },
|
|
27
|
-
]),
|
|
28
|
-
},
|
|
12
|
+
this.buildStageTransactionDataMapping(),
|
|
29
13
|
]),
|
|
30
14
|
}
|
|
31
15
|
}
|
|
32
16
|
|
|
17
|
+
private buildStageTransactionDataMapping(): TransferTableMapping {
|
|
18
|
+
return {
|
|
19
|
+
name: 'stage_transaction',
|
|
20
|
+
|
|
21
|
+
columns: this.associateByName([
|
|
22
|
+
{ name: 'transaction_id', type: Model.ColumnType.Uuid },
|
|
23
|
+
{ name: 'stage_slug', type: Model.ColumnType.String },
|
|
24
|
+
{ name: 'applied_at', type: Model.ColumnType.DateTime },
|
|
25
|
+
]),
|
|
26
|
+
|
|
27
|
+
createSelect: (db, table) => {
|
|
28
|
+
const namespaceContext = new Compiler.Context(db.schema, new Set())
|
|
29
|
+
|
|
30
|
+
let builder = db.selectBuilder()
|
|
31
|
+
|
|
32
|
+
for (const column of Object.values(table.columns)) {
|
|
33
|
+
if (column.name === 'stage_slug') {
|
|
34
|
+
builder = builder.select(['stage', 'slug'], 'stage_slug')
|
|
35
|
+
|
|
36
|
+
} else if (column.type === Model.ColumnType.Json || column.type === Model.ColumnType.Date || column.type === Model.ColumnType.DateTime) {
|
|
37
|
+
builder = builder.select(expr => expr.raw(`${wrapIdentifier('stage_transaction')}.${wrapIdentifier(column.name)}::text`))
|
|
38
|
+
|
|
39
|
+
} else {
|
|
40
|
+
builder = builder.select(['stage_transaction', column.name])
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return builder
|
|
45
|
+
.from('stage_transaction')
|
|
46
|
+
.join(
|
|
47
|
+
'stage',
|
|
48
|
+
undefined,
|
|
49
|
+
expr => expr.compareColumns(['stage', 'id'], Operator.eq, ['stage_transaction', 'stage_id']),
|
|
50
|
+
)
|
|
51
|
+
.createQuery(namespaceContext)
|
|
52
|
+
},
|
|
53
|
+
|
|
54
|
+
createInsertStartFragment: (schema, tableName, columnNames) => {
|
|
55
|
+
const columnNames2 = columnNames.map(name => name === 'stage_slug' ? 'stage_id' : name)
|
|
56
|
+
const tableQuoted = `${wrapIdentifier(schema)}.${wrapIdentifier(tableName)}`
|
|
57
|
+
const columnsQuoted = '(' + columnNames2.map(it => wrapIdentifier(it)).join(', ') + ')'
|
|
58
|
+
return `INSERT INTO ${tableQuoted} ${columnsQuoted} VALUES\n`
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
createRowParser: async (db, columns, baseType) => {
|
|
62
|
+
const stages = await db.selectBuilder<{id: string; slug: string}>()
|
|
63
|
+
.select('id')
|
|
64
|
+
.select('slug')
|
|
65
|
+
.from('stage')
|
|
66
|
+
.getResult(db)
|
|
67
|
+
|
|
68
|
+
const stageSlugToId = new Map(stages.map(row => ([row.slug, row.id])))
|
|
69
|
+
const stageSlugColumnIndex = columns.indexOf('stage_slug')
|
|
70
|
+
|
|
71
|
+
if (stageSlugColumnIndex < 0) {
|
|
72
|
+
return baseType
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return (input, path) => {
|
|
76
|
+
const row = baseType(input, path)
|
|
77
|
+
const stageSlug = Typesafe.string(row[stageSlugColumnIndex])
|
|
78
|
+
const stageSlugId = stageSlugToId.get(stageSlug)
|
|
79
|
+
|
|
80
|
+
if (stageSlugId === undefined) {
|
|
81
|
+
throw new ImportError(`Unknown stage slug ${stageSlug}`)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const newRow = [...row]
|
|
85
|
+
newRow[stageSlugColumnIndex] = stageSlugId
|
|
86
|
+
|
|
87
|
+
return newRow
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
33
93
|
private buildEventDataMapping(): TransferTableMapping {
|
|
34
94
|
return {
|
|
35
95
|
name: 'event_data',
|
|
@@ -74,18 +134,7 @@ export class SystemSchemaTransferMappingFactory {
|
|
|
74
134
|
},
|
|
75
135
|
|
|
76
136
|
createInsertStartFragment: (schema, tableName, columnNames) => {
|
|
77
|
-
const columnNames2 =
|
|
78
|
-
'id',
|
|
79
|
-
'type',
|
|
80
|
-
'table_name',
|
|
81
|
-
'row_ids',
|
|
82
|
-
'values',
|
|
83
|
-
'created_at',
|
|
84
|
-
'schema_id',
|
|
85
|
-
'identity_id',
|
|
86
|
-
'transaction_id',
|
|
87
|
-
]
|
|
88
|
-
|
|
137
|
+
const columnNames2 = columnNames.map(name => name === 'schema_version' ? 'schema_id' : name)
|
|
89
138
|
const tableQuoted = `${wrapIdentifier(schema)}.${wrapIdentifier(tableName)}`
|
|
90
139
|
const columnsQuoted = '(' + columnNames2.map(it => wrapIdentifier(it)).join(', ') + ')'
|
|
91
140
|
return `INSERT INTO ${tableQuoted} ${columnsQuoted} VALUES\n`
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest'
|
|
2
2
|
import { ProjectMembershipFetcher, ProjectMembershipResolver } from '../../../src'
|
|
3
3
|
import { createMock } from '../../utils'
|
|
4
|
-
import {
|
|
4
|
+
import { Acl } from '@contember/schema'
|
|
5
5
|
|
|
6
6
|
describe('membership resolver', () => {
|
|
7
7
|
it('should return implicit role', async () => {
|
|
8
8
|
const membershipResolver = new ProjectMembershipResolver(false, createMock<ProjectMembershipFetcher>({
|
|
9
|
-
fetchMemberships(): Promise<readonly Membership[]> {
|
|
9
|
+
fetchMemberships(): Promise<readonly Acl.Membership[]> {
|
|
10
10
|
return Promise.resolve([])
|
|
11
11
|
},
|
|
12
12
|
}))
|
|
@@ -16,7 +16,7 @@ describe('membership resolver', () => {
|
|
|
16
16
|
},
|
|
17
17
|
projectSlug: 'test',
|
|
18
18
|
identity: {
|
|
19
|
-
|
|
19
|
+
identityId: 'd4141336-6512-41ef-a25a-374de35a2806',
|
|
20
20
|
roles: [],
|
|
21
21
|
},
|
|
22
22
|
acl: {
|
|
@@ -35,7 +35,7 @@ describe('membership resolver', () => {
|
|
|
35
35
|
|
|
36
36
|
it('should return fetched memberships', async () => {
|
|
37
37
|
const membershipResolver = new ProjectMembershipResolver(false, createMock<ProjectMembershipFetcher>({
|
|
38
|
-
fetchMemberships(): Promise<readonly Membership[]> {
|
|
38
|
+
fetchMemberships(): Promise<readonly Acl.Membership[]> {
|
|
39
39
|
return Promise.resolve([{ role: 'test', variables: [] }])
|
|
40
40
|
},
|
|
41
41
|
}))
|
|
@@ -45,11 +45,16 @@ describe('membership resolver', () => {
|
|
|
45
45
|
},
|
|
46
46
|
projectSlug: 'test',
|
|
47
47
|
identity: {
|
|
48
|
-
|
|
48
|
+
identityId: 'd4141336-6512-41ef-a25a-374de35a2806',
|
|
49
49
|
roles: [],
|
|
50
50
|
},
|
|
51
51
|
acl: {
|
|
52
|
-
roles: {
|
|
52
|
+
roles: {
|
|
53
|
+
test: {
|
|
54
|
+
variables: {},
|
|
55
|
+
entities: {},
|
|
56
|
+
},
|
|
57
|
+
},
|
|
53
58
|
},
|
|
54
59
|
})
|
|
55
60
|
expect(resolvedMembership).deep.eq([{ role: 'test', variables: [] }])
|
|
@@ -57,19 +62,19 @@ describe('membership resolver', () => {
|
|
|
57
62
|
|
|
58
63
|
it('should return assumed membership', async () => {
|
|
59
64
|
const membershipResolver = new ProjectMembershipResolver(false, createMock<ProjectMembershipFetcher>({
|
|
60
|
-
fetchMemberships(): Promise<readonly Membership[]> {
|
|
65
|
+
fetchMemberships(): Promise<readonly Acl.Membership[]> {
|
|
61
66
|
return Promise.resolve([{ role: 'admin', variables: [] }])
|
|
62
67
|
},
|
|
63
68
|
}))
|
|
64
69
|
const resolvedMembership = await membershipResolver.resolveMemberships({
|
|
65
70
|
request: {
|
|
66
71
|
get: () => JSON.stringify({
|
|
67
|
-
memberships: [{ role: 'test', variables: [{ name: 'lang', values: ['cs'] }] }],
|
|
72
|
+
memberships: [{ role: 'test', variables: [{ name: 'lang', values: [JSON.stringify({ eq: 'cs' })] }] }],
|
|
68
73
|
}),
|
|
69
74
|
},
|
|
70
75
|
projectSlug: 'test',
|
|
71
76
|
identity: {
|
|
72
|
-
|
|
77
|
+
identityId: 'd4141336-6512-41ef-a25a-374de35a2806',
|
|
73
78
|
roles: [],
|
|
74
79
|
},
|
|
75
80
|
acl: {
|
|
@@ -85,10 +90,18 @@ describe('membership resolver', () => {
|
|
|
85
90
|
},
|
|
86
91
|
},
|
|
87
92
|
},
|
|
93
|
+
test: {
|
|
94
|
+
variables: {
|
|
95
|
+
lang: {
|
|
96
|
+
type: Acl.VariableType.condition,
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
entities: {},
|
|
100
|
+
},
|
|
88
101
|
},
|
|
89
102
|
},
|
|
90
103
|
})
|
|
91
|
-
expect(resolvedMembership).deep.eq([{ role: 'test', variables: [{ name: 'lang',
|
|
104
|
+
expect(resolvedMembership).deep.eq([{ role: 'test', variables: [{ name: 'lang', condition: { eq: 'cs' } }] }])
|
|
92
105
|
})
|
|
93
106
|
|
|
94
107
|
})
|