Added site create/assignAdmin/owner validation
ADD
src/assets/schemas/site/site.json
+{
+ "$id": "site",
+ "definitions": {
+ "id": {
+ "$ref": "common#/definitions/id"
+ },
+ "name": {
+ "type": "string",
+ "sanitize": "mongo"
+ },
+ "address": {
+ "$ref": "common#/definitions/address"
+ },
+ "public": {
+ "type": "boolean",
+ "sanitize": "mongo"
+ },
+ "tariffID": {
+ "type": "string",
+ "sanitize": "mongo"
+ },
+ "autoUserSiteAssignment": {
+ "type": "boolean",
+ "sanitize": "mongo"
+ },
+ "companyID": {
+ "$ref": "company#/definitions/id"
+ },
+ "image": {
+ "type": "string",
+ "sanitize": "mongo"
+ }
+ }
+}

ADD
src/assets/server/rest/v1/schemas/site/site-admin.json
+{
+ "$id": "site-admin",
+ "title": "Set / unset site admin Request",
+ "type": "object",
+ "properties": {
+ "siteID": {
+ "$ref": "site#/definitions/id"
+ },
+ "userID": {
+ "$ref": "user#/definitions/id"
+ },
+ "siteAdmin": {
+ "type": "boolean",
+ "sanitize": "mongo"
+ }
+ },
+ "required": [
+ "siteID",
+ "userID",
+ "siteAdmin"
+ ]
+}

ADD
src/assets/server/rest/v1/schemas/site/site-create.json
+{
+ "$id": "site-create",
+ "title": "Create site Request",
+ "type": "object",
+ "properties": {
+ "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": [
+ "name",
+ "companyID"
+ ]
+}

ADD
src/assets/server/rest/v1/schemas/site/site-owner.json
+{
+ "$id": "site-owner",
+ "title": "Set / unset site owner Request",
+ "type": "object",
+ "properties": {
+ "siteID": {
+ "$ref": "site#/definitions/id"
+ },
+ "userID": {
+ "$ref": "user#/definitions/id"
+ },
+ "siteOwner": {
+ "type": "boolean",
+ "sanitize": "mongo"
+ }
+ },
+ "required": [
+ "siteID",
+ "userID",
+ "siteOwner"
+ ]
+}

ADD
src/assets/server/rest/v1/schemas/site/sites-get.json
+{
+ "$id": "sites-get",
+ "title": "Get site list Request",
+ "type": "object",
+ "properties": {
+ "Issuer": {
+ "type": "boolean",
+ "sanitize": "mongo"
+ },
+ "UserID": {
+ "$ref": "user#/definitions/id"
+ },
+ "CompanyID": {
+ "$ref": "company#/definitions/id"
+ },
+ "SiteID": {
+ "$ref": "site#/definitions/id"
+ },
+ "ExcludeSitesOfUserID": {
+ "type": "boolean",
+ "sanitize": "mongo"
+ },
+ "WithCompany": {
+ "type": "boolean",
+ "sanitize": "mongo"
+ },
+ "WithAvailableChargers": {
+ "type": "boolean",
+ "sanitize": "mongo"
+ },
+ "LocLongitude": {
+ "type": "number",
+ "format": "longitude",
+ "sanitize": "mongo"
+ },
+ "LocLatitude": {
+ "type": "number",
+ "format": "latitude",
+ "sanitize": "mongo"
+ },
+ "LocMaxDistanceMeters": {
+ "type": "number",
+ "sanitize": "mongo",
+ "exclusiveMinimum": 0
+ },
+ "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"
+ }
+ }
+}

MODIFY
src/server/rest/v1/service/SiteService.ts
src/server/rest/v1/service/SiteService.ts
@@ -13,6 +13,7 @@ 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';
import TenantStorage from '../../../../storage/mongodb/TenantStorage';
import Utils from '../../../../utils/Utils';
@@ -27,7 +28,7 @@ export default class SiteService {
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.ORGANIZATION,
Action.UPDATE, Entity.SITE, MODULE_NAME, 'handleUpdateSiteUserAdmin');
// Filter request
- const filteredRequest = SiteSecurity.filterUpdateSiteUserAdminRequest(req.body);
+ const filteredRequest = SiteValidator.getInstance().validateSiteAdminReq(req.body);
// Check mandatory fields
if (!filteredRequest.userID) {
throw new AppError({
@@ -87,7 +88,7 @@ export default class SiteService {
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.ORGANIZATION,
Action.UPDATE, Entity.SITE, MODULE_NAME, 'handleUpdateSiteOwner');
// Filter request
- const filteredRequest = SiteSecurity.filterUpdateSiteOwnerRequest(req.body);
+ const filteredRequest = SiteValidator.getInstance().validateSiteOwnerReq(req.body);
// Check mandatory fields
if (!filteredRequest.userID) {
throw new AppError({
@@ -333,12 +334,12 @@ export default class SiteService {
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.ORGANIZATION,
Action.CREATE, Entity.SITE, MODULE_NAME, 'handleCreateSite');
// Filter request
- const filteredRequest = SiteSecurity.filterSiteCreateRequest(req.body);
+ const filteredRequest = SiteValidator.getInstance().validateSiteCreateReq(req.body);
// Check data is valid
UtilsService.checkIfSiteValid(filteredRequest, req);
// Get dynamic auth
const authorizationFilter = await AuthorizationService.checkAndGetSiteAuthorizations(
- req.tenant, req.user, {}, Action.CREATE, filteredRequest as Site);
+ req.tenant, req.user, {}, Action.CREATE, filteredRequest);
if (!authorizationFilter.authorized) {
throw new AppAuthError({
errorCode: HTTPAuthError.FORBIDDEN,

ADD
src/server/rest/v1/validator/SiteValidator.ts
+import { HttpSiteOwnerRequest, HttpSiteUserAdminRequest } from '../../../../types/requests/HttpSiteRequest';
+
+import Schema from '../../../../types/validator/Schema';
+import SchemaValidator from '../../../../validator/SchemaValidator';
+import Site from '../../../../types/Site';
+import fs from 'fs';
+import global from '../../../../types/GlobalType';
+
+export default class SiteValidator extends SchemaValidator {
+ private static instance: SiteValidator|null = null;
+ private siteCreate: Schema;
+ private siteAdmin: Schema;
+ private siteOwner: Schema;
+
+ private constructor() {
+ super('SiteValidator');
+ this.siteCreate = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/site/site-create.json`, 'utf8'));
+ this.siteAdmin = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/site/site-admin.json`, 'utf8'));
+ this.siteOwner = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/site/site-owner.json`, 'utf8'));
+ }
+
+ public static getInstance(): SiteValidator {
+ if (!SiteValidator.instance) {
+ SiteValidator.instance = new SiteValidator();
+ }
+ return SiteValidator.instance;
+ }
+
+ public validateSiteCreateReq(data: Record<string, unknown>): Site {
+ return this.validate(this.siteCreate, data);
+ }
+
+ public validateSiteAdminReq(data: Record<string, unknown>): HttpSiteUserAdminRequest {
+ return this.validate(this.siteAdmin, data);
+ }
+
+ public validateSiteOwnerReq(data: Record<string, unknown>): HttpSiteOwnerRequest {
+ return this.validate(this.siteOwner, data);
+ }
+}

MODIFY
src/validator/SchemaValidator.ts
src/validator/SchemaValidator.ts
@@ -33,6 +33,7 @@ export default class SchemaValidator {
private static oicpEndpointSchema: Schema = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/schemas/oicp/oicp-endpoint.json`, 'utf8'));
private static settingSchema: Schema = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/schemas/setting/setting.json`, 'utf8'));
private static registrationTokenSchema: Schema = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/schemas/registration-token/registration-token.json`, 'utf8'));
+ private static siteSchema: Schema = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/schemas/site/site.json`, 'utf8'));
private readonly ajv: Ajv;
constructor(readonly moduleName: string,
@@ -71,7 +72,8 @@ export default class SchemaValidator {
SchemaValidator.pricingDefinitionSchema,
SchemaValidator.oicpEndpointSchema,
SchemaValidator.settingSchema,
- SchemaValidator.registrationTokenSchema
+ SchemaValidator.registrationTokenSchema,
+ SchemaValidator.siteSchema
]);
}

Added site create/assignAdmin/owner validation