From 9d990816f8739068df8314efe7951bf209f49cb1 Mon Sep 17 00:00:00 2001 From: Chris Myers Date: Sun, 22 Dec 2024 19:09:50 -0700 Subject: [PATCH 1/3] Fix issue with remove owner having the wrong link --- lib/actions/removeOwnedBy.js | 44 ++++++++++++++++++------------- templates/layouts/identified.jade | 2 +- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/lib/actions/removeOwnedBy.js b/lib/actions/removeOwnedBy.js index 030fc79d9..9795a27cd 100644 --- a/lib/actions/removeOwnedBy.js +++ b/lib/actions/removeOwnedBy.js @@ -10,16 +10,15 @@ module.exports = function (req, res) { return getOwnedBy(uri, graphUri).then((ownedBy) => { if (ownedBy.indexOf(config.get('databasePrefix') + 'user/' + req.user.username) === -1) { - res.status(401).send('not authorized to remove an owner') + return res.status(401).send('Not authorized to remove an owner') } - return retrieveUris(uri, graphUri).then(uris => { + return retrieveUris(uri, graphUri).then((uris) => { let chunks = [] let offset = config.get('resolveBatch') for (let i = 0; i < uris.length; i += offset) { let end = i + offset < uris.length ? i + offset : uris.length - chunks.push(uris.slice(i, end)) } @@ -30,22 +29,29 @@ module.exports = function (req, res) { console.log(sharedRemovalQuery) - return sparql.updateQuery(sharedRemovalQuery, req.body.userUri).then(result => { - return Promise.all(chunks.map(chunk => { - let uris = chunk.map(uri => { - return '<' + uri + '> sbh:ownedBy <' + req.body.userUri + '>' - }).join(' . \n') - - const updateQuery = loadTemplate('./sparql/RemoveOwnedBy.sparql', { - uris: uris - }) - console.log(updateQuery) - - return sparql.updateQuery(updateQuery, graphUri).then(() => { - res.redirect(share) - }) - })) - }) + return sparql.updateQuery(sharedRemovalQuery, req.body.userUri) + .then(() => { + return Promise.all( + chunks.map((chunk) => { + let uris = chunk + .map((uri) => `<${uri}> sbh:ownedBy <${req.body.userUri}>`) + .join(' . \n') + + const updateQuery = loadTemplate('./sparql/RemoveOwnedBy.sparql', { uris }) + console.log(updateQuery) + + return sparql.updateQuery(updateQuery, graphUri) + }) + ) + }) + .then(() => { + // Redirect only once after all promises resolve + res.redirect(share) + }) + .catch((err) => { + console.error('Error:', err) + res.status(500).send(`Error removing ownership: ${err.message}`) + }) }) }) } diff --git a/templates/layouts/identified.jade b/templates/layouts/identified.jade index 4aa0c8072..8f6941a76 100644 --- a/templates/layouts/identified.jade +++ b/templates/layouts/identified.jade @@ -225,7 +225,7 @@ block content span.fa.fa-search if(meta.canEdit && annotation.removeOwner) form.form-inline(style="display: inline" method='POST', action=annotation.removeOwner) - input(type='hidden' name='userUri' value=config.databasePrefix + 'user/' + annotation.value) + input(type='hidden' name='userUri' value=config.databasePrefix + annotation.value) button.fakelink(type='submit') |   span.fa.fa-trash From 1a384bea07c3d1cf9604b443d5933a1f587b5e69 Mon Sep 17 00:00:00 2001 From: Chris Myers Date: Mon, 23 Dec 2024 13:41:48 -0700 Subject: [PATCH 2/3] Fix issues with adding and removing owners --- lib/actions/addOwnedBy.js | 18 +++++++++++++++++- lib/actions/removeOwnedBy.js | 8 ++++++-- lib/views/addOwner.js | 26 ++++++++++++++++---------- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/lib/actions/addOwnedBy.js b/lib/actions/addOwnedBy.js index b4c75671c..47024dceb 100644 --- a/lib/actions/addOwnedBy.js +++ b/lib/actions/addOwnedBy.js @@ -4,10 +4,20 @@ const config = require('../config') const getOwnedBy = require('../query/ownedBy') const retrieveUris = require('../retrieveUris') const getUrisFromReq = require('../getUrisFromReq') +const db = require('../db') -module.exports = function (req, res) { +module.exports = async function (req, res) { const userUri = req.body.user + var userId = '' + if (req.body.user.lastIndexOf('/') >= 0) { + userId = req.body.user.substring(req.body.user.lastIndexOf('/') + 1) + } + + const user = await db.model.User.findOne({ + where: db.sequelize.or({ email: userId }, { username: userId }) + }) + const { graphUri, uri } = getUrisFromReq(req, res) const sharedAdditionQuery = loadTemplate('./sparql/AddToSharedCollection.sparql', { @@ -23,6 +33,12 @@ module.exports = function (req, res) { }) } + if (!user) { + return new Promise(function (resolve, reject) { + reject(new Error('user ' + userId + ' not recognized')) + }) + } + return sparql.updateQuery(sharedAdditionQuery, userUri).then(() => { return retrieveUris(uri, graphUri) }).then((uris) => { diff --git a/lib/actions/removeOwnedBy.js b/lib/actions/removeOwnedBy.js index 9795a27cd..253fa4ecc 100644 --- a/lib/actions/removeOwnedBy.js +++ b/lib/actions/removeOwnedBy.js @@ -6,7 +6,7 @@ const loadTemplate = require('../loadTemplate') const getOwnedBy = require('../query/ownedBy') module.exports = function (req, res) { - const { graphUri, uri, share } = getUrisFromReq(req, res) + const { graphUri, uri, url } = getUrisFromReq(req, res) return getOwnedBy(uri, graphUri).then((ownedBy) => { if (ownedBy.indexOf(config.get('databasePrefix') + 'user/' + req.user.username) === -1) { @@ -46,7 +46,11 @@ module.exports = function (req, res) { }) .then(() => { // Redirect only once after all promises resolve - res.redirect(share) + if (!req.accepts('text/html')) { + res.status(200).send('Success') + } else { + res.redirect(url) + } }) .catch((err) => { console.error('Error:', err) diff --git a/lib/views/addOwner.js b/lib/views/addOwner.js index 51ef4e2c6..9508582d6 100644 --- a/lib/views/addOwner.js +++ b/lib/views/addOwner.js @@ -14,9 +14,7 @@ module.exports = function (req, res) { function view (req, res) { const { - graphUri, - uri, - designId + uri } = getUrisFromReq(req, res) db.model.User.findAll().then(users => { @@ -33,14 +31,22 @@ function view (req, res) { function post (req, res) { addOwnedBy(req, res).then(() => { - res.redirect(req.originalUrl.replace('/addOwner', '')) + if (!req.accepts('text/html')) { + res.status(200).send('Success') + } else { + res.redirect(req.originalUrl.replace('/addOwner', '')) + } }, function (err) { - const locals = { - config: config.get(), - section: 'errors', - user: req.user, - errors: [ err ] + if (!req.accepts('text/html')) { + res.status(400).send(err.message) + } else { + const locals = { + config: config.get(), + section: 'errors', + user: req.user, + errors: [ err ] + } + res.send(pug.renderFile('templates/views/errors/errors.jade', locals)) } - res.send(pug.renderFile('templates/views/errors/errors.jade', locals)) }) } From a7357c48cb8f3142461726cd7b341920a8d78f8b Mon Sep 17 00:00:00 2001 From: Chris Myers Date: Thu, 16 Jan 2025 10:57:09 -0700 Subject: [PATCH 3/3] Fix issue not removing correctly from shared collection --- sparql/RemoveFromSharedCollection.sparql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sparql/RemoveFromSharedCollection.sparql b/sparql/RemoveFromSharedCollection.sparql index 7f5585141..598b4ea04 100644 --- a/sparql/RemoveFromSharedCollection.sparql +++ b/sparql/RemoveFromSharedCollection.sparql @@ -2,7 +2,7 @@ PREFIX dcterms: PREFIX sbh: DELETE DATA { - <$uri> sbh:ownedBy <$userUri> . + <$userUri> sbh:canView <$uri> . }