11module . exports = allow
22
33const ACL = require ( '../acl-checker' )
4- const $rdf = require ( 'rdflib' )
5- const utils = require ( '../utils' )
64const debug = require ( '../debug.js' ) . ACL
7- const LegacyResourceMapper = require ( '../legacy-resource-mapper' )
85
96function allow ( mode ) {
10- return function allowHandler ( req , res , next ) {
7+ return async function allowHandler ( req , res , next ) {
118 const ldp = req . app . locals . ldp || { }
129 if ( ! ldp . webid ) {
1310 return next ( )
1411 }
1512
1613 // Set up URL to filesystem mapping
17- const rootUrl = utils . getBaseUri ( req )
18- const mapper = new LegacyResourceMapper ( {
19- rootUrl,
20- rootPath : ldp . root ,
21- includeHost : ldp . multiuser
22- } )
14+ const rootUrl = ldp . resourceMapper . resolveUrl ( req . hostname )
2315
2416 // Determine the actual path of the request
2517 // (This is used as an ugly hack to check the ACL status of other resources.)
@@ -28,37 +20,41 @@ function allow (mode) {
2820 : req . path
2921
3022 // Check whether the resource exists
31- ldp . exists ( req . hostname , reqPath , ( err , ret ) => {
32- // Ensure directories always end in a slash
33- const stat = err ? null : ret . stream
34- if ( ! reqPath . endsWith ( '/' ) && stat && stat . isDirectory ( ) ) {
35- reqPath += '/'
36- }
23+ let stat
24+ try {
25+ const ret = await ldp . exists ( req . hostname , reqPath )
26+ stat = ret . stream
27+ } catch ( err ) {
28+ stat = null
29+ }
3730
38- // Obtain and store the ACL of the requested resource
39- req . acl = new ACL ( rootUrl + reqPath , {
40- origin : req . get ( 'origin' ) ,
41- host : req . protocol + '://' + req . get ( 'host' ) ,
42- fetch : fetchFromLdp ( mapper , ldp ) ,
43- fetchGraph : ( uri , options ) => {
44- // first try loading from local fs
45- return ldp . getGraph ( uri , options . contentType )
46- // failing that, fetch remote graph
47- . catch ( ( ) => ldp . fetchGraph ( uri , options ) )
48- } ,
49- suffix : ldp . suffixAcl ,
50- strictOrigin : ldp . strictOrigin ,
51- originsAllowed : ldp . originsAllowed
52- } )
31+ // Ensure directories always end in a slash
32+ if ( ! reqPath . endsWith ( '/' ) && stat && stat . isDirectory ( ) ) {
33+ reqPath += '/'
34+ }
5335
54- // Ensure the user has the required permission
55- const userId = req . session . userId
56- req . acl . can ( userId , mode )
57- . then ( ( ) => next ( ) , err => {
58- debug ( `${ mode } access denied to ${ userId || '(none)' } ` )
59- next ( err )
60- } )
36+ // Obtain and store the ACL of the requested resource
37+ req . acl = new ACL ( rootUrl + reqPath , {
38+ origin : req . get ( 'origin' ) ,
39+ host : req . protocol + '://' + req . get ( 'host' ) ,
40+ fetch : fetchFromLdp ( ldp . resourceMapper , ldp ) ,
41+ fetchGraph : ( uri , options ) => {
42+ // first try loading from local fs
43+ return ldp . getGraph ( uri , options . contentType )
44+ // failing that, fetch remote graph
45+ . catch ( ( ) => ldp . fetchGraph ( uri , options ) )
46+ } ,
47+ suffix : ldp . suffixAcl ,
48+ strictOrigin : ldp . strictOrigin
6149 } )
50+
51+ // Ensure the user has the required permission
52+ const userId = req . session . userId
53+ req . acl . can ( userId , mode )
54+ . then ( ( ) => next ( ) , err => {
55+ debug ( `${ mode } access denied to ${ userId || '(none)' } ` )
56+ next ( err )
57+ } )
6258 }
6359}
6460
@@ -72,19 +68,6 @@ function allow (mode) {
7268 */
7369function fetchFromLdp ( mapper , ldp ) {
7470 return function fetch ( url , callback ) {
75- // Convert the URL into a filename
76- mapper . mapUrlToFile ( { url } )
77- // Read the file from disk
78- . then ( ( { path } ) => new Promise ( ( resolve , reject ) => {
79- ldp . readFile ( path , ( e , c ) => e ? reject ( e ) : resolve ( c ) )
80- } ) )
81- // Parse the file as Turtle
82- . then ( body => {
83- const graph = $rdf . graph ( )
84- $rdf . parse ( body , graph , url , 'text/turtle' )
85- return graph
86- } )
87- // Return the ACL graph
88- . then ( graph => callback ( null , graph ) , callback )
71+ ldp . getGraph ( url ) . then ( g => callback ( null , g ) , callback )
8972 }
9073}
0 commit comments