Added tenant definition schema
MODIFY
src/assets/server/rest/v1/schemas/tenant/tenant-update.json
src/assets/server/rest/v1/schemas/tenant/tenant-update.json
"$ref": "common.json#/definitions/id"
},
"name": {
- "$ref": "common.json#/definitions/name"
+ "$ref": "tenant.json#/definitions/name"
},
"email": {
"$ref": "user.json#/definitions/email"

MODIFY
src/assets/server/rest/v1/schemas/tenant/tenant.json
src/assets/server/rest/v1/schemas/tenant/tenant.json
{
- "id": "tenant.json",
+ "$id": "tenant.json",
"definitions": {
"name": {
"type": "string",

ADD
src/assets/server/rest/v1/schemas/user/user-admin-create.json
+{
+ "title": "Handle admin related fields for User creation",
+ "type": "object",
+ "definitions": {
+ "status": {
+ "$ref": "user.json#/definitions/status"
+ },
+ "plateID": {
+ "$ref": "user.json#/definitions/plateID"
+ },
+ "role": {
+ "$ref": "user.json#/definitions/role"
+ }
+ }
+}

MODIFY
src/assets/server/rest/v1/schemas/user/user-create.json
src/assets/server/rest/v1/schemas/user/user-create.json
{
"title": "Create one User",
"type": "object",
- "properties": {
+ "definitions": {
"name": {
- "$ref": "tenant.json#/definitions/name"
+ "$ref": "user.json#/definitions/name"
+ },
+ "firstName": {
+ "$ref": "user.json#/definitions/firstName"
},
"email": {
"$ref": "user.json#/definitions/email"
},
- "subdomain": {
- "$ref": "common.json#/definitions/subdomain"
+ "costCenter": {
+ "$ref": "user.json#/definitions/costCenter"
+ },
+ "iNumber": {
+ "$ref": "user.json#/definitions/iNumber"
+ },
+ "image": {
+ "$ref": "user.json#/definitions/image"
+ },
+ "mobile": {
+ "$ref": "user.json#/definitions/mobile"
},
- "logo": {
- "$ref": "common.json#/definitions/logo"
+ "locale": {
+ "$ref": "user.json#/definitions/locale"
},
"address": {
- "$ref": "common.json#/definitions/address"
+ "$ref": "user.json#/definitions/address"
+ },
+ "passwords": {
+ "$ref": "user.json#/definitions/passwords"
+ },
+ "phone": {
+ "$ref": "user.json#/definitions/phone"
+ },
+ "issuer": {
+ "$ref": "user.json#/definitions/issuer"
+ },
+ "notificationsActive": {
+ "$ref": "user.json#/definitions/notificationsActive"
},
- "components": {
- "$ref": "tenant-components.json#/definitions/components"
+ "notifications": {
+ "$ref": "user.json#/definitions/notifications"
}
},
"required": [

MODIFY
src/server/rest/v1/service/TenantService.ts
src/server/rest/v1/service/TenantService.ts
@@ -313,7 +313,13 @@ export default class TenantService {
public static async handleUpdateTenant(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
// Check
- const filteredRequest = TenantValidator.getInstance().validateTenantUpdateRequestSuperAdmin(req.body);
+ let filteredRequest;
+
+ try {
+ filteredRequest = TenantValidator.getInstance().validateTenantUpdateRequestSuperAdmin(req.body);
+ } catch (e) {
+ console.log(e);
+ }
// Check auth
if (!await Authorizations.canUpdateTenant(req.user)) {
throw new AppAuthError({

MODIFY
src/server/rest/v1/validator/SchemaValidator.ts
src/server/rest/v1/validator/SchemaValidator.ts
@@ -15,6 +15,7 @@ const extraSanitizers = {
export default class SchemaValidator {
private readonly ajv: Ajv.Ajv;
private commonSchema: Schema = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/common/common.json`, 'utf8'));
+ private tenantSchema: Schema = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/tenant/tenant.json`, 'utf8'));
private tenantComponentSchema: Schema = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/tenant/tenant-components.json`, 'utf8'));
private chargingStationSchema: Schema = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/chargingstation/chargingstation.json`, 'utf8'));
private tagSchema: Schema = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/tag/tag.json`, 'utf8'));
@@ -27,7 +28,7 @@ export default class SchemaValidator {
useDefaults: boolean; coerceTypes: boolean;
} = {
allErrors: true,
- removeAdditional: 'failing',
+ removeAdditional: 'all',
useDefaults: true,
coerceTypes: true
}) {
@@ -41,6 +42,7 @@ export default class SchemaValidator {
validate: (c) => Constants.REGEX_VALIDATION_LONGITUDE.test(c.toString())
});
this.ajv.addSchema(this.commonSchema);
+ this.ajv.addSchema(this.tenantSchema);
this.ajv.addSchema(this.tenantComponentSchema);
this.ajv.addSchema(this.chargingStationSchema);
this.ajv.addSchema(this.tagSchema);
@@ -49,7 +51,6 @@ export default class SchemaValidator {
}
public validate(schema: Schema, content: any): void {
- try {
const fnValidate = this.ajv.compile(schema);
if (!fnValidate(content)) {
if (!fnValidate.errors) {
@@ -76,8 +77,5 @@ export default class SchemaValidator {
detailedMessages: { errors, content, schema }
});
}
- } catch (e) {
- console.log(e);
- }
}
}

MODIFY
src/server/rest/v1/validator/UserValidator.ts
src/server/rest/v1/validator/UserValidator.ts
-import { ImportedUser } from '../../../../types/User';
+import User, { ImportedUser } from '../../../../types/User';
+
+import Authorizations from '../../../../authorization/Authorizations';
+import { HttpUserRequest } from '../../../../types/requests/HttpUserRequest';
import Schema from '../../../../types/validator/Schema';
import SchemaValidator from './SchemaValidator';
+import UserToken from '../../../../types/UserToken';
+import Utils from '../../../../utils/Utils';
import fs from 'fs';
import global from '../../../../types/GlobalType';
export default class UserValidator extends SchemaValidator {
private static instance: UserValidator|null = null;
private importedUserCreation: Schema;
+ private userCreate: Schema;
+ private userAdminCreate: Schema;
private constructor() {
super('UserValidator');
this.importedUserCreation = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/user/imported-user-create-req.json`, 'utf8'));
+ this.userCreate = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/user/user-create.json`, 'utf8'));
+ this.userAdminCreate = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/user/user-admin-create.json`, 'utf8'));
}
public static getInstance(): UserValidator {
@@ -23,4 +32,14 @@ export default class UserValidator extends SchemaValidator {
validateImportedUserCreation(importedUser: ImportedUser): void {
this.validate(this.importedUserCreation, importedUser);
}
+
+ validateUserCreate(user: Partial<User>, loggedUser: UserToken): Partial<User> {
+ const request = Utils.cloneObject(user);
+ this.validate(this.userCreate, user);
+ if (Authorizations.isAdmin(loggedUser) || Authorizations.isSuperAdmin(loggedUser)) {
+ this.validate(this.userAdminCreate, request);
+ user = { ...user, ...request };
+ }
+ return user;
+ }
}

Added tenant definition schema