Skip to content

Commit c9b1c0e

Browse files
committed
Move getNearestACL into separate method.
1 parent 3f5db03 commit c9b1c0e

File tree

1 file changed

+48
-46
lines changed

1 file changed

+48
-46
lines changed

lib/acl-checker.js

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,9 @@ class ACLChecker {
2424
mode = 'Control'
2525
}
2626

27-
// Find nearest ACL
28-
let accessType = 'accessTo'
29-
let nearestACL = Promise.reject()
30-
for (const acl of ACLChecker.possibleACLs(resource, this.suffix)) {
31-
nearestACL = nearestACL.catch(() => new Promise((resolve, reject) => {
32-
debug('Check if acl exist: ' + acl)
33-
this.fetch(acl, function (err, graph) {
34-
if (err || !graph || !graph.length) {
35-
if (err) debug(`Error reading ${acl}: ${err}`)
36-
accessType = 'defaultForNew'
37-
reject(err)
38-
} else {
39-
resolve({ acl, graph })
40-
}
41-
})
42-
}))
43-
}
44-
nearestACL = nearestACL.catch(() => {
45-
throw new Error('No ACL resource found')
46-
})
47-
48-
// Check the permissions within the ACL
49-
return nearestACL.then(({ acl, graph }) =>
27+
// Check the permissions within the nearest ACL
28+
return this.getNearestACL(resource)
29+
.then(({ acl, graph, accessType }) =>
5030
this.checkAccess(
5131
graph, // The ACL graph
5232
user, // The webId of the user
@@ -70,6 +50,51 @@ class ACLChecker {
7050
})
7151
}
7252

53+
// Gets the ACL that applies to the resource
54+
getNearestACL (uri) {
55+
let accessType = 'accessTo'
56+
let nearestACL = Promise.reject()
57+
for (const acl of this.getPossibleACLs(uri, this.suffix)) {
58+
nearestACL = nearestACL.catch(() => new Promise((resolve, reject) => {
59+
this.debug(`Check if ACL exists: ${acl}`)
60+
this.fetch(acl, (err, graph) => {
61+
if (err || !graph || !graph.length) {
62+
if (err) this.debug(`Error reading ${acl}: ${err}`)
63+
accessType = 'defaultForNew'
64+
reject(err)
65+
} else {
66+
resolve({ acl, graph, accessType })
67+
}
68+
})
69+
}))
70+
}
71+
return nearestACL.catch(e => { throw new Error('No ACL resource found') })
72+
}
73+
74+
// Get all possible ACL paths that apply to the resource
75+
getPossibleACLs (uri, suffix) {
76+
var first = uri.endsWith(suffix) ? uri : uri + suffix
77+
var urls = [first]
78+
var parsedUri = url.parse(uri)
79+
var baseUrl = (parsedUri.protocol ? parsedUri.protocol + '//' : '') +
80+
(parsedUri.host || '')
81+
if (baseUrl + '/' === uri) {
82+
return urls
83+
}
84+
85+
var times = parsedUri.pathname.split('/').length
86+
// TODO: improve temporary solution to stop recursive path walking above root
87+
if (parsedUri.pathname.endsWith('/')) {
88+
times--
89+
}
90+
91+
for (var i = 0; i < times - 1; i++) {
92+
uri = path.dirname(uri)
93+
urls.push(uri + (uri[uri.length - 1] === '/' ? suffix : '/' + suffix))
94+
}
95+
return urls
96+
}
97+
7398
/**
7499
* Tests whether a graph (parsed .acl resource) allows a given operation
75100
* for a given user. Calls the provided callback with `null` if the user
@@ -136,29 +161,6 @@ class ACLChecker {
136161
return false
137162
}
138163
}
139-
140-
static possibleACLs (uri, suffix) {
141-
var first = uri.endsWith(suffix) ? uri : uri + suffix
142-
var urls = [first]
143-
var parsedUri = url.parse(uri)
144-
var baseUrl = (parsedUri.protocol ? parsedUri.protocol + '//' : '') +
145-
(parsedUri.host || '')
146-
if (baseUrl + '/' === uri) {
147-
return urls
148-
}
149-
150-
var times = parsedUri.pathname.split('/').length
151-
// TODO: improve temporary solution to stop recursive path walking above root
152-
if (parsedUri.pathname.endsWith('/')) {
153-
times--
154-
}
155-
156-
for (var i = 0; i < times - 1; i++) {
157-
uri = path.dirname(uri)
158-
urls.push(uri + (uri[uri.length - 1] === '/' ? suffix : '/' + suffix))
159-
}
160-
return urls
161-
}
162164
}
163165

164166
module.exports = ACLChecker

0 commit comments

Comments
 (0)