Skip to content

Commit 62cdcbc

Browse files
committed
Simplify ACL path algorithm.
1 parent 3861c5b commit 62cdcbc

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

lib/acl-checker.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
'use strict'
22

3-
const path = require('path')
43
const PermissionSet = require('solid-permissions').PermissionSet
54
const rdf = require('rdflib')
6-
const url = require('url')
75
const debug = require('./debug').ACL
86
const HTTPError = require('./http-error')
97

@@ -74,28 +72,21 @@ class ACLChecker {
7472

7573
// Gets all possible ACL paths that apply to the resource
7674
getPossibleACLs () {
77-
let uri = this.resource
78-
const suffix = this.suffix
79-
const first = uri.endsWith(suffix) ? uri : uri + suffix
80-
const urls = [first]
81-
const parsedUri = url.parse(uri)
82-
const baseUrl = (parsedUri.protocol ? parsedUri.protocol + '//' : '') +
83-
(parsedUri.host || '')
84-
if (baseUrl + '/' === uri) {
85-
return urls
86-
}
75+
// Obtain the resource URI and the length of its base
76+
let { resource: uri, suffix } = this
77+
const [ { length: base } ] = uri.match(/^[^:]+:\/*[^/]+/)
8778

88-
let times = parsedUri.pathname.split('/').length
89-
// TODO: improve temporary solution to stop recursive path walking above root
90-
if (parsedUri.pathname.endsWith('/')) {
91-
times--
79+
// If the URI points to a file, append the file's ACL
80+
const possibleAcls = []
81+
if (!uri.endsWith('/')) {
82+
possibleAcls.push(uri.endsWith(suffix) ? uri : uri + suffix)
9283
}
9384

94-
for (let i = 0; i < times - 1; i++) {
95-
uri = path.dirname(uri)
96-
urls.push(uri + (uri[uri.length - 1] === '/' ? suffix : '/' + suffix))
85+
// Append the ACLs of all parent directories
86+
for (let i = lastSlash(uri); i >= base; i = lastSlash(uri, i - 1)) {
87+
possibleAcls.push(uri.substr(0, i + 1) + suffix)
9788
}
98-
return urls
89+
return possibleAcls
9990
}
10091

10192
// Tests whether the permissions allow a given operation
@@ -145,5 +136,10 @@ class ACLChecker {
145136
}
146137
}
147138

139+
// Returns the index of the last slash before the given position
140+
function lastSlash (string, pos = string.length) {
141+
return string.lastIndexOf('/', pos)
142+
}
143+
148144
module.exports = ACLChecker
149145
module.exports.DEFAULT_ACL_SUFFIX = DEFAULT_ACL_SUFFIX

0 commit comments

Comments
 (0)