Skip to content

Commit

Permalink
feat: add list rule
Browse files Browse the repository at this point in the history
  • Loading branch information
genu committed Jan 2, 2025
1 parent 2eecae5 commit 1eb735c
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ export class PolicyUtil extends QueryUtils {
create: { guard: true, inputChecker: true },
update: { guard: true },
delete: { guard: true },
list: { guard: true },
postUpdate: { guard: true },
},
};
Expand Down
6 changes: 6 additions & 0 deletions packages/runtime/src/enhancements/node/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export type ModelCrudDef = {
create: ModelCreateDef;
update: ModelUpdateDef;
delete: ModelDeleteDef;
list: ModelListDef;
postUpdate: ModelPostUpdateDef;
};

Expand Down Expand Up @@ -207,6 +208,11 @@ type ModelUpdateDef = ModelCrudCommon;
*/
type ModelDeleteDef = ModelCrudCommon;

/**
* Policy definition for listing a model
*/
type ModelListDef = ModelCrudCommon;

/**
* Policy definition for post-update checking a model
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface DbOperations {
*/
export type PolicyKind = 'allow' | 'deny';

export type PolicyCrudKind = 'read' | 'create' | 'update' | 'delete';
export type PolicyCrudKind = 'read' | 'create' | 'update' | 'delete' | 'list';

/**
* Kinds of operations controlled by access policies
Expand Down
4 changes: 2 additions & 2 deletions packages/schema/src/res/stdlib.zmodel
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ attribute @@schema(_ name: String) @@@prisma
* @param operation: comma-separated list of "create", "read", "update", "delete". Use "all" to denote all operations.
* @param condition: a boolean expression that controls if the operation should be allowed.
*/
attribute @@allow(_ operation: String @@@completionHint(["'create'", "'read'", "'update'", "'delete'", "'all'"]), _ condition: Boolean)
attribute @@allow(_ operation: String @@@completionHint(["'create'", "'read'", "'update'", "'delete'", "'list'", "'all'"]), _ condition: Boolean)

/**
* Defines an access policy that allows the annotated field to be read or updated.
Expand All @@ -545,7 +545,7 @@ attribute @allow(_ operation: String @@@completionHint(["'create'", "'read'", "'
* @param operation: comma-separated list of "create", "read", "update", "delete". Use "all" to denote all operations.
* @param condition: a boolean expression that controls if the operation should be denied.
*/
attribute @@deny(_ operation: String @@@completionHint(["'create'", "'read'", "'update'", "'delete'", "'all'"]), _ condition: Boolean)
attribute @@deny(_ operation: String @@@completionHint(["'create'", "'read'", "'update'", "'delete'", "'list'", "'all'"]), _ condition: Boolean)

/**
* Defines an access policy that denies the annotated field to be read or updated.
Expand Down
2 changes: 2 additions & 0 deletions packages/sdk/src/policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export function analyzePolicies(dataModel: DataModel) {
const read = toStaticPolicy('read', allows, denies);
const update = toStaticPolicy('update', allows, denies);
const del = toStaticPolicy('delete', allows, denies);
const list = toStaticPolicy('list', allows, denies);
const hasFieldValidation = hasValidationAttributes(dataModel);

return {
Expand All @@ -21,6 +22,7 @@ export function analyzePolicies(dataModel: DataModel) {
read,
update,
delete: del,
list,
allowAll: create === true && read === true && update === true && del === true,
denyAll: create === false && read === false && update === false && del === false,
hasFieldValidation,
Expand Down

0 comments on commit 1eb735c

Please sign in to comment.