Skip to content

Commit 7ffb723

Browse files
committed
feat: add support for boolean
1 parent 2504e8e commit 7ffb723

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
Expression,
66
Model,
77
isBinaryExpr,
8+
isUnaryExpr,
89
isDataModel,
910
isDataModelField,
1011
isEnum,
@@ -1070,7 +1071,10 @@ export class PolicyGenerator {
10701071
collectVariablesTypes(expr: Expression): Record<string, Expression['$type']> {
10711072
const result: Record<string, Expression['$type']> = {};
10721073
const visit = (node: Expression) => {
1073-
if (isBinaryExpr(node) && typeof (node.right.$type !== 'StringLiteral')) {
1074+
if (isReferenceExpr(node)) {
1075+
const variableName = node.target.ref?.name ?? 'unknown';
1076+
result[variableName] = 'BooleanLiteral';
1077+
} else if (isBinaryExpr(node) && typeof (node.right.$type !== 'StringLiteral')) {
10741078
if (isReferenceExpr(node.left)) {
10751079
// const variableName = `${lowerCaseFirst(
10761080
// node.left.target.ref?.$container.name ?? ''
@@ -1084,7 +1088,7 @@ export class PolicyGenerator {
10841088
visit(node.left);
10851089
visit(node.right);
10861090
}
1087-
} else if (isMemberAccessExpr(node)) {
1091+
} else if (isMemberAccessExpr(node) || isUnaryExpr(node)) {
10881092
visit(node.operand);
10891093
}
10901094
};

packages/sdk/src/z3-expression-transformer.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,10 @@ export class Z3ExpressionTransformer {
313313
}
314314

315315
private unary(expr: UnaryExpr): string {
316-
return `(${expr.operator} ${this.transform(expr.operand)})`;
316+
if (expr.operator !== '!') {
317+
throw new Z3ExpressionTransformerError(`Unsupported unary operator: ${expr.operator}`);
318+
}
319+
return `z3.Not(${this.transform(expr.operand)})`;
317320
}
318321

319322
private isModelType(expr: Expression) {

tests/integration/tests/enhancements/with-policy/permissions-checker.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ describe('With Policy: permissions checker test', () => {
3737
id String @id @default(uuid())
3838
title String
3939
rating Int
40+
published Boolean @default(false)
4041
authorId String @default("userId-1")
4142
author User @relation(fields: [authorId], references: [id])
4243
comments Comment[]
4344
4445
4546
@@allow('create,read', auth() == author && title == "Title" && rating > 1)
47+
@@deny('read', !published || published == false)
4648
4749
@@deny('update', rating < 10)
4850
@@allow('update', rating > 5)
@@ -95,6 +97,8 @@ describe('With Policy: permissions checker test', () => {
9597
await expect(authDb.post.check('update', { rating: 8 })).toResolveFalsy();
9698
await expect(db.post.check('delete', {})).toResolveFalsy();
9799
await expect(authDb.post.check('delete', {})).toResolveTruthy();
100+
await expect(authDb.post.check('read', { published: true })).toResolveTruthy();
101+
await expect(authDb.post.check('read', { published: false })).toResolveFalsy();
98102

99103
await expect(db.comment.check('delete', {})).toResolveFalsy();
100104
await expect(authDb.comment.check('delete', {})).toResolveTruthy();

0 commit comments

Comments
 (0)