added sitesOwnerUsers filter
MODIFY
src/authorization/AuthorizationsDefinition.ts
src/authorization/AuthorizationsDefinition.ts
@@ -3122,7 +3122,7 @@ export const AUTHORIZATION_DEFINITION: AuthorizationDefinition = {
Fn: 'custom:dynamicAuthorizations',
args: {
asserts: [],
- filters: ['AssignedSites']
+ filters: ['SitesOwnerUsers']
}
},
attributes: [
@@ -3147,7 +3147,7 @@ export const AUTHORIZATION_DEFINITION: AuthorizationDefinition = {
Fn: 'custom:dynamicAuthorizations',
args: {
asserts: [],
- filters: ['AssignedSites']
+ filters: ['SitesOwnerUsers']
}
},
attributes: [
@@ -3164,7 +3164,7 @@ export const AUTHORIZATION_DEFINITION: AuthorizationDefinition = {
Fn: 'custom:dynamicAuthorizations',
args: {
asserts: [],
- filters: ['AssignedSites']
+ filters: ['SitesOwnerUsers']
}
},
attributes: [
@@ -3202,7 +3202,7 @@ export const AUTHORIZATION_DEFINITION: AuthorizationDefinition = {
Fn: 'custom:dynamicAuthorizations',
args: {
asserts: [],
- filters: ['AssignedSites']
+ filters: ['SitesOwnerUsers']
}
},
attributes: [
@@ -3222,7 +3222,7 @@ export const AUTHORIZATION_DEFINITION: AuthorizationDefinition = {
Fn: 'custom:dynamicAuthorizations',
args: {
asserts: [],
- filters: ['AssignedSites']
+ filters: ['SitesOwnerUsers']
}
},
attributes: [
@@ -3235,7 +3235,7 @@ export const AUTHORIZATION_DEFINITION: AuthorizationDefinition = {
Fn: 'custom:dynamicAuthorizations',
args: {
asserts: [],
- filters: ['AssignedSites']
+ filters: ['SitesOwnerUsers']
}
},
attributes: [
@@ -3265,7 +3265,7 @@ export const AUTHORIZATION_DEFINITION: AuthorizationDefinition = {
Fn: 'custom:dynamicAuthorizations',
args: {
asserts: [],
- filters: ['SitesOwner']
+ filters: ['SitesOwnerUsers']
}
}
},
@@ -3275,7 +3275,7 @@ export const AUTHORIZATION_DEFINITION: AuthorizationDefinition = {
Fn: 'custom:dynamicAuthorizations',
args: {
asserts: [],
- filters: ['SitesOwner']
+ filters: ['SitesOwnerUsers']
}
},
},

MODIFY
src/authorization/DynamicAuthorizationFactory.ts
src/authorization/DynamicAuthorizationFactory.ts
@@ -24,6 +24,8 @@ import SitesAdminUsersDynamicAuthorizationDataSource from './dynamic-data-source
import SitesAdminUsersDynamicAuthorizationFilter from './dynamic-filters/SitesAdminUsersDynamicAuthorizationFilter';
import SitesOwnerDynamicAuthorizationDataSource from './dynamic-data-source/SitesOwnerDynamicAuthorizationDataSource';
import SitesOwnerDynamicAuthorizationFilter from './dynamic-filters/SitesOwnerDynamicAuthorizationFilter';
+import SitesOwnerUsersDynamicAuthorizationDataSource from './dynamic-data-source/SitesOwnerUsersDynamicAuthorizationDataSource';
+import SitesOwnerUsersDynamicAuthorizationFilter from './dynamic-filters/SitesOwnerUsersDynamicAuthorizationFilter';
import Tenant from '../types/Tenant';
import UserMandatoryDynamicAuthorizationAssert from './dynamic-assert/UserMandatoryDynamicAuthorizationAssert';
import UserToken from '../types/UserToken';
@@ -69,6 +71,9 @@ export default class DynamicAuthorizationFactory {
case DynamicAuthorizationFilterName.SITES_ADMIN_OR_OWNER:
dynamicFilter = new SitesAdminOrOwnerDynamicAuthorizationFilter(tenant, userToken, negateFilter);
break;
+ case DynamicAuthorizationFilterName.SITES_OWNER_USERS:
+ dynamicFilter = new SitesOwnerUsersDynamicAuthorizationFilter(tenant, userToken, negateFilter);
+ break;
}
// Init Data Source
if (dynamicFilter) {
@@ -134,6 +139,9 @@ export default class DynamicAuthorizationFactory {
case DynamicAuthorizationDataSourceName.SITES_ADMIN_OR_OWNER:
dataSource = new SitesAdminOrOwnerDynamicAuthorizationDataSource(tenant, user);
break;
+ case DynamicAuthorizationDataSourceName.SITES_OWNER_USERS:
+ dataSource = new SitesOwnerUsersDynamicAuthorizationDataSource(tenant, user);
+ break;
}
// Load data
if (dataSource) {

ADD
src/authorization/dynamic-data-source/SitesOwnerUsersDynamicAuthorizationDataSource.ts
+import { DynamicAuthorizationDataSourceName, SitesOwnerUsersDynamicAuthorizationDataSourceData } from '../../types/Authorization';
+
+import Constants from '../../utils/Constants';
+import DynamicAuthorizationDataSource from '../DynamicAuthorizationDataSource';
+import TagStorage from '../../storage/mongodb/TagStorage';
+import UserStorage from '../../storage/mongodb/UserStorage';
+
+export default class SitesOwnerUsersDynamicAuthorizationDataSource
+ extends DynamicAuthorizationDataSource<SitesOwnerUsersDynamicAuthorizationDataSourceData> {
+
+ public getName(): DynamicAuthorizationDataSourceName {
+ return DynamicAuthorizationDataSourceName.SITES_OWNER_USERS;
+ }
+
+ public async loadData(): Promise<void> {
+ const sitesOwnerData: SitesOwnerUsersDynamicAuthorizationDataSourceData = {};
+ // Get Site and user ID(s)
+ const data = await this.getSitesOwnerUserSiteIDs();
+ // Set
+ sitesOwnerData.siteIDs = data.siteIDs;
+ sitesOwnerData.userID = data.userID;
+ sitesOwnerData.tagIDs = data.tagIDs;
+ this.setData(sitesOwnerData);
+ }
+
+ private async getSitesOwnerUserSiteIDs(): Promise<{siteIDs: string[], tagIDs: string[], userID: string}> {
+ // Get the Site IDs of the Sites for which the user is Site Owner
+ const sites = await UserStorage.getUserSites(this.tenant,
+ {
+ userIDs: [this.userToken.id],
+ siteOwner: true
+ }, Constants.DB_PARAMS_MAX_LIMIT,
+ ['siteID']
+ );
+ // Get the tag and user Ids
+ const tags = await TagStorage.getTags(this.tenant,
+ {
+ userIDs: [this.userToken.id]
+ },
+ Constants.DB_PARAMS_DEFAULT_RECORD,
+ ['id']);
+ return { siteIDs: sites.result.map((userSite) => userSite.siteID),tagIDs: tags.result.map((tag) => tag.id), userID: this.userToken.id };
+ }
+}

MODIFY
src/authorization/dynamic-filters/SitesAdminUsersDynamicAuthorizationFilter.ts
src/authorization/dynamic-filters/SitesAdminUsersDynamicAuthorizationFilter.ts
@@ -53,7 +53,6 @@ export default class SitesAdminUsersDynamicAuthorizationFilter extends DynamicAu
}
}
}
-
if (!Utils.isEmptyArray(tagIDs)) {
// Force the filter
authorizationFilters.filters.ownUserTags = tagIDs;

ADD
src/authorization/dynamic-filters/SitesOwnerUsersDynamicAuthorizationFilter.ts
+import { AuthorizationFilter, DynamicAuthorizationDataSourceName, Entity } from '../../types/Authorization';
+
+import DynamicAuthorizationFilter from '../DynamicAuthorizationFilter';
+import { EntityData } from '../../types/GlobalType';
+import SitesOwnerUsersDynamicAuthorizationDataSource from '../dynamic-data-source/SitesOwnerUsersDynamicAuthorizationDataSource';
+import { TenantComponents } from '../../types/Tenant';
+import Utils from '../../utils/Utils';
+
+export default class SitesOwnerUsersDynamicAuthorizationFilter extends DynamicAuthorizationFilter {
+ public processFilter(authorizationFilters: AuthorizationFilter, extraFilters: Record<string, any>, entityData?: EntityData): void {
+ // Retrieve site ids where user is site owner and logged user id
+ const sitesOwnerUsersDataSource = this.getDataSource(
+ DynamicAuthorizationDataSourceName.SITES_OWNER_USERS) as SitesOwnerUsersDynamicAuthorizationDataSource;
+ const { siteIDs, tagIDs, userID } = sitesOwnerUsersDataSource.getData();
+ // Flag
+ let authFilterUsed = false;
+ // Perform site ids check only if organization component is active
+ if (Utils.isTenantComponentActive(this.tenant, TenantComponents.ORGANIZATION)) {
+ // Init user site IDs
+ authorizationFilters.filters.siteOwnerIDs = [];
+ if (!Utils.isEmptyArray(siteIDs)) {
+ authorizationFilters.filters.siteOwnerIDs = siteIDs;
+ // Check if filter is provided
+ if (Utils.objectHasProperty(extraFilters, 'SiteID') && !Utils.isNullOrUndefined(extraFilters['SiteID'])) {
+ // Update flag
+ authFilterUsed = true;
+ const filteredSiteIDs: string[] = extraFilters['SiteID'].split('|');
+ // Override
+ authorizationFilters.filters.siteOwnerIDs = filteredSiteIDs.filter(
+ (siteID) => authorizationFilters.filters.siteOwnerIDs.includes(siteID));
+ // Check auth
+ if (!Utils.isEmptyArray(authorizationFilters.filters.siteOwnerIDs)) {
+ authorizationFilters.authorized = true;
+ }
+ }
+ }
+ }
+ // Check user filter
+ authorizationFilters.filters.ownerID = [];
+ if (userID) {
+ authorizationFilters.filters.ownerID = [userID];
+ // Check if filter is provided
+ if (Utils.objectHasProperty(extraFilters, 'UserID') && !Utils.isNullOrUndefined(extraFilters['UserID'])) {
+ // Update flag
+ authFilterUsed = true;
+ const filteredUserIDs: string[] = extraFilters['UserID'].split('|');
+ // Override
+ authorizationFilters.filters.ownerID = filteredUserIDs.filter(
+ (user) => authorizationFilters.filters.ownerID.includes(user));
+ // Check auth
+ if (!Utils.isEmptyArray(authorizationFilters.filters.ownerID)) {
+ authorizationFilters.authorized = true;
+ }
+ }
+ }
+ if (!Utils.isEmptyArray(tagIDs)) {
+ // Force the filter
+ authorizationFilters.filters.ownUserTags = tagIDs;
+ // Check if filter is provided
+ if (Utils.objectHasProperty(extraFilters, 'TagIDs') && !Utils.isNullOrUndefined(extraFilters['TagIDs'])) {
+ // Update flag
+ authFilterUsed = true;
+ const filteredUserTagIDs: string[] = extraFilters['TagIDs'].split('|');
+ // Override
+ authorizationFilters.filters.ownUserTags = filteredUserTagIDs.filter(
+ (tag) => authorizationFilters.filters.ownUserTags.includes(tag));
+ // Check auth
+ if (!Utils.isEmptyArray(authorizationFilters.filters.ownUserTags)) {
+ authorizationFilters.authorized = true;
+ }
+ }
+ }
+ // No auth filter, we authorize
+ if (!authFilterUsed) {
+ authorizationFilters.authorized = true;
+ }
+ // Remove sensible data if not authorized and filter is provided
+ if (!authorizationFilters.authorized) {
+ Utils.removeSensibeDataFromEntity(extraFilters, entityData);
+ }
+ }
+
+ public getApplicableEntities(): Entity[] {
+ return [
+ Entity.TRANSACTION,
+ ];
+ }
+
+ public getApplicableDataSources(): DynamicAuthorizationDataSourceName[] {
+ return [
+ DynamicAuthorizationDataSourceName.SITES_OWNER_USERS
+ ];
+ }
+}

MODIFY
src/types/Authorization.ts
src/types/Authorization.ts
@@ -380,6 +380,7 @@ export enum DynamicAuthorizationFilterName {
ASSIGNED_SITES_COMPANIES = 'AssignedSitesCompanies',
SITES_ADMIN = 'SitesAdmin',
SITES_ADMIN_USERS = 'SitesAdminUsers',
+ SITES_OWNER_USERS = 'SitesOwnerUsers',
SITES_OWNER = 'SitesOwner',
ASSIGNED_SITES = 'AssignedSites',
OWN_USER = 'OwnUser',
@@ -400,6 +401,7 @@ export enum DynamicAuthorizationDataSourceName {
ASSIGNED_SITES_COMPANIES = 'AssignedSitesCompanies',
SITES_ADMIN = 'SitesAdmin',
SITES_ADMIN_USERS = 'SitesAdminUsers',
+ SITES_OWNER_USERS = 'SitesOwnerUsers',
SITES_OWNER = 'SitesOwner',
ASSIGNED_SITES = 'AssignedSites',
OWN_USER = 'OwnUser',
@@ -426,6 +428,13 @@ export interface SitesAdminUsersDynamicAuthorizationDataSourceData extends Dynam
userID?: string;
tagIDs?: string[];
}
+
+export interface SitesOwnerUsersDynamicAuthorizationDataSourceData extends DynamicAuthorizationDataSourceData {
+ siteIDs?: string[];
+ userID?: string;
+ tagIDs?: string[];
+}
+
export interface SitesOwnerDynamicAuthorizationDataSourceData extends DynamicAuthorizationDataSourceData {
siteIDs?: string[];
}

added sitesOwnerUsers filter