Added getUsers schema validation
RENAME
src/assets/server/rest/v1/schemas/user/user-get-by-id.json
src/assets/server/rest/v1/schemas/user/user-get.json
{
- "title": "Get a user by id",
+ "title": "Get a User by id",
"type": "object",
"definitions": {
"ID": {

MODIFY
src/assets/server/rest/v1/schemas/user/user.json
src/assets/server/rest/v1/schemas/user/user.json
},
"role": {
"type": "string",
- "sanitize": "mongo"
+ "sanitize": "mongo",
+ "enum": ["S", "A", "B", "D"]
},
"notifications": {
"type": "object",

ADD
src/assets/server/rest/v1/schemas/user/users-get.json
+{
+ "title": "Get Users",
+ "type": "object",
+ "definitions": {
+ "Issuer": {
+ "type": "boolean",
+ "sanitize": "mongo"
+ },
+ "WithTag": {
+ "type": "boolean",
+ "sanitize": "mongo"
+ },
+ "SiteID": {
+ "$ref": "common.json#/definitions/ids"
+ },
+ "UserID": {
+ "$ref": "common.json#/definitions/ids"
+ },
+ "Roles": {
+ "type": "string",
+ "sanitize": "mongo",
+ "pattern": "^[SABD](\\|[SABD])*$"
+ },
+ "Status": {
+ "type": "string",
+ "sanitize": "mongo",
+ "pattern": "^[PAIBL](\\|[PAIBL])*$"
+ },
+ "ErrorType": {
+ "type": "string",
+ "sanitize": "mongo",
+ "pattern": "^((inactive_user|unassigned_user|inactive_user_account|failed_billing_synchro|no_billing_data)(\\||inactive_user|unassigned_user|inactive_user_account|failed_billing_synchro|no_billing_data)*)$"
+ },
+ "ExcludeSiteID": {
+ "$ref": "common.json#/definitions/ids"
+ },
+ "TagID": {
+ "$ref": "tag.json#/definitions/ids"
+ },
+ "ExcludeUserIDs": {
+ "$ref": "common.json#/definitions/ids"
+ },
+ "IncludeCarUserIDs": {
+ "$ref": "common.json#/definitions/ids"
+ },
+ "NotAssignedToCarID": {
+ "$ref": "common.json#/definitions/id"
+ },
+ "Search": {
+ "$ref": "common.json#/definitions/search"
+ },
+ "SortFields": {
+ "$ref": "common.json#/definitions/sortFields"
+ },
+ "Skip": {
+ "$ref": "common.json#/definitions/skip"
+ },
+ "OnlyRecordCount": {
+ "$ref": "common.json#/definitions/onlyRecordCount"
+ },
+ "ProjectFields": {
+ "$ref": "common.json#/definitions/projectFields"
+ }
+ },
+ "required": []
+}

MODIFY
src/server/rest/v1/service/UserService.ts
src/server/rest/v1/service/UserService.ts
@@ -382,7 +382,7 @@ export default class UserService {
public static async handleGetUsersInError(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
// Filter
- const filteredRequest = UserSecurity.filterUsersRequest(req.query);
+ const filteredRequest = UserValidator.getInstance().validateUsersGet(req.query);
// Get authorization filters
const authorizationUserInErrorFilters = await AuthorizationService.checkAndGetUsersInErrorAuthorizationFilters(
req.tenant, req.user, filteredRequest);
@@ -767,7 +767,7 @@ export default class UserService {
private static async getUsers(req: Request, res: Response, next: NextFunction): Promise<DataResult<User>> {
// Filter
- const filteredRequest = UserSecurity.filterUsersRequest(req.query);
+ const filteredRequest = UserValidator.getInstance().validateUsersGet(req.query);
// Get authorization filters
const authorizationUsersFilters = await AuthorizationService.checkAndGetUsersAuthorizationFilters(
req.tenant, req.user, filteredRequest);

MODIFY
src/server/rest/v1/validator/UserValidator.ts
src/server/rest/v1/validator/UserValidator.ts
+import { HttpUserAssignSitesRequest, HttpUsersRequest } from '../../../../types/requests/HttpUserRequest';
import User, { ImportedUser } from '../../../../types/User';
import HttpByIDRequest from '../../../../types/requests/HttpByIDRequest';
-import { HttpUserAssignSitesRequest } from '../../../../types/requests/HttpUserRequest';
import Schema from '../../../../types/validator/Schema';
import SchemaValidator from './SchemaValidator';
import fs from 'fs';
@@ -13,13 +13,15 @@ export default class UserValidator extends SchemaValidator {
private userCreate: Schema;
private userAssignSites: Schema;
private userGetByID: Schema;
+ private usersGet: 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.userAssignSites = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/user/user-assign-sites.json`, 'utf8'));
- this.userGetByID = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/user/user-get-by-id.json`, 'utf8'));
+ this.userGetByID = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/user/user-get.json`, 'utf8'));
+ this.usersGet = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/user/users-get.json`, 'utf8'));
}
public static getInstance(): UserValidator {
@@ -48,4 +50,9 @@ export default class UserValidator extends SchemaValidator {
this.validate(this.userGetByID, data);
return data;
}
+
+ validateUsersGet(data: any): HttpUsersRequest {
+ this.validate(this.usersGet, data);
+ return data;
+ }
}

Added getUsers schema validation