Change onCreate logic
MODIFY
src/server/rest/v1/service/AuthorizationService.ts
src/server/rest/v1/service/AuthorizationService.ts
@@ -53,32 +53,15 @@ export default class AuthorizationService {
}
public static async checkAndGetSiteAuthorizationFilters(tenant: Tenant, userToken: UserToken,
- filteredRequest: HttpSiteRequest): Promise<AuthorizationFilter> {
+ filteredRequest: HttpSiteRequest, authAction: Action): Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
dataSources: new Map(),
projectFields: [],
authorized: false,
};
- // Check static auth
- const authorizationContext: AuthorizationContext = {};
- const authResult = await Authorizations.canReadSite(userToken, authorizationContext);
- authorizationFilters.authorized = authResult.authorized;
- // Check
- if (!authorizationFilters.authorized) {
- throw new AppAuthError({
- errorCode: HTTPAuthError.FORBIDDEN,
- user: userToken,
- action: Action.READ, entity: Entity.SITE,
- module: MODULE_NAME, method: 'checkAndGetSiteAuthorizationFilters',
- });
- }
- // Process dynamic filters
- await AuthorizationService.processDynamicFilters(tenant, userToken, Action.READ, Entity.SITE,
- authorizationFilters, authorizationContext, { SiteID: filteredRequest.ID });
- // Filter projected fields
- authorizationFilters.projectFields = AuthorizationService.filterProjectFields(
- authResult.fields, filteredRequest.ProjectFields);
+
+ await this.canPerformAuthorizationAction(tenant, userToken, Entity.SITE, authAction, authorizationFilters, filteredRequest);
return authorizationFilters;
}

MODIFY
src/server/rest/v1/service/SiteService.ts
src/server/rest/v1/service/SiteService.ts
@@ -350,6 +350,24 @@ export default class SiteService {
// Check if component is active
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.ORGANIZATION,
Action.CREATE, Entity.SITE, MODULE_NAME, 'handleCreateSite');
+ // Filter request
+ const filteredRequest = SiteSecurity.filterSiteCreateRequest(req.body);
+ // Check data is valid
+ UtilsService.checkIfSiteValid(filteredRequest, req);
+ // Get dynamic auth
+ const authorizationFilter = await AuthorizationService.checkAndGetSiteAuthorizationFilters(
+ req.tenant, req.user, { }, Action.CREATE);
+ if (!authorizationFilter.authorized) {
+ throw new AppAuthError({
+ errorCode: HTTPAuthError.FORBIDDEN,
+ user: req.user,
+ action: Action.READ, entity: Entity.SITE,
+ module: MODULE_NAME, method: 'handleCreateSite'
+ });
+ }
+ // Check Company
+ await UtilsService.checkAndGetCompanyAuthorization(
+ req.tenant, req.user, filteredRequest.companyID, Action.READ, action, {});
// Check static auth
if (!await Authorizations.canCreateSite(req.user)) {
throw new AppAuthError({
@@ -359,13 +377,6 @@ export default class SiteService {
module: MODULE_NAME, method: 'handleCreateSite'
});
}
- // Filter request
- const filteredRequest = SiteSecurity.filterSiteCreateRequest(req.body);
- // Check data is valid
- UtilsService.checkIfSiteValid(filteredRequest, req);
- // Check and Get Company
- await UtilsService.checkAndGetCompanyAuthorization(
- req.tenant, req.user, filteredRequest.companyID, Action.READ, action, {});
// Create site
const site: Site = {
...filteredRequest,

MODIFY
src/server/rest/v1/service/UtilsService.ts
src/server/rest/v1/service/UtilsService.ts
@@ -210,12 +210,12 @@ export default class UtilsService {
UtilsService.assertIdIsProvided(action, siteID, MODULE_NAME, 'checkAndGetSiteAuthorization', userToken);
// Get dynamic auth
const authorizationFilter = await AuthorizationService.checkAndGetSiteAuthorizationFilters(
- tenant, userToken, { ID: siteID });
+ tenant, userToken, { ID: siteID }, authAction);
if (!authorizationFilter.authorized) {
throw new AppAuthError({
errorCode: HTTPAuthError.FORBIDDEN,
user: userToken,
- action: Action.READ, entity: Entity.SITE,
+ action: authAction, entity: Entity.SITE,
module: MODULE_NAME, method: 'checkAndGetSiteAuthorization',
value: siteID
});

Change onCreate logic