Skip to content

Commit

Permalink
Added ConvertStringToArray
Browse files Browse the repository at this point in the history
Converts string into Array, with basic checks
  • Loading branch information
pepijndik committed Dec 4, 2024
1 parent 5c50174 commit 560ff11
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
16 changes: 16 additions & 0 deletions Control/lib/common/StringToArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Convert string to list
* @param {String|Array} data
* @returns {Array} list
*/
const stringToArray = (data) => {

let list = [];
if (typeof data === 'string') {
list = data.split(',');
} else if (Array.isArray(data)) {
list = data;
}
return list;
}
exports.stringToArray = stringToArray;
12 changes: 3 additions & 9 deletions Control/lib/middleware/detectorOwnership.middleware.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const {User} = require('../dtos/User');
const {isRoleSufficient,Role} = require('../common/role.enum.js');
const {UnauthorizedAccessError} = require('./../errors/UnauthorizedAccessError.js');

const {stringToArray} = require('../common/StringToArray.js');
const {updateExpressResponseFromNativeError} = require('./../errors/updateExpressResponseFromNativeError.js');
/**
* Middleware function to check detector ownership.
Expand All @@ -20,14 +20,8 @@ const detectorOwnershipMiddleware = (req, res, next) => {
}

try {


let accessList = [];
if (typeof access === 'string') {
accessList = access.split(',');
} else if (Array.isArray(access)) {
accessList = access;
}
// Convert access string to Array
let accessList = stringToArray(access);
// Check if the user's role is sufficient to bypass the ownership check
if (accessList?.some((role) => {
return isRoleSufficient(role, Role.GLOBAL)
Expand Down
9 changes: 2 additions & 7 deletions Control/lib/middleware/minimumRole.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
const {isRoleSufficient} = require('../common/role.enum.js');
const {UnauthorizedAccessError} = require('../errors/UnauthorizedAccessError.js');
const {updateExpressResponseFromNativeError} = require('../errors/updateExpressResponseFromNativeError.js');

const {stringToArray} = require('../common/StringToArray.js');
/**
* Method to receive a minimum role that needs to be met by owner of request and to return a middleware function
* @param {Role} minimumRole - minimum role that should be fulfilled by the requestor
Expand All @@ -33,12 +33,7 @@ const minimumRoleMiddleware = (minimumRole) => {
try {
const { access } = req?.session ?? '';

let accessList = [];
if (typeof access === 'string') {
accessList = access.split(',');
} else if (Array.isArray(access)) {
accessList = access;
}
let accessList = stringToArray(access);
const isAllowed = accessList.some((role) => isRoleSufficient(role, minimumRole));
if (!isAllowed) {
updateExpressResponseFromNativeError(res,
Expand Down

0 comments on commit 560ff11

Please sign in to comment.