@@ -6,7 +6,7 @@ const debug = require('../debug.js').ACL
66const LegacyResourceMapper = require ( '../legacy-resource-mapper' )
77
88function allow ( mode ) {
9- return function allowHandler ( req , res , next ) {
9+ return async function allowHandler ( req , res , next ) {
1010 const ldp = req . app . locals . ldp || { }
1111 if ( ! ldp . webid ) {
1212 return next ( )
@@ -27,37 +27,41 @@ function allow (mode) {
2727 : req . path
2828
2929 // Check whether the resource exists
30- ldp . exists ( req . hostname , reqPath , ( err , ret ) => {
31- // Ensure directories always end in a slash
32- const stat = err ? null : ret . stream
33- if ( ! reqPath . endsWith ( '/' ) && stat && stat . isDirectory ( ) ) {
34- reqPath += '/'
35- }
30+ let stat
31+ try {
32+ const ret = await ldp . exists ( req . hostname , reqPath )
33+ stat = ret . stream
34+ } catch ( err ) {
35+ stat = null
36+ }
3637
37- // Obtain and store the ACL of the requested resource
38- req . acl = new ACL ( rootUrl + reqPath , {
39- origin : req . get ( 'origin' ) ,
40- host : req . protocol + '://' + req . get ( 'host' ) ,
41- fetch : fetchFromLdp ( mapper , ldp ) ,
42- fetchGraph : ( uri , options ) => {
43- // first try loading from local fs
44- return ldp . getGraph ( uri , options . contentType )
45- // failing that, fetch remote graph
46- . catch ( ( ) => ldp . fetchGraph ( uri , options ) )
47- } ,
48- suffix : ldp . suffixAcl ,
49- strictOrigin : ldp . strictOrigin ,
50- originsAllowed : ldp . originsAllowed
51- } )
38+ // Ensure directories always end in a slash
39+ if ( ! reqPath . endsWith ( '/' ) && stat && stat . isDirectory ( ) ) {
40+ reqPath += '/'
41+ }
5242
53- // Ensure the user has the required permission
54- const userId = req . session . userId
55- req . acl . can ( userId , mode )
56- . then ( ( ) => next ( ) , err => {
57- debug ( `${ mode } access denied to ${ userId || '(none)' } ` )
58- next ( err )
59- } )
43+ // Obtain and store the ACL of the requested resource
44+ req . acl = new ACL ( rootUrl + reqPath , {
45+ origin : req . get ( 'origin' ) ,
46+ host : req . protocol + '://' + req . get ( 'host' ) ,
47+ fetch : fetchFromLdp ( mapper , ldp ) ,
48+ fetchGraph : ( uri , options ) => {
49+ // first try loading from local fs
50+ return ldp . getGraph ( uri , options . contentType )
51+ // failing that, fetch remote graph
52+ . catch ( ( ) => ldp . fetchGraph ( uri , options ) )
53+ } ,
54+ suffix : ldp . suffixAcl ,
55+ strictOrigin : ldp . strictOrigin
6056 } )
57+
58+ // Ensure the user has the required permission
59+ const userId = req . session . userId
60+ req . acl . can ( userId , mode )
61+ . then ( ( ) => next ( ) , err => {
62+ debug ( `${ mode } access denied to ${ userId || '(none)' } ` )
63+ next ( err )
64+ } )
6165 }
6266}
6367
0 commit comments