Skip to content

Commit acd0753

Browse files
authored
fix: typing of policy definition (#640)
1 parent 9a6f39b commit acd0753

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

packages/runtime/src/enhancements/policy/policy-utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { AuthUser, DbClientContract, DbOperations, FieldInfo, PolicyOperationKin
1818
import { getVersion } from '../../version';
1919
import { getFields, resolveField } from '../model-meta';
2020
import { NestedWriteVisitorContext } from '../nested-write-vistor';
21-
import type { InputCheckFunc, ModelMeta, PolicyDef, PolicyFunc, ReadFieldCheckFunc, ZodSchemas } from '../types';
21+
import type { InputCheckFunc, ModelMeta, PolicyDef, ReadFieldCheckFunc, ZodSchemas } from '../types';
2222
import {
2323
formatObject,
2424
getIdFields,
@@ -223,7 +223,7 @@ export class PolicyUtil {
223223
if (!guard) {
224224
return false;
225225
}
226-
const provider: PolicyFunc | boolean | undefined = guard[operation];
226+
const provider = guard[operation];
227227
return typeof provider !== 'boolean' || provider !== true;
228228
}
229229

packages/runtime/src/enhancements/types.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import { z } from 'zod';
3+
import type { DbOperations, FieldInfo, PolicyOperationKind, QueryContext } from '../types';
34
import {
45
FIELD_LEVEL_READ_CHECKER_SELECTOR,
5-
HAS_FIELD_LEVEL_POLICY_FLAG,
66
PRE_UPDATE_VALUE_SELECTOR,
7+
FIELD_LEVEL_READ_CHECKER_PREFIX,
8+
FIELD_LEVEL_UPDATE_GUARD_PREFIX,
9+
HAS_FIELD_LEVEL_POLICY_FLAG,
710
} from '../constants';
8-
import type { DbOperations, FieldInfo, PolicyOperationKind, QueryContext } from '../types';
911

1012
/**
1113
* Metadata for a model-level unique constraint
@@ -43,15 +45,18 @@ export type PolicyDef = {
4345
// Prisma query guards
4446
guard: Record<
4547
string,
46-
{
47-
allowAll?: boolean;
48-
denyAll?: boolean;
49-
} & Partial<Record<PolicyOperationKind, PolicyFunc>> & {
50-
create_input: InputCheckFunc;
51-
} & {
48+
// policy operation guard functions
49+
Partial<Record<PolicyOperationKind, PolicyFunc | boolean>> &
50+
// 'create_input' checker function
51+
Partial<Record<`${PolicyOperationKind}_input`, InputCheckFunc | boolean>> &
52+
// field-level read checker functions or update guard functions
53+
Record<`${typeof FIELD_LEVEL_READ_CHECKER_PREFIX}${string}`, ReadFieldCheckFunc> &
54+
Record<`${typeof FIELD_LEVEL_UPDATE_GUARD_PREFIX}${string}`, PolicyFunc> & {
55+
// pre-update value selector
5256
[PRE_UPDATE_VALUE_SELECTOR]?: object;
57+
// field-level read checker selector
5358
[FIELD_LEVEL_READ_CHECKER_SELECTOR]?: object;
54-
} & Record<string, ReadFieldCheckFunc | PolicyFunc> & {
59+
// flag that indicates if the model has field-level access control
5560
[HAS_FIELD_LEVEL_POLICY_FLAG]?: boolean;
5661
}
5762
>;

packages/schema/src/plugins/access-policy/policy-guard-generator.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
FIELD_LEVEL_READ_CHECKER_SELECTOR,
2323
FIELD_LEVEL_UPDATE_GUARD_PREFIX,
2424
HAS_FIELD_LEVEL_POLICY_FLAG,
25+
PRE_UPDATE_VALUE_SELECTOR,
2526
type PolicyKind,
2627
type PolicyOperationKind,
2728
} from '@zenstackhq/runtime';
@@ -74,7 +75,12 @@ export default class PolicyGenerator {
7475
sf.addStatements('/* eslint-disable */');
7576

7677
sf.addImportDeclaration({
77-
namedImports: [{ name: 'type QueryContext' }, { name: 'type DbOperations' }, { name: 'hasAllFields' }],
78+
namedImports: [
79+
{ name: 'type QueryContext' },
80+
{ name: 'type DbOperations' },
81+
{ name: 'hasAllFields' },
82+
{ name: 'type PolicyDef' },
83+
],
7884
moduleSpecifier: `${RUNTIME_PACKAGE}`,
7985
});
8086

@@ -99,6 +105,7 @@ export default class PolicyGenerator {
99105
declarations: [
100106
{
101107
name: 'policy',
108+
type: 'PolicyDef',
102109
initializer: (writer) => {
103110
writer.block(() => {
104111
writer.write('guard:');
@@ -256,7 +263,7 @@ export default class PolicyGenerator {
256263
if (kind === 'postUpdate') {
257264
const preValueSelect = this.generateSelectForRules(allows, denies);
258265
if (preValueSelect) {
259-
result['preValueSelect'] = preValueSelect;
266+
result[PRE_UPDATE_VALUE_SELECTOR] = preValueSelect;
260267
}
261268
}
262269

0 commit comments

Comments
 (0)