Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/runtime/src/enhancements/policy/policy-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { AuthUser, DbClientContract, DbOperations, FieldInfo, PolicyOperationKin
import { getVersion } from '../../version';
import { getFields, resolveField } from '../model-meta';
import { NestedWriteVisitorContext } from '../nested-write-vistor';
import type { InputCheckFunc, ModelMeta, PolicyDef, PolicyFunc, ReadFieldCheckFunc, ZodSchemas } from '../types';
import type { InputCheckFunc, ModelMeta, PolicyDef, ReadFieldCheckFunc, ZodSchemas } from '../types';
import {
formatObject,
getIdFields,
Expand Down Expand Up @@ -223,7 +223,7 @@ export class PolicyUtil {
if (!guard) {
return false;
}
const provider: PolicyFunc | boolean | undefined = guard[operation];
const provider = guard[operation];
return typeof provider !== 'boolean' || provider !== true;
}

Expand Down
23 changes: 14 additions & 9 deletions packages/runtime/src/enhancements/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { z } from 'zod';
import type { DbOperations, FieldInfo, PolicyOperationKind, QueryContext } from '../types';
import {
FIELD_LEVEL_READ_CHECKER_SELECTOR,
HAS_FIELD_LEVEL_POLICY_FLAG,
PRE_UPDATE_VALUE_SELECTOR,
FIELD_LEVEL_READ_CHECKER_PREFIX,
FIELD_LEVEL_UPDATE_GUARD_PREFIX,
HAS_FIELD_LEVEL_POLICY_FLAG,
} from '../constants';
import type { DbOperations, FieldInfo, PolicyOperationKind, QueryContext } from '../types';

/**
* Metadata for a model-level unique constraint
Expand Down Expand Up @@ -43,15 +45,18 @@ export type PolicyDef = {
// Prisma query guards
guard: Record<
string,
{
allowAll?: boolean;
denyAll?: boolean;
} & Partial<Record<PolicyOperationKind, PolicyFunc>> & {
create_input: InputCheckFunc;
} & {
// policy operation guard functions
Partial<Record<PolicyOperationKind, PolicyFunc | boolean>> &
// 'create_input' checker function
Partial<Record<`${PolicyOperationKind}_input`, InputCheckFunc | boolean>> &
// field-level read checker functions or update guard functions
Record<`${typeof FIELD_LEVEL_READ_CHECKER_PREFIX}${string}`, ReadFieldCheckFunc> &
Record<`${typeof FIELD_LEVEL_UPDATE_GUARD_PREFIX}${string}`, PolicyFunc> & {
// pre-update value selector
[PRE_UPDATE_VALUE_SELECTOR]?: object;
// field-level read checker selector
[FIELD_LEVEL_READ_CHECKER_SELECTOR]?: object;
} & Record<string, ReadFieldCheckFunc | PolicyFunc> & {
// flag that indicates if the model has field-level access control
[HAS_FIELD_LEVEL_POLICY_FLAG]?: boolean;
}
>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
FIELD_LEVEL_READ_CHECKER_SELECTOR,
FIELD_LEVEL_UPDATE_GUARD_PREFIX,
HAS_FIELD_LEVEL_POLICY_FLAG,
PRE_UPDATE_VALUE_SELECTOR,
type PolicyKind,
type PolicyOperationKind,
} from '@zenstackhq/runtime';
Expand Down Expand Up @@ -74,7 +75,12 @@ export default class PolicyGenerator {
sf.addStatements('/* eslint-disable */');

sf.addImportDeclaration({
namedImports: [{ name: 'type QueryContext' }, { name: 'type DbOperations' }, { name: 'hasAllFields' }],
namedImports: [
{ name: 'type QueryContext' },
{ name: 'type DbOperations' },
{ name: 'hasAllFields' },
{ name: 'type PolicyDef' },
],
moduleSpecifier: `${RUNTIME_PACKAGE}`,
});

Expand All @@ -99,6 +105,7 @@ export default class PolicyGenerator {
declarations: [
{
name: 'policy',
type: 'PolicyDef',
initializer: (writer) => {
writer.block(() => {
writer.write('guard:');
Expand Down Expand Up @@ -256,7 +263,7 @@ export default class PolicyGenerator {
if (kind === 'postUpdate') {
const preValueSelect = this.generateSelectForRules(allows, denies);
if (preValueSelect) {
result['preValueSelect'] = preValueSelect;
result[PRE_UPDATE_VALUE_SELECTOR] = preValueSelect;
}
}

Expand Down