Skip to content

Commit 5a55971

Browse files
committedJul 15, 2021
feature(grants): add support for validating built-in $identity param
To support more advanced ACL where users have access to edit their own documents we need to inject the user id when evaluating the grant filters
1 parent 81b6149 commit 5a55971

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed
 

‎packages/@sanity/base/src/datastores/grants/createGrantsStore.ts

+21-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ import {evaluate, parse} from 'groq-js'
55
import {SanityDocument} from '@sanity/types'
66
import {refCountDelay} from 'rxjs-etc/operators'
77
import sanityClient from 'part:@sanity/base/client'
8-
import {GrantsStore, DocumentPermissionName, Grant, PermissionCheckResult} from './types'
8+
import userStore from '../user'
9+
import {
10+
GrantsStore,
11+
DocumentPermissionName,
12+
Grant,
13+
PermissionCheckResult,
14+
EvaluationParams,
15+
} from './types'
916
import {debugGrants$} from './debug'
1017

1118
const client = sanityClient.withConfig({apiVersion: '2021-06-07'})
@@ -21,6 +28,16 @@ async function getDatasetGrants(projectId: string, dataset: string): Promise<Gra
2128
return grants
2229
}
2330

31+
async function getParams(): Promise<EvaluationParams> {
32+
const params: EvaluationParams = {}
33+
const user = await userStore.getCurrentUser()
34+
if (user !== null) {
35+
params.identity = user.id
36+
}
37+
38+
return params
39+
}
40+
2441
const PARSED_FILTERS_MEMO = new Map()
2542
async function matchesFilter(filter: string, document: SanityDocument) {
2643
if (!PARSED_FILTERS_MEMO.has(filter)) {
@@ -32,7 +49,9 @@ async function matchesFilter(filter: string, document: SanityDocument) {
3249
PARSED_FILTERS_MEMO.set(filter, parse(`*[${filter}]`))
3350
}
3451
const parsed = PARSED_FILTERS_MEMO.get(filter)
35-
const data = await (await evaluate(parsed, {dataset: [document]})).get()
52+
53+
const params = await getParams()
54+
const data = await (await evaluate(parsed, {dataset: [document], params})).get()
3655
return data?.length === 1
3756
}
3857

‎packages/@sanity/base/src/datastores/grants/highlevel.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@ function getSchemaType(typeName: string): SchemaType {
1818
return type
1919
}
2020

21-
export function canCreateType(typeName: string) {
21+
export function canCreateType(id: string, typeName: string) {
2222
const type = getSchemaType(typeName)
2323
return from(resolveInitialValueForType(type)).pipe(
2424
mergeMap((initialValue: any) => {
2525
return grantsStore.checkDocumentPermission('create', {
2626
...initialValue,
27-
_id: type.liveEdit ? 'dummy-id' : 'drafts.dummy-id',
27+
_id: type.liveEdit ? id : `drafts.${id}`,
2828
_type: typeName,
2929
})
3030
})
3131
)
3232
}
3333

3434
export function canCreateAnyOf(types: string[]) {
35-
return combineLatest(types.map((typeName) => canCreateType(typeName))).pipe(
35+
return combineLatest(types.map((typeName) => canCreateType('dummy-id', typeName))).pipe(
3636
map((results) => {
3737
const granted = results.some((res) => res.granted)
3838
return {

‎packages/@sanity/base/src/datastores/grants/hooks.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export function unstable_useCheckDocumentPermission(
5252
return canUpdate(id, type)
5353
}
5454
if (permission === 'create') {
55-
return canCreateType(type)
55+
return canCreateType(id, type)
5656
}
5757
if (permission === 'publish') {
5858
return canPublish(id, type)

‎packages/@sanity/base/src/datastores/grants/types.ts

+4
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ export interface GrantsStore {
1919
document: Partial<SanityDocument>
2020
): Observable<PermissionCheckResult>
2121
}
22+
23+
export interface EvaluationParams {
24+
identity?: string
25+
}

2 commit comments

Comments
 (2)

vercel[bot] commented on Jul 15, 2021

@vercel[bot]

Successfully deployed to the following URLs:

test-studio – ./

test-studio.sanity.build
test-studio-git-next.sanity.build

vercel[bot] commented on Jul 15, 2021

@vercel[bot]

Successfully deployed to the following URLs:

perf-studio – ./

perf-studio.sanity.build
perf-studio-git-next.sanity.build

Please sign in to comment.