Skip to content

Commit 6b6257f

Browse files
committed
update DELETE
1 parent 667b3a5 commit 6b6257f

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

lib/acl-checker.js

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { dirname } = require('path')
55
const rdf = require('rdflib')
66
const debug = require('./debug').ACL
77
// const debugCache = require('./debug').cache
8+
const debugAccounts = require('./debug').accounts
89
const HTTPError = require('./http-error')
910
const aclCheck = require('@solid/acl-check')
1011
const { URL } = require('url')
@@ -55,7 +56,7 @@ class ACLChecker {
5556
}
5657
this.messagesCached[cacheKey] = this.messagesCached[cacheKey] || []
5758

58-
const acl = await this.getNearestACL().catch(err => {
59+
const acl = await this.getNearestACL(method).catch(err => {
5960
this.messagesCached[cacheKey].push(new HTTPError(err.status || 500, err.message || err))
6061
})
6162
if (!acl) {
@@ -77,21 +78,7 @@ class ACLChecker {
7778
parentResource = resource
7879
if (!thisResource.endsWith('/')) parentResource = rdf.sym(ACLChecker.getDirectory(thisResource))
7980
}
80-
/* let resource = rdf.sym(this.resource)
81-
if (this.resource.endsWith('/' + this.suffix)) {
82-
resource = rdf.sym(ACLChecker.getDirectory(this.resource))
83-
}
84-
// If this is an ACL, Control mode must be present for any operations
85-
if (this.isAcl(this.resource)) {
86-
mode = 'Control'
87-
resource = rdf.sym(this.resource.substring(0, this.resource.length - this.suffix.length))
88-
} */
89-
// If the slug is an acl, reject
90-
/* if (this.isAcl(this.slug)) {
91-
this.aclCached[cacheKey] = Promise.resolve(false)
92-
return this.aclCached[cacheKey]
93-
} */
94-
let directory = acl.isContainer ? rdf.sym(ACLChecker.getDirectory(acl.docAcl)) : null
81+
const directory = acl.isContainer ? rdf.sym(ACLChecker.getDirectory(acl.docAcl)) : null
9582
const aclFile = rdf.sym(acl.docAcl)
9683
const aclGraph = acl.docGraph
9784
const agent = user ? rdf.sym(user) : null
@@ -116,12 +103,14 @@ class ACLChecker {
116103
accessDenied = accessResult ? false : accessDenied || accessDeniedAccessTo
117104
// debugCache('accessDenied result ' + accessDenied)
118105
}
119-
function accessDeniedForAccessToParent (mode) {
106+
function accessdeniedFromParent (modes) {
120107
const parentAclDirectory = ACLChecker.getDirectory(acl.parentAcl)
121108
const parentDirectory = parentResource === parentAclDirectory ? null : rdf.sym(parentAclDirectory)
122-
const accessDeniedAccessTo = aclCheck.accessDenied(acl.parentGraph, parentResource, parentDirectory, rdf.sym(acl.parentAcl), agent, [ACL(mode)], agentOrigin, trustedOrigins, originTrustedModes)
123-
const accessResult = !accessDenied && !accessDeniedAccessTo
124-
accessDenied = accessResult ? false : accessDenied || accessDeniedAccessTo
109+
const deniedParent = Promise.all(modes
110+
.map(mode => aclCheck.accessDenied(acl.parentGraph, parentResource, parentDirectory, rdf.sym(acl.parentAcl), agent, [ACL(mode)], agentOrigin, trustedOrigins, originTrustedModes)))
111+
const accessDeniedParent = deniedParent.reduce((memo, deniedParent) => memo && !deniedParent, true)
112+
const accessResult = !accessDenied && !accessDeniedParent
113+
accessDenied = accessResult ? false : accessDenied || accessDeniedParent
125114
// debugCache('accessDenied result ' + accessDenied)
126115
}
127116
// For create and update HTTP methods
@@ -137,11 +126,11 @@ class ACLChecker {
137126
if ((method === 'DELETE')) {
138127
// if resource and acl have same parent container,
139128
// then accessTo Write from parent is required
140-
if (!directory && aclFile.value.endsWith('/.acl')) directory = rdf.sym(dirname(aclFile.value) + '/')
129+
if (!directory && aclFile.value.endsWith('/.acl')) accessdeniedFromParent(['Read', 'Write']) // directory = rdf.sym(dirname(aclFile.value) + '/')
141130
if ((directory && directory.value === dirname(aclFile.value) + '/')) {
142131
accessDeniedForAccessTo('Write')
143132
} else {
144-
accessDeniedForAccessToParent('Write')
133+
accessdeniedFromParent(['Write'])
145134
}
146135
}
147136

@@ -169,19 +158,19 @@ class ACLChecker {
169158
}
170159

171160
// Gets the ACL that applies to the resource
172-
async getNearestACL () {
161+
async getNearestACL (method) {
173162
const { resource } = this
174163
let isContainer = false
175164
const possibleACLs = this.getPossibleACLs()
176165
const acls = [...possibleACLs]
177166
let returnAcl = null
178-
// let returnParentAcl = null
167+
let returnParentAcl = null
179168
let parentAcl = null
180169
let parentGraph = null
181170
let docAcl = null
182171
let docGraph = null
183172
// while (possibleACLs.length > 0 && !returnParentAcl) {
184-
while (possibleACLs.length > 0 && !returnAcl) {
173+
while (possibleACLs.length > 0 && !returnParentAcl) { // alain returnParentAcl
185174
const acl = possibleACLs.shift()
186175
let graph
187176
try {
@@ -205,30 +194,43 @@ class ACLChecker {
205194
parentGraph = graph // alain
206195
returnParentAcl = true
207196
} */
197+
if (method !== 'DELETE') returnParentAcl = true
208198
} else {
209199
parentAcl = acl
210200
parentGraph = graph
211-
returnAcl = true
201+
returnParentAcl = true
212202
}
213-
// returnParentAcl = true
214203

215204
returnAcl = { docAcl, docGraph, isContainer, parentAcl, parentGraph }
216205
}
217206
if (!returnAcl) {
218207
throw new HTTPError(500, `No ACL found for ${resource}, searched in \n- ${acls.join('\n- ')}`)
219208
}
209+
if (!parentAcl) { // alain is it needed
210+
returnAcl.parentAcl = returnAcl.docAcl
211+
returnAcl.parentGraph = returnAcl.docGraph
212+
}
220213
const groupNodes = returnAcl.docGraph.statementsMatching(null, ACL('agentGroup'), null)
221214
const groupUrls = groupNodes.map(node => node.object.value.split('#')[0])
222215
await Promise.all(groupUrls.map(async groupUrl => {
223216
try {
224217
const docGraph = await this.fetch(groupUrl, returnAcl.docGraph)
225218
this.requests[groupUrl] = this.requests[groupUrl] || docGraph
219+
// debugAccounts(this.requests[groupUrl].statementsMatching())
226220
} catch (e) {} // failed to fetch groupUrl
227221
}))
228-
if (!parentAcl) { // alain is it needed
229-
returnAcl.parentAcl = docAcl
230-
returnAcl.parentGraph = docGraph
231-
}
222+
/* groupNodes = returnAcl.parentGraph.statementsMatching(null, ACL('agentGroup'), null)
223+
groupUrls = groupNodes.map(node => node.object.value.split('#')[0])
224+
await Promise.all(groupUrls.map(async groupUrl => {
225+
try {
226+
const docGraph = await this.fetch(groupUrl, returnAcl.parentGraph)
227+
this.requests[groupUrl] = this.requests[groupUrl] || docGraph
228+
} catch (e) {} // failed to fetch groupUrl
229+
})) */
230+
/* returnAcl.parentAcl = returnAcl.docAcl
231+
returnAcl.parentGraph = returnAcl.docGraph
232+
*/
233+
debugAccounts('ALAIN returnACl ' + '\ndocAcl ' + returnAcl.docAcl + '\nparentAcl ' + returnAcl.parentAcl)
232234
return returnAcl
233235
}
234236

0 commit comments

Comments
 (0)