Skip to content

Commit be4dcc3

Browse files
author
Dushyant Bhalgami
authored
Merge pull request #133 from meshde/metadata-hiding
HOTFIX: Hide review metadata for non-admin non-copilot users when getting submission details
2 parents b335e5c + 0dc8ffe commit be4dcc3

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

src/common/helper.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,33 @@ function * postToBusApi (payload) {
554554
yield busApiClient.postEvent(payload)
555555
}
556556

557+
/**
558+
* Function to remove metadata details from reviews for members who shouldn't see them
559+
* @param {Array} reviews The reviews to remove metadata from
560+
* @param {Object} authUser The authenticated user details
561+
*/
562+
function cleanseReviews (reviews, authUser) {
563+
// Not a machine user
564+
if (!authUser.scopes) {
565+
const admin = _.filter(authUser.roles, role => role.toLowerCase() === 'Administrator'.toLowerCase())
566+
const copilot = _.filter(authUser.roles, role => role.toLowerCase() === 'Copilot'.toLowerCase())
567+
568+
// User is neither admin nor copilot
569+
if (admin.length === 0 && copilot.length === 0) {
570+
const cleansedReviews = []
571+
572+
_.forEach(reviews, (review) => {
573+
_.unset(review, 'metadata')
574+
cleansedReviews.push(review)
575+
})
576+
577+
return cleansedReviews
578+
}
579+
}
580+
581+
return reviews
582+
}
583+
557584
module.exports = {
558585
wrapExpress,
559586
autoWrapExpress,
@@ -566,5 +593,6 @@ module.exports = {
566593
checkGetAccess,
567594
checkReviewGetAccess,
568595
downloadFile,
569-
postToBusApi
596+
postToBusApi,
597+
cleanseReviews
570598
}

src/controllers/SubmissionController.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function * downloadSubmission (req, res) {
3737
* @param res the http response
3838
*/
3939
function * listSubmissions (req, res) {
40-
const data = yield SubmissionService.listSubmissions(req.query)
40+
const data = yield SubmissionService.listSubmissions(req.authUser, req.query)
4141
helper.setPaginationHeaders(req, res, data)
4242
}
4343

src/services/SubmissionService.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ function * getSubmission (authUser, submissionId) {
150150
yield helper.checkGetAccess(authUser, submissionRecord)
151151
}
152152

153+
submissionRecord.review = helper.cleanseReviews(submissionRecord.review, authUser)
154+
153155
// Return the retrieved submission
154156
logger.info(`getSubmission: returning data for submissionId: ${submissionId}`)
155157
return submissionRecord
@@ -174,11 +176,19 @@ function * downloadSubmission (authUser, submissionId) {
174176

175177
/**
176178
* Function to list submissions from Elastic Search
179+
* @param {Object} authUser Authenticated User
177180
* @param {Object} query Query filters passed in HTTP request
178181
* @return {Object} Data fetched from ES
179182
*/
180-
function * listSubmissions (query) {
181-
return yield helper.fetchFromES(query, helper.camelize(table))
183+
function * listSubmissions (authUser, query) {
184+
const data = yield helper.fetchFromES(query, helper.camelize(table))
185+
data.rows = _.map(data.rows, (submission) => {
186+
if (submission.review) {
187+
submission.review = helper.cleanseReviews(submission.review, authUser)
188+
}
189+
return submission
190+
})
191+
return data
182192
}
183193

184194
const listSubmissionsQuerySchema = {
@@ -209,6 +219,7 @@ listSubmissionsQuerySchema.sortBy = joi.string().valid(_.difference(
209219
))
210220

211221
listSubmissions.schema = {
222+
authUser: joi.object().required(),
212223
query: joi.object().keys(listSubmissionsQuerySchema).with('orderBy', 'sortBy')
213224
}
214225

0 commit comments

Comments
 (0)