[Utils Service | Trunk]: Add upload method for stakeholders
git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-portal/trunk@59489 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
parent
2af5bb33e4
commit
d71054cca7
|
@ -90,7 +90,7 @@ app.post("/upload", upload.array("uploads[]", 12), function (req, res) {
|
||||||
|
|
||||||
app.post('/upload/stakeholder/:id', upload.single('photo'), (req, res) => {
|
app.post('/upload/stakeholder/:id', upload.single('photo'), (req, res) => {
|
||||||
sendFile(req, res, (result) => {
|
sendFile(req, res, (result) => {
|
||||||
return isAdminOrCurator(result.edu_person_entitlements);
|
return isMonitorCurator(result.edu_person_entitlements);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -116,32 +116,16 @@ app.get('/tiny', function (req, res) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.delete('/delete/stakeholder/:filename', function (req, res) {
|
||||||
|
deleteFileSend(req, res, (result) => {
|
||||||
|
return isMonitorCurator(result.edu_person_entitlements);
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
app.delete('/delete/:filename', function (req, res) {
|
app.delete('/delete/:filename', function (req, res) {
|
||||||
const token = req.cookies['AccessToken'];
|
deleteFileSend(req, res, (result) => {
|
||||||
if (!token) {
|
return result.sub.indexOf(req.params.filename.split('-')[0]) !== -1 || isAdminOrCurator(result.edu_person_entitlements);
|
||||||
res.status(401).send(getResponse(401, "Unauthorized"));
|
})
|
||||||
} else {
|
|
||||||
http.get(auth + token, function (resp) {
|
|
||||||
var responseString = "";
|
|
||||||
resp.on("data", function (data) {
|
|
||||||
responseString += data;
|
|
||||||
});
|
|
||||||
resp.on("end", function () {
|
|
||||||
var result = JSON.parse(responseString);
|
|
||||||
if (result.error) {
|
|
||||||
res.status(401).send(getResponse(401, "Unauthorized"));
|
|
||||||
} else {
|
|
||||||
// if user id is on filename or is Admin delete file else unauthorized.
|
|
||||||
if (result.sub.indexOf(req.params.filename.split('-')[0]) !== -1 || isAdminOrCurator(result.edu_person_entitlements)) {
|
|
||||||
deleteFile('./uploads/' + req.params.filename);
|
|
||||||
return res.status(200).send(getResponse(200, "File Deleted Successfully"));
|
|
||||||
} else {
|
|
||||||
res.status(401).send(getResponse(401, "Unauthorized"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const server = app.listen(8000, function () {
|
const server = app.listen(8000, function () {
|
||||||
|
@ -185,6 +169,34 @@ function sendFile(req, res, authorized) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function deleteFileSend(req, res, authorized) {
|
||||||
|
const token = req.cookies['AccessToken'];
|
||||||
|
if (!token) {
|
||||||
|
res.status(401).send(getResponse(401, "Unauthorized"));
|
||||||
|
} else {
|
||||||
|
http.get(auth + token, function (resp) {
|
||||||
|
var responseString = "";
|
||||||
|
resp.on("data", function (data) {
|
||||||
|
responseString += data;
|
||||||
|
});
|
||||||
|
resp.on("end", function () {
|
||||||
|
var result = JSON.parse(responseString);
|
||||||
|
if (result.error) {
|
||||||
|
res.status(401).send(getResponse(401, "Unauthorized"));
|
||||||
|
} else {
|
||||||
|
// if user id is on filename or is Admin delete file else unauthorized.
|
||||||
|
if (authorized) {
|
||||||
|
deleteFile('./uploads/' + req.params.filename);
|
||||||
|
return res.status(200).send(getResponse(200, "File Deleted Successfully"));
|
||||||
|
} else {
|
||||||
|
res.status(401).send(getResponse(401, "Unauthorized"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function getResponse(code, message) {
|
function getResponse(code, message) {
|
||||||
var response = {};
|
var response = {};
|
||||||
response["code"] = code;
|
response["code"] = code;
|
||||||
|
@ -208,6 +220,20 @@ function deleteFile(filepath) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isMonitorCurator(roles) {
|
||||||
|
var isAdmin = false;
|
||||||
|
var isCurator = false;
|
||||||
|
for (var i = 0; i < roles.length; i++) {
|
||||||
|
if (roles[i] === 'urn:geant:openaire.eu:group:Portal+Administrator#aai.openaire.eu') {
|
||||||
|
isAdmin = true;
|
||||||
|
}
|
||||||
|
if (roles[i] === 'urn:geant:openaire.eu:group:Curator+-+Funder#aai.openaire.eu') {
|
||||||
|
isCurator = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return isAdmin || isCurator;
|
||||||
|
}
|
||||||
|
|
||||||
function isAdminOrCurator(roles) {
|
function isAdminOrCurator(roles) {
|
||||||
var isAdmin = false;
|
var isAdmin = false;
|
||||||
var isCurator = false;
|
var isCurator = false;
|
||||||
|
|
Loading…
Reference in New Issue