Handle OR and AND in dynamic filters
MODIFY
src/authorization/AuthorizationsDefinition.ts
src/authorization/AuthorizationsDefinition.ts
@@ -539,7 +539,7 @@ const AUTHORIZATION_DEFINITION: AuthorizationDefinition = {
demo: {
grants: [
{
- resource: Entity.USER, action: [Action.READ], attributes: [
+ resource: Entity.USER, action: Action.READ, attributes: [
'id', 'name', 'firstName', 'email', 'role', 'status', 'issuer', 'locale', 'plateID',
'notificationsActive', 'notifications', 'phone', 'mobile', 'iNumber', 'costCenter', 'address'
],

MODIFY
src/server/rest/v1/service/AuthorizationService.ts
src/server/rest/v1/service/AuthorizationService.ts
@@ -788,11 +788,18 @@ export default class AuthorizationService {
private static async processDynamicFilters(tenant: Tenant, userToken: UserToken, action: Action, entity: Entity,
authorizationFilters: AuthorizationFilter, authorizationContext: AuthorizationContext, extraFilters?: Record<string, any>): Promise<void> {
if (!Utils.isEmptyArray(authorizationContext.filters)) {
+ // First array is an AND between filters
+ for (let filtersToProcess of authorizationContext.filters) {
+ authorizationFilters.authorized = false;
+ // Array?
+ if (!Array.isArray(filtersToProcess)) {
+ filtersToProcess = [filtersToProcess];
+ }
let authorized = false;
- // Process filters
- for (const filter of authorizationContext.filters) {
+ // Second array is an OR between filters
+ for (const filterToProcess of filtersToProcess) {
// Get the filter
- const dynamicFilter = await DynamicAuthorizationFactory.getDynamicFilter(tenant, userToken, filter, authorizationFilters.dataSources);
+ const dynamicFilter = await DynamicAuthorizationFactory.getDynamicFilter(tenant, userToken, filterToProcess, authorizationFilters.dataSources);
if (!dynamicFilter) {
// Filter not found -> Not authorized (all auth filter MUST work)
throw new AppAuthError({
@@ -804,10 +811,15 @@ export default class AuthorizationService {
}
// Process the filter
dynamicFilter.processFilter(authorizationFilters, extraFilters);
- // At least one filter must authorize
authorized = authorized || authorizationFilters.authorized;
}
+ // Assign
authorizationFilters.authorized = authorized;
+ // Failed?
+ if (!authorizationFilters.authorized) {
+ return;
+ }
+ }
}
}

MODIFY
src/types/Authorization.ts
src/types/Authorization.ts
@@ -174,7 +174,7 @@ export interface AuthorizationContext {
companies?: string[];
asset?: string;
assets?: string[];
- filters?: DynamicAuthorizationFilterName[];
+ filters?: DynamicAuthorizationFilterName[] | [DynamicAuthorizationFilterName[]];
}
export interface AuthorizationActions {

Handle OR and AND in dynamic filters