11module . exports = allow
22
33const $rdf = require ( 'rdflib' )
4+ const path = require ( 'path' )
45const ACL = require ( '../acl-checker' )
56const debug = require ( '../debug.js' ) . ACL
67const fs = require ( 'fs' )
78const { promisify } = require ( 'util' )
89const HTTPError = require ( '../http-error' )
910
10- function allow ( mode ) {
11+ function allow ( mode , checkPermissionsForDirectory ) {
1112 return async function allowHandler ( req , res , next ) {
1213 const ldp = req . app . locals . ldp || { }
1314 if ( ! ldp . webid ) {
@@ -19,26 +20,31 @@ function allow (mode) {
1920
2021 // Determine the actual path of the request
2122 // (This is used as an ugly hack to check the ACL status of other resources.)
22- let reqPath = res && res . locals && res . locals . path
23+ let resourcePath = res && res . locals && res . locals . path
2324 ? res . locals . path
2425 : req . path
2526
27+ // Check permissions of the directory instead of the file itself.
28+ if ( checkPermissionsForDirectory ) {
29+ resourcePath = path . dirname ( resourcePath )
30+ }
31+
2632 // Check whether the resource exists
2733 let stat
2834 try {
29- const ret = await ldp . exists ( req . hostname , reqPath )
35+ const ret = await ldp . exists ( req . hostname , resourcePath )
3036 stat = ret . stream
3137 } catch ( err ) {
3238 stat = null
3339 }
3440
3541 // Ensure directories always end in a slash
36- if ( ! reqPath . endsWith ( '/' ) && stat && stat . isDirectory ( ) ) {
37- reqPath += '/'
42+ if ( ! resourcePath . endsWith ( '/' ) && stat && stat . isDirectory ( ) ) {
43+ resourcePath += '/'
3844 }
3945
4046 // Obtain and store the ACL of the requested resource
41- req . acl = new ACL ( rootUrl + reqPath , {
47+ req . acl = new ACL ( rootUrl + resourcePath , {
4248 agentOrigin : req . get ( 'origin' ) ,
4349 // host: req.get('host'),
4450 fetch : fetchFromLdp ( ldp . resourceMapper ) ,
0 commit comments