This commit is contained in:
xfarrow
2024-02-15 16:23:47 +01:00
parent 9053ea6c5f
commit 1891ee2067
2 changed files with 31 additions and 18 deletions

View File

@ -289,6 +289,7 @@ async function createOrganization(req, res){
* PUT Request * PUT Request
* Updates an Organization's details * Updates an Organization's details
* *
* Required field(s): none.
*/ */
async function updateOrganization(req, res){ async function updateOrganization(req, res){
@ -364,7 +365,7 @@ async function updateOrganization(req, res){
return res.status(200).json({ success : "true"}); return res.status(200).json({ success : "true"});
} }
else{ else{
return res.status(404).json({error : "Company either not found or not sufficient permissions"}); return res.status(404).json({error : "Organization either not found or not sufficient permissions"});
} }
} }
catch (error) { catch (error) {
@ -416,6 +417,7 @@ async function deleteOrganization(req, res){
* *
* Creates a Post belonging to an organization * Creates a Post belonging to an organization
* *
* Required field(s): organization_id, content
* @returns the inserted Post * @returns the inserted Post
*/ */
async function createOrganizationPost(req, res){ async function createOrganizationPost(req, res){
@ -457,6 +459,9 @@ async function createOrganizationPost(req, res){
* GET Request * GET Request
* *
* Obtains an organization by its identifier. * Obtains an organization by its identifier.
*
* Required field(s): none.
*
* @returns the organization. * @returns the organization.
*/ */
async function getOrganization(req, res){ async function getOrganization(req, res){
@ -479,30 +484,36 @@ async function getOrganization(req, res){
} }
} }
// DELETE /**
* DELETE Request
*
* Deletes a Post belonging to an Organization, only if
* the logged user is an administrator of that Organization.
*
* Required field(s): none.
*/
async function deleteOrganizationPost(req, res){ async function deleteOrganizationPost(req, res){
const organizationPostIdToDelete = req.params.id; const organizationPostIdToDelete = req.params.id;
try{ try{
knex.transaction(async (trx) => { const isOrganizationAdmin = await knex('OrganizationPost')
// Check if user is allowed to delete the post (they must have created it) .join('OrganizationAdministrator', 'OrganizationPost.organization_id', 'OrganizationAdministrator.id_organization')
const isOrganizationAdmin = await trx('OrganizationPost') .where('OrganizationPost.id', organizationPostIdToDelete)
.join('OrganizationAdministrator', 'OrganizationPost.organization_id', 'OrganizationAdministrator.id_organization') .where('OrganizationAdministrator.id_person', req.jwt.person_id)
.where('OrganizationPost.id', organizationPostIdToDelete) .select('*')
.where('OrganizationAdministrator.id_person', req.jwt.person_id) .first();
.select('*')
.first(); // Unexploitable TOC/TOU
if(isOrganizationAdmin){
if (isOrganizationAdmin) { await knex('OrganizationPost')
await trx('OrganizationPost')
.where('id', organizationPostIdToDelete) .where('id', organizationPostIdToDelete)
.del(); .del();
await trx.commit(); return res.status(200).json({success : true});
return res.status(200).json({success: true}); }
} else{
else {
return res.status(401).json({error : "Forbidden"}); return res.status(401).json({error : "Forbidden"});
} }
});
} }
catch (error) { catch (error) {
console.log(error); console.log(error);

View File

@ -7,6 +7,8 @@
callbacks: callbacks:
resolve(value) — if the job is finished successfully, with result value. resolve(value) — if the job is finished successfully, with result value.
reject(error) — if an error has occurred, error is the error object. reject(error) — if an error has occurred, error is the error object.
Remember that Promises are not intrensically asyncronous
*/ */
let promise = new Promise(function(resolve, reject) { let promise = new Promise(function(resolve, reject) {
setTimeout(() => resolve("done"), 500); setTimeout(() => resolve("done"), 500);