Skip to content
This repository was archived by the owner on Apr 17, 2023. It is now read-only.

Commit 6e513c0

Browse files
author
Enda Phelan
committed
fix(knex): add ability to filter by null state
1 parent bba3a5d commit 6e513c0

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
lines changed

docs/authentication/keycloak.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,23 @@ With this configuration the following rules are in place.
5757

5858
## Relationships Autorization
5959

60-
Developers can also add authorization rules on sepecific relationships for data fetching purposes.
60+
Developers can also add authorization rules on specific one-to-many relationships for data fetching purposes.
6161
Relationship rules will be added on top of the existing rules that are defined for the individual objects.
6262

63+
To apply a relationship field rule to the one-to-many field `Task.users`, you must configure it on the `User.task` configuration object, the inverse field which `Task.users` maps to.
64+
6365
```ts
6466
const authConfig = {
65-
Task: {
67+
User: {
6668
relations: {
67-
taskUsers: { roles: ['admin'] }
68-
allTasksComments: { roles: ['commenter'] }
69+
task: { roles: ['admin'] }
6970
},
70-
71-
},
71+
}
7272
```
7373
74-
With this configuration the following rules are in place.
74+
With this configuration the following authorization rule is set:
7575
76-
- Tasks `taskUsers` field has `admin` role applied. Fetching User object will require `admin` role for any user field fetched
77-
- Tasks `allTasksComments` field has `commenter` role applied. Fetching `Comment` object will require `commenter` role for any user field fetched
76+
- `User.relations.task.roles` applies the `admin` role on `Task.users`. Users must have the `admin` role in order to query `Task.users`.
7877
7978
:::info
8079
Due to limitations of the Graphback `relations` authorization works only on `OneToMany` relationships.

docs/releases.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Please follow individual releases for more information.
2323
* Prevent creation of empty `Subscription`, `Query`, `Mutation` resolver objects ([#2073](https://github.com/aerogear/graphback/pull/2073), fixed by [97e8267](https://github.com/aerogear/graphback/commit/97e82677257b54783916c3062ed6f0e74f25c038))
2424
* Fix `TypeError: Cannot read property 'page' of undefined` error in CRUDService ([#2095](https://github.com/aerogear/graphback/pull/2095) fixed by [5143fb6](https://github.com/aerogear/graphback/commit/5143fb6c6a76d20f44b3e79ab25c6922408dd54a))
2525
* It was not possible to map a `WHERE X IS/IS NOT NULL` query in the Knex query mapper ([#2095](https://github.com/aerogear/graphback/pull/2095) fixed by [d10e918](https://github.com/aerogear/graphback/commit/d10e918714a85c8c6f6ebb4260e9aff0b6b99ffa))
26+
* Prevent creation of empty `Subscription`, `Query`, `Mutation` resolver objects ([#2073](https://github.com/aerogear/graphback/pull/2073), fixed by [97e826](https://github.com/aerogear/graphback/commit/97e82677257b54783916c3062ed6f0e74f25c038))
27+
* Configure relationship auth rule with field instead of database key ([#2101](https://github.com/aerogear/graphback/pull/2073), fixed by [525bc9a](https://github.com/aerogear/graphback/commit/525bc9a641fa7cb1818a0727a675564e6fa12dda))
2628

2729
### Breaking Changes
2830

packages/graphback-keycloak-authz/src/KeycloakCrudService.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { isAuthorizedByRole, KeycloakContext, KeycloakContextBase } from "keycloak-connect-graphql";
2-
import { GraphbackCRUDService, ResultList, GraphbackProxyService, GraphbackContext, QueryFilter, FindByArgs, getSelectedFieldsFromResolverInfo, ModelDefinition } from '@graphback/core';
2+
import { GraphbackCRUDService, ResultList, GraphbackProxyService, GraphbackContext, QueryFilter, FindByArgs, getSelectedFieldsFromResolverInfo, ModelDefinition, FieldRelationshipMetadata } from '@graphback/core';
33
import { GraphQLResolveInfo } from 'graphql';
44
import { CrudServiceAuthConfig } from './KeycloakConfig';
55
import { getEmptyServiceConfig, UnauthorizedError, checkAuthRulesForInput, checkAuthRulesForSelections } from "./utils";
@@ -63,7 +63,7 @@ export class KeycloakCrudService<Type = any> extends GraphbackProxyService<Type>
6363
throw new UnauthorizedError()
6464
}
6565
}
66-
66+
6767
checkAuthRulesForInput(context, this.authConfig, Object.keys(data));
6868

6969
return super.update(data, context, info);
@@ -158,8 +158,10 @@ export class KeycloakCrudService<Type = any> extends GraphbackProxyService<Type>
158158
}
159159

160160
public batchLoadData(relationField: string, id: string | number, filter: QueryFilter, context: GraphbackContext, info?: GraphQLResolveInfo) {
161-
if (this.authConfig?.relations && this.authConfig?.relations[relationField]?.roles.length > 0) {
162-
const { roles } = this.authConfig?.relations[relationField];
161+
const relationshipMetadata = this.model.relationships.find((r: FieldRelationshipMetadata) => r.relationForeignKey === relationField);
162+
const modelRelationshipField = relationshipMetadata ? relationshipMetadata.ownerField.name : relationField;
163+
if (this.authConfig?.relations && this.authConfig?.relations[modelRelationshipField]?.roles.length > 0) {
164+
const { roles } = this.authConfig?.relations[modelRelationshipField];
163165
if (!isAuthorizedByRole(roles, context)) {
164166
throw new UnauthorizedError()
165167
}

packages/graphback-keycloak-authz/tests/KeycloakCrudService.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ test('Batching', async () => {
313313
description: String
314314
315315
""" @oneToMany(field: 'task') """
316-
comment: Comment
316+
comments: [Comment]
317317
}
318318
319319
"""@model"""

0 commit comments

Comments
 (0)