Skip to content

Commit

Permalink
Merge pull request #547 from OneCommunityGlobal/nathan-individual-per…
Browse files Browse the repository at this point in the history
…missions-fix

Nathan individual permissions fix
  • Loading branch information
one-community authored Oct 16, 2023
2 parents b3e5d63 + 92059d5 commit aa329ed
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 126 deletions.
12 changes: 6 additions & 6 deletions src/controllers/badgeController.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const mongoose = require('mongoose');
const UserProfile = require('../models/userProfile');
const { hasPermission, hasIndividualPermission } = require('../utilities/permissions');
const { hasPermission } = require('../utilities/permissions');
const escapeRegex = require('../utilities/escapeRegex');

const badgeController = function (Badge) {
const getAllBadges = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'seeBadges') && !await hasIndividualPermission(req.body.requestor.requestorId, 'seeBadges')) {
if (!await hasPermission(req.body.requestor, 'seeBadges')) {
res.status(403).send('You are not authorized to view all badge data.');
return;
}
Expand All @@ -26,7 +26,7 @@ const badgeController = function (Badge) {
};

const assignBadges = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'assignBadges')) {
if (!await hasPermission(req.body.requestor, 'assignBadges')) {
res.status(403).send('You are not authorized to assign badges.');
return;
}
Expand Down Expand Up @@ -57,7 +57,7 @@ const badgeController = function (Badge) {
};

const postBadge = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'createBadges')) {
if (!await hasPermission(req.body.requestor, 'createBadges')) {
res.status(403).send({ error: 'You are not authorized to create new badges.' });
return;
}
Expand Down Expand Up @@ -91,7 +91,7 @@ const badgeController = function (Badge) {
};

const deleteBadge = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'deleteBadges')) {
if (!await hasPermission(req.body.requestor, 'deleteBadges')) {
res.status(403).send({ error: 'You are not authorized to delete badges.' });
return;
}
Expand All @@ -112,7 +112,7 @@ const badgeController = function (Badge) {
};

const putBadge = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'updateBadges')) {
if (!await hasPermission(req.body.requestor, 'updateBadges')) {
res.status(403).send({ error: 'You are not authorized to update badges.' });
return;
}
Expand Down
26 changes: 13 additions & 13 deletions src/controllers/inventoryController.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const escapeRegex = require('../utilities/escapeRegex');

const inventoryController = function (Item, ItemType) {
const getAllInvInProjectWBS = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'getAllInvInProjectWBS')) {
if (!await hasPermission(req.body.requestor, 'getAllInvInProjectWBS')) {
return res.status(403).send('You are not authorized to view inventory data.');
}
// use req.params.projectId and wbsId
Expand Down Expand Up @@ -40,7 +40,7 @@ const inventoryController = function (Item, ItemType) {
};

const postInvInProjectWBS = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'postInvInProjectWBS')) {
if (!await hasPermission(req.body.requestor, 'postInvInProjectWBS')) {
return res.status(403).send('You are not authorized to view inventory data.');
}
// use req.body.projectId and req.body.wbsId req.body.quantity,
Expand Down Expand Up @@ -108,7 +108,7 @@ const inventoryController = function (Item, ItemType) {


const getAllInvInProject = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'getAllInvInProject')) {
if (!await hasPermission(req.body.requestor, 'getAllInvInProject')) {
return res.status(403).send('You are not authorized to view inventory data.');
}
// same as getAllInvInProjectWBS but just using only the project to find the items of inventory
Expand Down Expand Up @@ -140,7 +140,7 @@ const inventoryController = function (Item, ItemType) {
};

const postInvInProject = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'postInvInProject')) {
if (!await hasPermission(req.body.requestor, 'postInvInProject')) {
return res.status(403).send('You are not authorized to post new inventory data.');
}
// same as posting an item inProjectWBS but the WBS is uanassigned(i.e. null)
Expand Down Expand Up @@ -194,7 +194,7 @@ const inventoryController = function (Item, ItemType) {
};

const transferInvById = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'transferInvById')) {
if (!await hasPermission(req.body.requestor, 'transferInvById')) {
return res.status(403).send('You are not authorized to transfer inventory data.');
}
// This function transfer inventory by id
Expand Down Expand Up @@ -283,7 +283,7 @@ const inventoryController = function (Item, ItemType) {


const delInvById = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'delInvById')) {
if (!await hasPermission(req.body.requestor, 'delInvById')) {
return res.status(403).send('You are not authorized to waste inventory.');
}
// send result just sending something now to have it work and not break anything
Expand Down Expand Up @@ -372,7 +372,7 @@ const inventoryController = function (Item, ItemType) {
};

const unWasteInvById = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'unWasteInvById')) {
if (!await hasPermission(req.body.requestor, 'unWasteInvById')) {
return res.status(403).send('You are not authorized to unwaste inventory.');
}
const properUnWaste = await Item.findOne({ _id: req.params.invId, quantity: { $gte: req.body.quantity }, wasted: true }).select('_id').lean();
Expand Down Expand Up @@ -453,7 +453,7 @@ const inventoryController = function (Item, ItemType) {
};

const getInvIdInfo = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'getInvIdInfo')) {
if (!await hasPermission(req.body.requestor, 'getInvIdInfo')) {
return res.status(403).send('You are not authorized to get inventory by id.');
}
// req.params.invId
Expand All @@ -465,7 +465,7 @@ const inventoryController = function (Item, ItemType) {
};

const putInvById = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'putInvById')) {
if (!await hasPermission(req.body.requestor, 'putInvById')) {
return res.status(403).send('You are not authorized to edit inventory by id.');
}
// update the inv by id.
Expand Down Expand Up @@ -493,7 +493,7 @@ const inventoryController = function (Item, ItemType) {
};

const getInvTypeById = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'getInvTypeById')) {
if (!await hasPermission(req.body.requestor, 'getInvTypeById')) {
return res.status(403).send('You are not authorized to get inv type by id.');
}
// send result just sending something now to have it work and not break anything
Expand All @@ -504,7 +504,7 @@ const inventoryController = function (Item, ItemType) {
};

const putInvType = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'putInvType')) {
if (!await hasPermission(req.body.requestor, 'putInvType')) {
return res.status(403).send('You are not authorized to edit an inventory type.');
}
const { typeId } = req.params;
Expand All @@ -527,7 +527,7 @@ const inventoryController = function (Item, ItemType) {
};

const getAllInvType = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'getAllInvType')) {
if (!await hasPermission(req.body.requestor, 'getAllInvType')) {
return res.status(403).send('You are not authorized to get all inventory.');
}
// send result just sending something now to have it work and not break anything
Expand All @@ -537,7 +537,7 @@ const inventoryController = function (Item, ItemType) {
};

const postInvType = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'postInvType')) {
if (!await hasPermission(req.body.requestor, 'postInvType')) {
return res.status(403).send('You are not authorized to save an inventory type.');
}
return ItemType.find({ name: { $regex: escapeRegex(req.body.name), $options: 'i' } })
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/popupEditorBackupController.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const popupEditorBackupController = function (PopupEditorBackups) {


const createPopupEditorBackup = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'createPopup')) {
if (!await hasPermission(req.body.requestor, 'createPopup')) {
res
.status(403)
.send({ error: 'You are not authorized to create new popup' });
Expand All @@ -46,7 +46,7 @@ const popupEditorBackupController = function (PopupEditorBackups) {
};

const updatePopupEditorBackup = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'updatePopup')) {
if (!await hasPermission(req.body.requestor, 'updatePopup')) {
res
.status(403)
.send({ error: 'You are not authorized to create new popup' });
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/popupEditorController.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const popupEditorController = function (PopupEditors) {


const createPopupEditor = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'createPopup')) {
if (!await hasPermission(req.body.requestor, 'createPopup')) {
res
.status(403)
.send({ error: 'You are not authorized to create new popup' });
Expand All @@ -38,7 +38,7 @@ const popupEditorController = function (PopupEditors) {
};

const updatePopupEditor = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'updatePopup')) {
if (!await hasPermission(req.body.requestor, 'updatePopup')) {
res
.status(403)
.send({ error: 'You are not authorized to create new popup' });
Expand Down
20 changes: 7 additions & 13 deletions src/controllers/projectController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const mongoose = require('mongoose');
const timeentry = require('../models/timeentry');
const userProfile = require('../models/userProfile');
const userProject = require('../helpers/helperModels/userProjects');
const { hasPermission, hasIndividualPermission } = require('../utilities/permissions');
const { hasPermission } = require('../utilities/permissions');
const escapeRegex = require('../utilities/escapeRegex');


Expand All @@ -14,10 +14,9 @@ const projectController = function (Project) {
.catch(error => res.status(404).send(error));
};

const deleteProject = function (req, res) {
if (!hasPermission(req.body.requestor.role, 'deleteProject')
&& !hasIndividualPermission(req.body.requestor.requestorId, 'seeProjectManagement')) {
res.status(403).send({ error: 'You are not authorized to delete projects.' });
const deleteProject = async function (req, res) {
if (!await hasPermission(req.body.requestor, 'deleteProject')) {
res.status(403).send({ error: 'You are not authorized to delete projects.' });
return;
}
const { projectId } = req.params;
Expand Down Expand Up @@ -47,8 +46,7 @@ const projectController = function (Project) {
};

const postProject = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'postProject')
&& !await hasIndividualPermission(req.body.requestor.requestorId, 'seeProjectManagement')) {
if (!await hasPermission(req.body.requestor, 'postProject')) {
res.status(403).send({ error: 'You are not authorized to create new projects.' });
return;
}
Expand Down Expand Up @@ -79,8 +77,7 @@ const projectController = function (Project) {


const putProject = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'putProject')
&& !await hasIndividualPermission(req.body.requestor.requestorId, 'seeProjectManagement')) {
if (!await hasPermission(req.body.requestor, 'putProject')) {
res.status(403).send('You are not authorized to make changes in the projects.');
return;
}
Expand Down Expand Up @@ -127,12 +124,9 @@ const projectController = function (Project) {
const assignProjectToUsers = async function (req, res) {
// verify requestor is administrator, projectId is passed in request params and is valid mongoose objectid, and request body contains an array of users

if (!await hasPermission(req.body.requestor.role, 'assignProjectToUsers')) {
if (!await hasIndividualPermission(req.body.requestor.requestorId, 'seeProjectManagement')
&& !await hasIndividualPermission(req.body.requestor.requestorId, 'seeProjectManagementTab')) {
if (!await hasPermission(req.body.requestor, 'assignProjectToUsers')) {
res.status(403).send({ error: 'You are not authorized to perform this operation' });
return;
}
}

if (!req.params.projectId || !mongoose.Types.ObjectId.isValid(req.params.projectId) || !req.body.users || (req.body.users.length === 0)) {
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/reportsController.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const reporthelper = require('../helpers/reporthelper')();
const { hasPermission, hasIndividualPermission } = require('../utilities/permissions');
const { hasPermission } = require('../utilities/permissions');

const reportsController = function () {
const getWeeklySummaries = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'getWeeklySummaries') && !await hasIndividualPermission(req.body.requestor.requestorId, 'getWeeklySummaries')) {
if (!await hasPermission(req.body.requestor, 'getWeeklySummaries')) {
res.status(403).send('You are not authorized to view all users');
return;
}
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/rolesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const rolesController = function (Role) {
};

const createNewRole = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'postRole')) {
if (!await hasPermission(req.body.requestor, 'postRole')) {
res.status(403).send('You are not authorized to create new roles.');
return;
}
Expand Down Expand Up @@ -39,7 +39,7 @@ const rolesController = function (Role) {


const updateRoleById = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'putRole')) {
if (!await hasPermission(req.body.requestor, 'putRole')) {
res.status(403).send('You are not authorized to make changes to roles.');
return;
}
Expand Down Expand Up @@ -67,7 +67,7 @@ const rolesController = function (Role) {
};

const deleteRoleById = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'deleteRole')) {
if (!await hasPermission(req.body.requestor, 'deleteRole')) {
res.status(403).send('You are not authorized to delete roles.');
return;
}
Expand Down
14 changes: 7 additions & 7 deletions src/controllers/taskController.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ const taskController = function (Task) {
};

const importTask = async (req, res) => {
if (!await hasPermission(req.body.requestor.role, 'importTask')) {
if (!await hasPermission(req.body.requestor, 'importTask')) {
res
.status(403)
.send({ error: 'You are not authorized to create new Task.' });
Expand Down Expand Up @@ -420,7 +420,7 @@ const taskController = function (Task) {
};

const postTask = async (req, res) => {
if (!await hasPermission(req.body.requestor.role, 'postTask')) {
if (!await hasPermission(req.body.requestor, 'postTask')) {
res
.status(403)
.send({ error: 'You are not authorized to create new Task.' });
Expand Down Expand Up @@ -456,7 +456,7 @@ const taskController = function (Task) {
};

const updateNum = async (req, res) => {
if (!await hasPermission(req.body.requestor.role, 'updateNum')) {
if (!await hasPermission(req.body.requestor, 'updateNum')) {
res
.status(403)
.send({ error: 'You are not authorized to create new projects.' });
Expand Down Expand Up @@ -593,7 +593,7 @@ const taskController = function (Task) {
};

const deleteTask = async (req, res) => {
if (!await hasPermission(req.body.requestor.role, 'deleteTask')) {
if (!await hasPermission(req.body.requestor, 'deleteTask')) {
res
.status(403)
.send({ error: 'You are not authorized to deleteTasks.' });
Expand Down Expand Up @@ -642,7 +642,7 @@ const taskController = function (Task) {
};

const deleteTaskByWBS = async (req, res) => {
if (!await hasPermission(req.body.requestor.role, 'deleteTask')) {
if (!await hasPermission(req.body.requestor, 'deleteTask')) {
res
.status(403)
.send({ error: 'You are not authorized to deleteTasks.' });
Expand Down Expand Up @@ -673,7 +673,7 @@ const taskController = function (Task) {
};

const updateTask = async (req, res) => {
if (!await hasPermission(req.body.requestor.role, 'updateTask')) {
if (!await hasPermission(req.body.requestor, 'updateTask')) {
res.status(403).send({ error: 'You are not authorized to update Task.' });
return;
}
Expand All @@ -689,7 +689,7 @@ const taskController = function (Task) {
};

const swap = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'swapTask')) {
if (!await hasPermission(req.body.requestor, 'swapTask')) {
res
.status(403)
.send({ error: 'You are not authorized to create new projects.' });
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/teamController.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const teamcontroller = function (Team) {
.catch(error => res.send(error).status(404));
};
const postTeam = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'postTeam')) {
if (!await hasPermission(req.body.requestor, 'postTeam')) {
res.status(403).send({ error: 'You are not authorized to create teams.' });
return;
}
Expand All @@ -35,7 +35,7 @@ const teamcontroller = function (Team) {
.catch(error => res.send(error).status(404));
};
const deleteTeam = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'deleteTeam')) {
if (!await hasPermission(req.body.requestor, 'deleteTeam')) {
res.status(403).send({ error: 'You are not authorized to delete teams.' });
return;
}
Expand All @@ -58,7 +58,7 @@ const teamcontroller = function (Team) {
});
};
const putTeam = async function (req, res) {
if (!await hasPermission(req.body.requestor.role, 'putTeam')) {
if (!await hasPermission(req.body.requestor, 'putTeam')) {
res.status(403).send('You are not authorized to make changes in the teams.');
return;
}
Expand All @@ -85,7 +85,7 @@ const teamcontroller = function (Team) {
const assignTeamToUsers = async function (req, res) {
// verify requestor is administrator, teamId is passed in request params and is valid mongoose objectid, and request body contains an array of users

if (!await hasPermission(req.body.requestor.role, 'assignTeamToUsers')) {
if (!await hasPermission(req.body.requestor, 'assignTeamToUsers')) {
res.status(403).send({ error: 'You are not authorized to perform this operation' });
return;
}
Expand Down
Loading

0 comments on commit aa329ed

Please sign in to comment.