-
-
Notifications
You must be signed in to change notification settings - Fork 12
feat(policy): implementing check function
#255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { type OperationNode, AliasNode, ColumnNode, ReferenceNode, TableNode } from 'kysely'; | ||
|
|
||
| /** | ||
| * Strips alias from the node if it exists. | ||
| */ | ||
| export function stripAlias(node: OperationNode) { | ||
| if (AliasNode.is(node)) { | ||
| return { alias: node.alias, node: node.node }; | ||
| } else { | ||
| return { alias: undefined, node }; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Extracts model name from an OperationNode. | ||
| */ | ||
| export function extractModelName(node: OperationNode) { | ||
| const { node: innerNode } = stripAlias(node); | ||
| return TableNode.is(innerNode!) ? innerNode!.table.identifier.name : undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Extracts field name from an OperationNode. | ||
| */ | ||
| export function extractFieldName(node: OperationNode) { | ||
| if (ReferenceNode.is(node) && ColumnNode.is(node.column)) { | ||
| return node.column.column.name; | ||
| } else if (ColumnNode.is(node)) { | ||
| return node.column.name; | ||
| } else { | ||
| return undefined; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { invariant } from '@zenstackhq/common-helpers'; | ||
| import { ExpressionWrapper, ValueNode, type Expression, type ExpressionBuilder } from 'kysely'; | ||
| import { CRUD } from '../../client/contract'; | ||
| import { extractFieldName } from '../../client/kysely-utils'; | ||
| import type { ZModelFunction, ZModelFunctionContext } from '../../client/options'; | ||
| import { buildJoinPairs, requireField } from '../../client/query-utils'; | ||
| import { PolicyHandler } from './policy-handler'; | ||
|
|
||
| /** | ||
| * Relation checker implementation. | ||
| */ | ||
| export const check: ZModelFunction<any> = ( | ||
| eb: ExpressionBuilder<any, any>, | ||
| args: Expression<any>[], | ||
| { client, model, modelAlias, operation }: ZModelFunctionContext<any>, | ||
| ) => { | ||
| invariant(args.length === 1 || args.length === 2, '"check" function requires 1 or 2 arguments'); | ||
|
|
||
| const arg1Node = args[0]!.toOperationNode(); | ||
|
|
||
| const arg2Node = args.length === 2 ? args[1]!.toOperationNode() : undefined; | ||
| if (arg2Node) { | ||
| invariant( | ||
| ValueNode.is(arg2Node) && typeof arg2Node.value === 'string', | ||
| '"operation" parameter must be a string literal when provided', | ||
| ); | ||
| invariant( | ||
| CRUD.includes(arg2Node.value as CRUD), | ||
| '"operation" parameter must be one of "create", "read", "update", "delete"', | ||
| ); | ||
| } | ||
|
|
||
| // first argument must be a field reference | ||
| const fieldName = extractFieldName(arg1Node); | ||
| invariant(fieldName, 'Failed to extract field name from the first argument of "check" function'); | ||
| const fieldDef = requireField(client.$schema, model, fieldName); | ||
| invariant(fieldDef.relation, `Field "${fieldName}" is not a relation field in model "${model}"`); | ||
| invariant(!fieldDef.array, `Field "${fieldName}" is a to-many relation, which is not supported by "check"`); | ||
| const relationModel = fieldDef.type; | ||
|
|
||
| const op = arg2Node ? (arg2Node.value as CRUD) : operation; | ||
ymc9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const policyHandler = new PolicyHandler(client); | ||
|
|
||
| // join with parent model | ||
| const joinPairs = buildJoinPairs(client.$schema, model, modelAlias, fieldName, relationModel); | ||
| const joinCondition = | ||
| joinPairs.length === 1 | ||
| ? eb(eb.ref(joinPairs[0]![0]), '=', eb.ref(joinPairs[0]![1])) | ||
| : eb.and(joinPairs.map(([left, right]) => eb(eb.ref(left), '=', eb.ref(right)))); | ||
|
|
||
| // policy condition of the related model | ||
| const policyCondition = policyHandler.buildPolicyFilter(relationModel, undefined, op); | ||
|
|
||
| // build the final nested select that evaluates the policy condition | ||
| const result = eb | ||
| .selectFrom(relationModel) | ||
| .where(joinCondition) | ||
| .select(new ExpressionWrapper(policyCondition).as('$condition')); | ||
|
|
||
| return result; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.