|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | | -const path = require('path') |
4 | 3 | const PermissionSet = require('solid-permissions').PermissionSet |
5 | 4 | const rdf = require('rdflib') |
6 | | -const url = require('url') |
7 | 5 | const debug = require('./debug').ACL |
8 | 6 | const HTTPError = require('./http-error') |
9 | 7 |
|
@@ -74,28 +72,21 @@ class ACLChecker { |
74 | 72 |
|
75 | 73 | // Gets all possible ACL paths that apply to the resource |
76 | 74 | 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(/^[^:]+:\/*[^/]+/) |
87 | 78 |
|
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) |
92 | 83 | } |
93 | 84 |
|
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) |
97 | 88 | } |
98 | | - return urls |
| 89 | + return possibleAcls |
99 | 90 | } |
100 | 91 |
|
101 | 92 | // Tests whether the permissions allow a given operation |
@@ -145,5 +136,10 @@ class ACLChecker { |
145 | 136 | } |
146 | 137 | } |
147 | 138 |
|
| 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 | + |
148 | 144 | module.exports = ACLChecker |
149 | 145 | module.exports.DEFAULT_ACL_SUFFIX = DEFAULT_ACL_SUFFIX |
0 commit comments