site added getUsers/update JSON schemas + removed security class
ADD
src/assets/server/rest/v1/schemas/site/site-get-users.json
+{
+ "$id": "site-get-users",
+ "title": "Get site users Request",
+ "type": "object",
+ "properties": {
+ "SiteID": {
+ "$ref": "site#/definitions/id"
+ },
+ "Search": {
+ "$ref": "common#/definitions/search"
+ },
+ "Limit": {
+ "$ref": "common#/definitions/limit"
+ },
+ "Skip": {
+ "$ref": "common#/definitions/skip"
+ },
+ "SortFields": {
+ "$ref": "common#/definitions/sortFields"
+ },
+ "ProjectFields": {
+ "$ref": "common#/definitions/projectFields"
+ },
+ "OnlyRecordCount": {
+ "$ref": "common#/definitions/onlyRecordCount"
+ }
+ },
+ "required": [
+ "SiteID"
+ ]
+}

MODIFY
src/assets/server/rest/v1/schemas/site/site-get.json
src/assets/server/rest/v1/schemas/site/site-get.json
"WithCompany": {
"type": "boolean",
"sanitize": "mongo"
+ },
+ "ProjectFields": {
+ "$ref": "common#/definitions/projectFields"
}
},
"required": [

ADD
src/assets/server/rest/v1/schemas/site/site-update.json
+{
+ "$id": "site-update",
+ "title": "Update site Request",
+ "type": "object",
+ "properties": {
+ "id": {
+ "$ref": "site#/definitions/id"
+ },
+ "name": {
+ "$ref": "site#/definitions/name"
+ },
+ "address": {
+ "$ref": "site#/definitions/address"
+ },
+ "public": {
+ "$ref": "site#/definitions/public"
+ },
+ "tariffID": {
+ "$ref": "site#/definitions/tariffID"
+ },
+ "autoUserSiteAssignment": {
+ "$ref": "site#/definitions/autoUserSiteAssignment"
+ },
+ "companyID": {
+ "$ref": "site#/definitions/companyID"
+ },
+ "image": {
+ "$ref": "site#/definitions/image"
+ }
+ },
+ "required": [
+ "id",
+ "name",
+ "companyID"
+ ]
+}

MODIFY
src/server/rest/v1/service/SiteService.ts
src/server/rest/v1/service/SiteService.ts
@@ -11,7 +11,6 @@ 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 SiteValidator from '../validator/SiteValidator';
import { TenantComponents } from '../../../../types/Tenant';
@@ -170,7 +169,7 @@ export default class SiteService {
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.ORGANIZATION,
Action.UPDATE, Entity.SITE, MODULE_NAME, 'handleGetUsers');
// Filter
- const filteredRequest = SiteSecurity.filterSiteUsersRequest(req.query);
+ const filteredRequest = SiteValidator.getInstance().validateSiteGetUsersReq(req.query);
// Check Site
try {
await UtilsService.checkAndGetSiteAuthorization(
@@ -384,15 +383,15 @@ export default class SiteService {
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.ORGANIZATION,
Action.UPDATE, Entity.SITE, MODULE_NAME, 'handleUpdateSite');
// Filter request
- const filteredRequest = SiteSecurity.filterSiteUpdateRequest(req.body);
+ const filteredRequest = SiteValidator.getInstance().validateSiteUpdateReq(req.body);
// Check data is valid
UtilsService.checkIfSiteValid(filteredRequest, req);
// Check and Get Site
const site = await UtilsService.checkAndGetSiteAuthorization(
- req.tenant, req.user, filteredRequest.id, Action.UPDATE, action, filteredRequest as Site);
+ req.tenant, req.user, filteredRequest.id, Action.UPDATE, action, filteredRequest);
// Check and Get Company
await UtilsService.checkAndGetCompanyAuthorization(
- req.tenant, req.user, filteredRequest.companyID, Action.READ, action, filteredRequest as Site);
+ req.tenant, req.user, filteredRequest.companyID, Action.READ, action, filteredRequest);
// Update
site.name = filteredRequest.name;
site.companyID = filteredRequest.companyID;

DELETE
src/server/rest/v1/service/security/SiteSecurity.ts
-import { HttpSiteAssignUsersRequest, HttpSiteImageRequest, HttpSiteOwnerRequest, HttpSiteRequest, HttpSiteUserAdminRequest, HttpSiteUsersRequest, HttpSitesRequest } from '../../../../../types/requests/HttpSiteRequest';
-
-import Site from '../../../../../types/Site';
-import Utils from '../../../../../utils/Utils';
-import UtilsSecurity from './UtilsSecurity';
-import sanitize from 'mongo-sanitize';
-
-export default class SiteSecurity {
- public static filterUpdateSiteUserAdminRequest(request: any): HttpSiteUserAdminRequest {
- const filteredRequest: HttpSiteUserAdminRequest = {
- siteID: sanitize(request.siteID),
- userID: sanitize(request.userID)
- } as HttpSiteUserAdminRequest;
- if ('siteAdmin' in request) {
- filteredRequest.siteAdmin = UtilsSecurity.filterBoolean(request.siteAdmin);
- }
- return filteredRequest;
- }
-
- public static filterUpdateSiteOwnerRequest(request: any): HttpSiteOwnerRequest {
- const filteredRequest: HttpSiteOwnerRequest = {
- siteID: sanitize(request.siteID),
- userID: sanitize(request.userID)
- } as HttpSiteOwnerRequest;
- if ('siteOwner' in request) {
- filteredRequest.siteOwner = UtilsSecurity.filterBoolean(request.siteOwner);
- }
- return filteredRequest;
- }
-
- public static filterAssignSiteUsers(request: any): HttpSiteAssignUsersRequest {
- const filteredRequest: HttpSiteAssignUsersRequest = {
- siteID: sanitize(request.siteID)
- } as HttpSiteAssignUsersRequest;
- filteredRequest.userIDs = request.userIDs.map(sanitize);
- UtilsSecurity.filterProject(request, filteredRequest);
- return filteredRequest;
- }
-
- public static filterSiteRequest(request: any): HttpSiteRequest {
- const filteredRequest: HttpSiteRequest = {
- ID: sanitize(request.ID),
- WithCompany: sanitize(request.WithCompany),
- };
- UtilsSecurity.filterProject(request, filteredRequest);
- return filteredRequest;
- }
-
- public static filterSiteRequestByID(request: any): string {
- return sanitize(request.ID);
- }
-
- public static filterSiteImageRequest(request: any): HttpSiteImageRequest {
- return {
- ID: sanitize(request.ID),
- TenantID: sanitize(request.TenantID),
- };
- }
-
- public static filterSiteUsersRequest(request: any): HttpSiteUsersRequest {
- const filteredRequest: HttpSiteUsersRequest = {} as HttpSiteUsersRequest;
- filteredRequest.SiteID = sanitize(request.SiteID);
- filteredRequest.Search = sanitize(request.Search);
- UtilsSecurity.filterSkipAndLimit(request, filteredRequest);
- UtilsSecurity.filterSort(request, filteredRequest);
- UtilsSecurity.filterProject(request, filteredRequest);
- return filteredRequest;
- }
-
- public static filterSitesRequest(request: any): HttpSitesRequest {
- const filteredRequest: HttpSitesRequest = {} as HttpSitesRequest;
- if (Utils.objectHasProperty(request, 'Issuer')) {
- filteredRequest.Issuer = UtilsSecurity.filterBoolean(request.Issuer);
- }
- filteredRequest.Search = sanitize(request.Search);
- filteredRequest.UserID = sanitize(request.UserID);
- filteredRequest.CompanyID = sanitize(request.CompanyID);
- filteredRequest.SiteID = sanitize(request.SiteID);
- filteredRequest.ExcludeSitesOfUserID = sanitize(request.ExcludeSitesOfUserID);
- filteredRequest.WithCompany = UtilsSecurity.filterBoolean(request.WithCompany);
- filteredRequest.WithAvailableChargers = UtilsSecurity.filterBoolean(request.WithAvailableChargers);
- if (Utils.containsGPSCoordinates([request.LocLongitude, request.LocLatitude])) {
- filteredRequest.LocCoordinates = [
- Utils.convertToFloat(sanitize(request.LocLongitude)),
- Utils.convertToFloat(sanitize(request.LocLatitude))
- ];
- if (Utils.objectHasProperty(request, 'LocMaxDistanceMeters')) {
- request.LocMaxDistanceMeters = Utils.convertToInt(sanitize(request.LocMaxDistanceMeters));
- if (request.LocMaxDistanceMeters > 0) {
- filteredRequest.LocMaxDistanceMeters = request.LocMaxDistanceMeters;
- }
- }
- }
- UtilsSecurity.filterSkipAndLimit(request, filteredRequest);
- UtilsSecurity.filterSort(request, filteredRequest);
- UtilsSecurity.filterProject(request, filteredRequest);
- return filteredRequest;
- }
-
- public static filterSiteUpdateRequest(request: any): Partial<Site> {
- const filteredRequest = SiteSecurity._filterSiteRequest(request);
- filteredRequest.id = sanitize(request.id);
- return filteredRequest;
- }
-
- public static filterSiteCreateRequest(request: any): Partial<Site> {
- return SiteSecurity._filterSiteRequest(request);
- }
-
- public static _filterSiteRequest(request: any): Partial<Site> {
- const filteredRequest = {
- name: sanitize(request.name),
- address: UtilsSecurity.filterAddressRequest(request.address),
- public: UtilsSecurity.filterBoolean(request.public),
- tariffID: sanitize(request.tariffID),
- autoUserSiteAssignment: UtilsSecurity.filterBoolean(request.autoUserSiteAssignment),
- companyID: sanitize(request.companyID),
- } as Partial<Site>;
- if (Utils.objectHasProperty(request, 'image')) {
- filteredRequest.image = sanitize(request.image);
- }
- return filteredRequest;
- }
-}
-

MODIFY
src/server/rest/v1/validator/SiteValidator.ts
src/server/rest/v1/validator/SiteValidator.ts
-import { HttpSiteAssignUsersRequest, HttpSiteImageRequest, HttpSiteOwnerRequest, HttpSiteRequest, HttpSiteUserAdminRequest, HttpSitesRequest } from '../../../../types/requests/HttpSiteRequest';
+import { HttpSiteAssignUsersRequest, HttpSiteImageRequest, HttpSiteOwnerRequest, HttpSiteRequest, HttpSiteUserAdminRequest, HttpSiteUsersRequest, HttpSitesRequest } from '../../../../types/requests/HttpSiteRequest';
-import HttpByIDRequest from '../../../../types/requests/HttpByIDRequest';
import Schema from '../../../../types/validator/Schema';
import SchemaValidator from '../../../../validator/SchemaValidator';
import Site from '../../../../types/Site';
@@ -16,6 +15,8 @@ export default class SiteValidator extends SchemaValidator {
private siteAssignUsers: Schema;
private siteGet: Schema;
private siteGetImage: Schema;
+ private siteGetUsers: Schema;
+ private siteUpdate: Schema;
private constructor() {
super('SiteValidator');
@@ -26,6 +27,8 @@ export default class SiteValidator extends SchemaValidator {
this.siteAssignUsers = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/site/site-users-assign.json`, 'utf8'));
this.siteGet = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/site/site-get.json`, 'utf8'));
this.siteGetImage = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/site/site-get-image.json`, 'utf8'));
+ this.siteGetUsers = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/site/site-get-users.json`, 'utf8'));
+ this.siteUpdate = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/site/site-update.json`, 'utf8'));
}
public static getInstance(): SiteValidator {
@@ -62,4 +65,12 @@ export default class SiteValidator extends SchemaValidator {
public validateSiteGetImageReq(data: Record<string, unknown>): HttpSiteImageRequest {
return this.validate(this.siteGetImage, data);
}
+
+ public validateSiteGetUsersReq(data: Record<string, unknown>): HttpSiteUsersRequest {
+ return this.validate(this.siteGetUsers, data);
+ }
+
+ public validateSiteUpdateReq(data: Record<string, unknown>): Site {
+ return this.validate(this.siteUpdate, data);
+ }
}

site added getUsers/update JSON schemas + removed security class