Skip to content

Commit 23c5f75

Browse files
authored
Merge pull request #1604 from SynBioHub/fixRemoveOwner
Fix issue with remove owner having the wrong link
2 parents 544350e + 8245e0b commit 23c5f75

File tree

5 files changed

+65
-33
lines changed

5 files changed

+65
-33
lines changed

lib/actions/addOwnedBy.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,20 @@ const config = require('../config')
44
const getOwnedBy = require('../query/ownedBy')
55
const retrieveUris = require('../retrieveUris')
66
const getUrisFromReq = require('../getUrisFromReq')
7+
const db = require('../db')
78

8-
module.exports = function (req, res) {
9+
module.exports = async function (req, res) {
910
const userUri = req.body.user
1011

12+
var userId = ''
13+
if (req.body.user.lastIndexOf('/') >= 0) {
14+
userId = req.body.user.substring(req.body.user.lastIndexOf('/') + 1)
15+
}
16+
17+
const user = await db.model.User.findOne({
18+
where: db.sequelize.or({ email: userId }, { username: userId })
19+
})
20+
1121
const { graphUri, uri } = getUrisFromReq(req, res)
1222

1323
const sharedAdditionQuery = loadTemplate('./sparql/AddToSharedCollection.sparql', {
@@ -23,6 +33,12 @@ module.exports = function (req, res) {
2333
})
2434
}
2535

36+
if (!user) {
37+
return new Promise(function (resolve, reject) {
38+
reject(new Error('user ' + userId + ' not recognized'))
39+
})
40+
}
41+
2642
return sparql.updateQuery(sharedAdditionQuery, userUri).then(() => {
2743
return retrieveUris(uri, graphUri)
2844
}).then((uris) => {

lib/actions/removeOwnedBy.js

+30-20
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,19 @@ const loadTemplate = require('../loadTemplate')
66
const getOwnedBy = require('../query/ownedBy')
77

88
module.exports = function (req, res) {
9-
const { graphUri, uri, share } = getUrisFromReq(req, res)
9+
const { graphUri, uri, url } = getUrisFromReq(req, res)
1010

1111
return getOwnedBy(uri, graphUri).then((ownedBy) => {
1212
if (ownedBy.indexOf(config.get('databasePrefix') + 'user/' + req.user.username) === -1) {
13-
res.status(401).send('not authorized to remove an owner')
13+
return res.status(401).send('Not authorized to remove an owner')
1414
}
1515

16-
return retrieveUris(uri, graphUri).then(uris => {
16+
return retrieveUris(uri, graphUri).then((uris) => {
1717
let chunks = []
1818
let offset = config.get('resolveBatch')
1919

2020
for (let i = 0; i < uris.length; i += offset) {
2121
let end = i + offset < uris.length ? i + offset : uris.length
22-
2322
chunks.push(uris.slice(i, end))
2423
}
2524

@@ -30,22 +29,33 @@ module.exports = function (req, res) {
3029

3130
console.log(sharedRemovalQuery)
3231

33-
return sparql.updateQuery(sharedRemovalQuery, req.body.userUri).then(result => {
34-
return Promise.all(chunks.map(chunk => {
35-
let uris = chunk.map(uri => {
36-
return '<' + uri + '> sbh:ownedBy <' + req.body.userUri + '>'
37-
}).join(' . \n')
38-
39-
const updateQuery = loadTemplate('./sparql/RemoveOwnedBy.sparql', {
40-
uris: uris
41-
})
42-
console.log(updateQuery)
43-
44-
return sparql.updateQuery(updateQuery, graphUri).then(() => {
45-
res.redirect(share)
46-
})
47-
}))
48-
})
32+
return sparql.updateQuery(sharedRemovalQuery, req.body.userUri)
33+
.then(() => {
34+
return Promise.all(
35+
chunks.map((chunk) => {
36+
let uris = chunk
37+
.map((uri) => `<${uri}> sbh:ownedBy <${req.body.userUri}>`)
38+
.join(' . \n')
39+
40+
const updateQuery = loadTemplate('./sparql/RemoveOwnedBy.sparql', { uris })
41+
console.log(updateQuery)
42+
43+
return sparql.updateQuery(updateQuery, graphUri)
44+
})
45+
)
46+
})
47+
.then(() => {
48+
// Redirect only once after all promises resolve
49+
if (!req.accepts('text/html')) {
50+
res.status(200).send('Success')
51+
} else {
52+
res.redirect(url)
53+
}
54+
})
55+
.catch((err) => {
56+
console.error('Error:', err)
57+
res.status(500).send(`Error removing ownership: ${err.message}`)
58+
})
4959
})
5060
})
5161
}

lib/views/addOwner.js

+16-10
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ module.exports = function (req, res) {
1414

1515
function view (req, res) {
1616
const {
17-
graphUri,
18-
uri,
19-
designId
17+
uri
2018
} = getUrisFromReq(req, res)
2119

2220
db.model.User.findAll().then(users => {
@@ -33,14 +31,22 @@ function view (req, res) {
3331

3432
function post (req, res) {
3533
addOwnedBy(req, res).then(() => {
36-
res.redirect(req.originalUrl.replace('/addOwner', ''))
34+
if (!req.accepts('text/html')) {
35+
res.status(200).send('Success')
36+
} else {
37+
res.redirect(req.originalUrl.replace('/addOwner', ''))
38+
}
3739
}, function (err) {
38-
const locals = {
39-
config: config.get(),
40-
section: 'errors',
41-
user: req.user,
42-
errors: [ err ]
40+
if (!req.accepts('text/html')) {
41+
res.status(400).send(err.message)
42+
} else {
43+
const locals = {
44+
config: config.get(),
45+
section: 'errors',
46+
user: req.user,
47+
errors: [ err ]
48+
}
49+
res.send(pug.renderFile('templates/views/errors/errors.jade', locals))
4350
}
44-
res.send(pug.renderFile('templates/views/errors/errors.jade', locals))
4551
})
4652
}

sparql/RemoveFromSharedCollection.sparql

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ PREFIX dcterms: <http://purl.org/dc/terms/>
22
PREFIX sbh: <http://wiki.synbiohub.org/wiki/Terms/synbiohub#>
33

44
DELETE DATA {
5-
<$uri> sbh:ownedBy <$userUri> .
5+
<$userUri> sbh:canView <$uri> .
66
}
77

88

templates/layouts/identified.jade

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ block content
225225
span.fa.fa-search
226226
if(meta.canEdit && annotation.removeOwner)
227227
form.form-inline(style="display: inline" method='POST', action=annotation.removeOwner)
228-
input(type='hidden' name='userUri' value=config.databasePrefix + 'user/' + annotation.value)
228+
input(type='hidden' name='userUri' value=config.databasePrefix + annotation.value)
229229
button.fakelink(type='submit')
230230
| &nbsp;
231231
span.fa.fa-trash

0 commit comments

Comments
 (0)