Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const cookieParser = require('cookie-parser');
const logger = require('morgan');
var debug = require('debug')('report-service:server');

const mountRoutes = require('./routes')
const mountRoutes = require('./routes');
const app = express();
const { sequelize } = require('./models');
const { printEnvVariablesStatus } = require('./helpers/envHelpers');
Expand All @@ -17,16 +17,16 @@ app.use(express.static(path.join(__dirname, 'public')));

//connect to the database and mount routes
(async () => {
try {
printEnvVariablesStatus();
await sequelize.authenticate();
// await sequelize.sync();
debug('Connected to the database');
mountRoutes(app);
} catch (error) {
debug('Unable to connect to database', error);
process.exit(1);
}
})()
try {
printEnvVariablesStatus();
await sequelize.authenticate();
// await sequelize.sync();
debug('Connected to the database');
mountRoutes(app);
} catch (error) {
debug('Unable to connect to database', error);
process.exit(1);
}
})();

module.exports = app;
22 changes: 11 additions & 11 deletions controllers/accessPaths/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ const _ = require('lodash');
*/

module.exports = {
ruleName: 'block',
isMatch(user, payload) {
payload = Array.isArray(payload) ? payload : [payload];
const userProfileLocation = _.get(user, 'profileLocation') || _.get(user, 'userLocations');
if (!userProfileLocation) return false;
if (!Array.isArray(userProfileLocation)) return false;
const userBlock = _.find(userProfileLocation, location => _.get(location, 'type') === 'block');
if (!(userBlock && ('id' in userBlock))) return false;
return _.some(payload, locationId => locationId === userBlock.id);
}
}
ruleName: 'block',
isMatch(user, payload) {
payload = Array.isArray(payload) ? payload : [payload];
const userProfileLocation = _.get(user, 'profileLocation') || _.get(user, 'userLocations');
if (!userProfileLocation) return false;
if (!Array.isArray(userProfileLocation)) return false;
const userBlock = _.find(userProfileLocation, location => _.get(location, 'type') === 'block');
if (!(userBlock && ('id' in userBlock))) return false;
return _.some(payload, locationId => locationId === userBlock.id);
}
};
30 changes: 15 additions & 15 deletions controllers/accessPaths/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ const _ = require('lodash');
*/

module.exports = {
ruleName: 'board',
isMatch(user, payload) {
payload = Array.isArray(payload) ? payload : [payload];
const userBoards = _.get(user, 'framework.board');
if (!userBoards) return false;
if (!Array.isArray(userBoards)) return false;
return _.some(payload, board => {
board = _.toLower(board);
if (_.find(userBoards, userBoard => _.toLower(userBoard) === board)) {
return true;
}
ruleName: 'board',
isMatch(user, payload) {
payload = Array.isArray(payload) ? payload : [payload];
const userBoards = _.get(user, 'framework.board');
if (!userBoards) return false;
if (!Array.isArray(userBoards)) return false;
return _.some(payload, board => {
board = _.toLower(board);
if (_.find(userBoards, userBoard => _.toLower(userBoard) === board)) {
return true;
}

return false;
});
}
}
return false;
});
}
};
16 changes: 8 additions & 8 deletions controllers/accessPaths/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ const _ = require('lodash');
*/

module.exports = {
ruleName: 'channel',
isMatch(user, payload) {
payload = Array.isArray(payload) ? payload : [payload];
const userChannelId = _.get(user, 'rootOrg.hashTagId') || _.get(user, 'rootOrg.channel') || _.get(user, 'channel');
if (!userChannelId) return false;
return _.some(payload, channel => channel === userChannelId);
}
}
ruleName: 'channel',
isMatch(user, payload) {
payload = Array.isArray(payload) ? payload : [payload];
const userChannelId = _.get(user, 'rootOrg.hashTagId') || _.get(user, 'rootOrg.channel') || _.get(user, 'channel');
if (!userChannelId) return false;
return _.some(payload, channel => channel === userChannelId);
}
};
22 changes: 11 additions & 11 deletions controllers/accessPaths/district.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ const _ = require('lodash');
*/

module.exports = {
ruleName: 'district',
isMatch(user, payload) {
payload = Array.isArray(payload) ? payload : [payload];
const userProfileLocation = _.get(user, 'profileLocation') || _.get(user, 'userLocations');
if (!userProfileLocation) return false;
if (!Array.isArray(userProfileLocation)) return false;
const userDistrict = _.find(userProfileLocation, location => _.get(location, 'type') === 'district');
if (!(userDistrict && ('id' in userDistrict))) return false;
return _.some(payload, locationId => locationId === userDistrict.id);
}
}
ruleName: 'district',
isMatch(user, payload) {
payload = Array.isArray(payload) ? payload : [payload];
const userProfileLocation = _.get(user, 'profileLocation') || _.get(user, 'userLocations');
if (!userProfileLocation) return false;
if (!Array.isArray(userProfileLocation)) return false;
const userDistrict = _.find(userProfileLocation, location => _.get(location, 'type') === 'district');
if (!(userDistrict && ('id' in userDistrict))) return false;
return _.some(payload, locationId => locationId === userDistrict.id);
}
};
28 changes: 14 additions & 14 deletions controllers/accessPaths/framework.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ const _ = require('lodash');
*/

module.exports = {
ruleName: 'framework',
isMatch(user, payload) {
payload = Array.isArray(payload) ? payload : [payload];
ruleName: 'framework',
isMatch(user, payload) {
payload = Array.isArray(payload) ? payload : [payload];

const frameworkIds = _.get(user, 'framework.id');
if (!frameworkIds) return false;
if (!Array.isArray(frameworkIds)) return false;
const frameworkIds = _.get(user, 'framework.id');
if (!frameworkIds) return false;
if (!Array.isArray(frameworkIds)) return false;

return _.some(payload, frameworkId => {
if (_.find(frameworkIds, id => id === frameworkId)) {
return true;
}
return _.some(payload, frameworkId => {
if (_.find(frameworkIds, id => id === frameworkId)) {
return true;
}

return false;
});
}
}
return false;
});
}
};
30 changes: 15 additions & 15 deletions controllers/accessPaths/gradeLevel.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ const _ = require('lodash');
*/

module.exports = {
ruleName: 'gradeLevel',
isMatch(user, payload) {
payload = Array.isArray(payload) ? payload : [payload];
const userGradeLevels = _.get(user, 'framework.gradeLevel');
if (!userGradeLevels) return false;
if (!Array.isArray(userGradeLevels)) return false;
return _.some(payload, gradeLevel => {
gradeLevel = _.toLower(gradeLevel);
if (_.find(userGradeLevels, userGradeLevel => _.toLower(userGradeLevel) === gradeLevel)) {
return true;
}
ruleName: 'gradeLevel',
isMatch(user, payload) {
payload = Array.isArray(payload) ? payload : [payload];
const userGradeLevels = _.get(user, 'framework.gradeLevel');
if (!userGradeLevels) return false;
if (!Array.isArray(userGradeLevels)) return false;
return _.some(payload, gradeLevel => {
gradeLevel = _.toLower(gradeLevel);
if (_.find(userGradeLevels, userGradeLevel => _.toLower(userGradeLevel) === gradeLevel)) {
return true;
}

return false;
});
}
}
return false;
});
}
};
100 changes: 50 additions & 50 deletions controllers/accessPaths/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ isMatch function validates the given payload against the user context data and r
const rules = new Map();

((folderPath) => {
fs.readdirSync(folderPath)
.filter(file => file !== basename)
.forEach(file => {
const { ruleName, isMatch } = require(path.join(folderPath, file));
rules.set(ruleName, isMatch);
})
fs.readdirSync(folderPath)
.filter(file => file !== basename)
.forEach(file => {
const { ruleName, isMatch } = require(path.join(folderPath, file));
rules.set(ruleName, isMatch);
});
})(__dirname);

//check if the user is the creator of the report or not.
Expand All @@ -35,65 +35,65 @@ const isCreatorOfReport = ({ user, report }) => _.get(report, 'createdby') === (
* @param {*} user
*/
const validateAccessPath = user => report => {
let { accesspath, type } = report;
let { accesspath, type } = report;

if (type === CONSTANTS.REPORT_TYPE.PUBLIC) return true;
if (type === CONSTANTS.REPORT_TYPE.PUBLIC) return true;

if (type === CONSTANTS.REPORT_TYPE.PROTECTED) {
if (!accesspath) return false;
if (typeof accesspath !== 'object') return false;
}
if (type === CONSTANTS.REPORT_TYPE.PROTECTED) {
if (!accesspath) return false;
if (typeof accesspath !== 'object') return false;
}

if (type === CONSTANTS.REPORT_TYPE.PRIVATE && !accesspath) {
// if report is private then it should be accessible only by the creator of the report.
accesspath = accessPathForPrivateReports({ user });
}
if (type === CONSTANTS.REPORT_TYPE.PRIVATE && !accesspath) {
// if report is private then it should be accessible only by the creator of the report.
accesspath = accessPathForPrivateReports({ user });
}

for (let [key, value] of Object.entries(accesspath)) {
if (!rules.has(key)) return false;
const validator = rules.get(key);
const success = validator(user, value);
if (!success) return false;
}
for (let [key, value] of Object.entries(accesspath)) {
if (!rules.has(key)) return false;
const validator = rules.get(key);
const success = validator(user, value);
if (!success) return false;
}

return true;
}
return true;
};

/**
* @description func used when access path is sent in the filters for search query. Used to filter out the reports
* @param {*} accessPathSearchPayload
* @return {*}
*/
const matchAccessPath = accessPathSearchPayload => {
const accessPathSearchPayloadIterable = Object.entries(accessPathSearchPayload);
const accessPathSearchPayloadIterable = Object.entries(accessPathSearchPayload);

return report => {
const { accesspath: reportAccessPath } = report;
if (!reportAccessPath) return false;
return report => {
const { accesspath: reportAccessPath } = report;
if (!reportAccessPath) return false;

for (let [ruleName, value] of accessPathSearchPayloadIterable) {
value = Array.isArray(value) ? value : [value];
for (let [ruleName, value] of accessPathSearchPayloadIterable) {
value = Array.isArray(value) ? value : [value];

if (!(ruleName in reportAccessPath)) return false;
if (!(ruleName in reportAccessPath)) return false;

let ruleValue = reportAccessPath[ruleName];
ruleValue = Array.isArray(ruleValue) ? ruleValue : [ruleValue];
if (_.intersection(ruleValue, value).length === 0) return false;
}
return true;
let ruleValue = reportAccessPath[ruleName];
ruleValue = Array.isArray(ruleValue) ? ruleValue : [ruleValue];
if (_.intersection(ruleValue, value).length === 0) return false;
}
}
return true;
};
};

/**
* @description private reports should should have accesspath set as userId of the creator
* @param {*} { user }
* @return {*}
*/
const accessPathForPrivateReports = ({ user }) => {
if (user) {
return { userId: _.get(user, 'identifier') || _.get(user, 'id') }
}
return null;
if (user) {
return { userId: _.get(user, 'identifier') || _.get(user, 'id') };
}
return null;
};

/**
Expand All @@ -102,16 +102,16 @@ const accessPathForPrivateReports = ({ user }) => {
* @return {*}
*/
const roleBasedAccess = ({ report, user }) => {
if (!user) return false;
const { status } = report;
if ([CONSTANTS.REPORT_STATUS.DRAFT, CONSTANTS.REPORT_STATUS.RETIRED].includes(status)) {
if (!isUserAdmin(user)) {
return false;
}
if (!user) return false;
const { status } = report;
if ([CONSTANTS.REPORT_STATUS.DRAFT, CONSTANTS.REPORT_STATUS.RETIRED].includes(status)) {
if (!isUserAdmin(user)) {
return false;
}
}

return true;
}
return true;
};

module.exports = { validateAccessPath, matchAccessPath, accessPathForPrivateReports, isCreatorOfReport, roleBasedAccess }
module.exports = { validateAccessPath, matchAccessPath, accessPathForPrivateReports, isCreatorOfReport, roleBasedAccess };

14 changes: 7 additions & 7 deletions controllers/accessPaths/isMinor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ const _ = require('lodash');
*/

module.exports = {
ruleName: 'isMinor',
isMatch(user, payload) {
//payload is boolean here
const isUserMinor = _.get(user, 'isMinor') || false;
return isUserMinor === payload
}
}
ruleName: 'isMinor',
isMatch(user, payload) {
//payload is boolean here
const isUserMinor = _.get(user, 'isMinor') || false;
return isUserMinor === payload;
}
};
Loading