|
| 1 | + |
| 2 | +import _ from 'lodash'; |
| 3 | +import util from '../util'; |
| 4 | +import models from '../models'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Topcoder admin and Project managers who are part of the project can view the copilot applications in it |
| 8 | + * Also, users who had an application will have access to view it. |
| 9 | + * @param {Object} freq the express request instance |
| 10 | + * @return {Promise} Returns a promise |
| 11 | + */ |
| 12 | +module.exports = freq => new Promise((resolve, reject) => { |
| 13 | + console.log("start permission check"); |
| 14 | + const opportunityId = _.parseInt(freq.params.id); |
| 15 | + const currentUserId = freq.authUser.userId; |
| 16 | + return models.CopilotOpportunity.find({ |
| 17 | + where: { |
| 18 | + id: opportunityId, |
| 19 | + }, |
| 20 | + }) |
| 21 | + .then((opportunity) => { |
| 22 | + const req = freq; |
| 23 | + req.context = req.context || {}; |
| 24 | + req.context.currentOpportunity = opportunity; |
| 25 | + const projectId = opportunity.projectId; |
| 26 | + const isProjectManager = util.hasProjectManagerRole(req); |
| 27 | + |
| 28 | + console.log("got opportunity", opportunityId); |
| 29 | + return models.ProjectMember.getActiveProjectMembers(projectId) |
| 30 | + .then((members) => { |
| 31 | + |
| 32 | + console.log("got active members", projectId); |
| 33 | + return models.CopilotApplications.findOne({ |
| 34 | + where: { |
| 35 | + opportunityId: opportunityId, |
| 36 | + userId: currentUserId, |
| 37 | + }, |
| 38 | + }).then((copilotApplication) => { |
| 39 | + const isPartOfProject = isProjectManager && members.find(member => member.userId === currentUserId); |
| 40 | + // check if auth user has acecss to this project |
| 41 | + const hasAccess = util.hasAdminRole(req) || isPartOfProject || !!copilotApplication; |
| 42 | + console.log("got assigned application", hasAccess); |
| 43 | + return Promise.resolve(hasAccess); |
| 44 | + }) |
| 45 | + }) |
| 46 | + }) |
| 47 | + .then((hasAccess) => { |
| 48 | + if (!hasAccess) { |
| 49 | + const errorMessage = 'You do not have permissions to perform this action'; |
| 50 | + // user is not an admin nor is a registered project member |
| 51 | + return reject(new Error(errorMessage)); |
| 52 | + } |
| 53 | + return resolve(true); |
| 54 | + }); |
| 55 | +}); |
0 commit comments