Skip to content

Commit

Permalink
Many levels of nested filters fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
ackava committed Jul 11, 2023
1 parent 1d4cc03 commit 6f8ad05
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/tests/security/events/OrderEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ export class OrderEvents extends EntityEvents<Order> {
// user can only modify placed orders
return query.where({ userID }, (p) => (x) => x.customerID === p.userID || x.orderItems.some((item) => item.product.ownerID === p.userID));
}

onForeignKeyFilter(filter: ForeignKeyFilter<Order>): IEntityQuery<any> {
if (filter.is((x) => x.customer)) {
return filter.read();
}
}
}

export class OrderItemEvents extends EntityEvents<OrderItem> {
Expand All @@ -52,7 +58,7 @@ export class OrderItemEvents extends EntityEvents<OrderItem> {
}
const { userID } = this.user;
// user can only modify placed orders
return query.where({ userID }, (p) => (x) => x.order.customerID === p.userID);
return query.where({ userID }, (p) => (x) => x.order.customerID === p.userID || x.product.ownerID === p.userID);
}

onForeignKeyFilter(filter: ForeignKeyFilter<OrderItem>): IEntityQuery<any> {
Expand Down
12 changes: 11 additions & 1 deletion src/tests/security/events/UserEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ export class UserEvents extends EntityEvents<User> {
return null;
}
const { userID } = this.user;
return query.where({ userID }, (p) => (x) => x.userID === p.userID);
return query.where({ userID }, (p) => (x) => x.userID === p.userID
|| x.orders.some(
(op) => op.orderItems.some((oi) => oi.product.ownerID === p.userID)));
}

modify(query: IEntityQuery<User>): IEntityQuery<User> {
if (this.user.admin) {
return null;
}
const { userID } = this.user;
return query.where({ userID}, (p) => (x) => x.userID === p.userID);
}
}
2 changes: 1 addition & 1 deletion src/tests/security/tests/place-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async function getNewOrders(this: TestConfig) {
const scope = ServiceProvider.global.createScope();
try {
const user = new UserInfo();
user.userID = 1;
user.userID = 2;
scope.add(Logger, Logger.instance);
scope.add(BaseDriver, this.driver);
scope.add(UserInfo, user);
Expand Down

0 comments on commit 6f8ad05

Please sign in to comment.