Skip to content

Commit d7a429f

Browse files
RubenVerborghdmitrizagidulin
authored andcommitted
Simplify ACL path algorithm.
1 parent 0a58ac5 commit d7a429f

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

@@ -73,28 +71,21 @@ class ACLChecker {
7371

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

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

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

10091
// Tests whether the permissions allow a given operation
@@ -144,5 +135,10 @@ class ACLChecker {
144135
}
145136
}
146137

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

0 commit comments

Comments
 (0)