Add canCreate on sites root.
MODIFY
src/server/rest/v1/service/AuthorizationService.ts
src/server/rest/v1/service/AuthorizationService.ts
import { Action, AuthorizationFilter, Entity } from '../../../../types/Authorization';
+import { CompanyDataResult, SiteDataResult } from '../../../../types/DataResult';
import { HttpCompaniesRequest, HttpCompanyRequest } from '../../../../types/requests/HttpCompanyRequest';
import { HttpSiteAssignUsersRequest, HttpSiteRequest, HttpSiteUsersRequest } from '../../../../types/requests/HttpSiteRequest';
import { HttpTagsRequest, HttpUserAssignSitesRequest, HttpUserRequest, HttpUserSitesRequest, HttpUsersRequest } from '../../../../types/requests/HttpUserRequest';
@@ -6,7 +7,6 @@ import User, { UserRole } from '../../../../types/User';
import AppAuthError from '../../../../exception/AppAuthError';
import Authorizations from '../../../../authorization/Authorizations';
-import { CompanyDataResult } from '../../../../types/DataResult';
import Constants from '../../../../utils/Constants';
import { HTTPAuthError } from '../../../../types/HTTPError';
import HttpByIDRequest from '../../../../types/requests/HttpByIDRequest';
@@ -85,11 +85,13 @@ export default class AuthorizationService {
return authorizationFilters;
}
- public static async addSitesAuthorizations(tenant: Tenant, userToken: UserToken, sites: Site[]): Promise<void> {
+ public static async addSitesAuthorizations(tenant: Tenant, userToken: UserToken, sites: SiteDataResult): Promise<void> {
// Get Site Admins
const { siteAdminIDs, siteOwnerIDs } = await AuthorizationService.getSiteAdminOwnerIDs(tenant, userToken);
+ // Add canCreate flag to root
+ sites.canCreate = Authorizations.canCreateSite(userToken);
// Enrich
- for (const site of sites) {
+ for (const site of sites.result) {
site.canRead = Authorizations.canReadSite(userToken);
site.canDelete = Authorizations.canDeleteSite(userToken);
// update can be performed by admin or site admin

MODIFY
src/server/rest/v1/service/SiteService.ts
src/server/rest/v1/service/SiteService.ts
@@ -11,6 +11,7 @@ import Constants from '../../../../utils/Constants';
import Logging from '../../../../utils/Logging';
import { ServerAction } from '../../../../types/Server';
import Site from '../../../../types/Site';
+import { SiteDataResult } from '../../../../types/DataResult';
import SiteSecurity from './security/SiteSecurity';
import SiteStorage from '../../../../storage/mongodb/SiteStorage';
import TenantComponents from '../../../../types/TenantComponents';
@@ -476,7 +477,7 @@ export default class SiteService {
authorizationSiteFilters.projectFields
);
// Add Auth flags
- await AuthorizationService.addSitesAuthorizations(req.tenant, req.user, sites.result);
+ await AuthorizationService.addSitesAuthorizations(req.tenant, req.user, sites as SiteDataResult);
// Return
res.json(sites);
next();

MODIFY
src/types/DataResult.ts
src/types/DataResult.ts
import Company from './Company';
+import Site from './Site';
import { Transaction } from '@google-cloud/firestore';
export interface DataResult<T> {
@@ -9,6 +10,9 @@ export interface DataResult<T> {
export interface CompanyDataResult extends DataResult<Company>{
canCreate: boolean;
}
+export interface SiteDataResult extends DataResult<Site>{
+ canCreate: boolean;
+}
export interface TransactionDataResult {
count: number;

Add canCreate on sites root.