11module . exports = allow
22
3- const $rdf = require ( 'rdflib' )
43const path = require ( 'path' )
54const ACL = require ( '../acl-checker' )
65const debug = require ( '../debug.js' ) . ACL
7- const fs = require ( 'fs' )
8- const { promisify } = require ( 'util' )
9- const HTTPError = require ( '../http-error' )
106
117function allow ( mode , checkPermissionsForDirectory ) {
128 return async function allowHandler ( req , res , next ) {
@@ -48,20 +44,7 @@ function allow (mode, checkPermissionsForDirectory) {
4844 trustedOrigins . push ( ldp . serverUri )
4945 }
5046 // Obtain and store the ACL of the requested resource
51- req . acl = new ACL ( rootUrl + resourcePath , {
52- agentOrigin : req . get ( 'origin' ) ,
53- // host: req.get('host'),
54- fetch : fetchFromLdp ( ldp . resourceMapper ) ,
55- fetchGraph : ( uri , options ) => {
56- // first try loading from local fs
57- return ldp . getGraph ( uri , options . contentType )
58- // failing that, fetch remote graph
59- . catch ( ( ) => ldp . fetchGraph ( uri , options ) )
60- } ,
61- suffix : ldp . suffixAcl ,
62- strictOrigin : ldp . strictOrigin ,
63- trustedOrigins : trustedOrigins
64- } )
47+ req . acl = ACL . createFromLDPAndRequest ( rootUrl + resourcePath , ldp , req )
6548
6649 // Ensure the user has the required permission
6750 const userId = req . session . userId
@@ -74,28 +57,3 @@ function allow (mode, checkPermissionsForDirectory) {
7457 next ( error )
7558 }
7659}
77-
78- /**
79- * Returns a fetch document handler used by the ACLChecker to fetch .acl
80- * resources up the inheritance chain.
81- * The `fetch(uri, callback)` results in the callback, with either:
82- * - `callback(err, graph)` if any error is encountered, or
83- * - `callback(null, graph)` with the parsed RDF graph of the fetched resource
84- * @return {Function } Returns a `fetch(uri, callback)` handler
85- */
86- function fetchFromLdp ( mapper ) {
87- return async function fetch ( url , graph = $rdf . graph ( ) ) {
88- // Convert the URL into a filename
89- let path , contentType
90- try {
91- ( { path, contentType } = await mapper . mapUrlToFile ( { url } ) )
92- } catch ( err ) {
93- throw new HTTPError ( 404 , err )
94- }
95- // Read the file from disk
96- const body = await promisify ( fs . readFile ) ( path , { 'encoding' : 'utf8' } )
97- // Parse the file as Turtle
98- $rdf . parse ( body , graph , url , contentType )
99- return graph
100- }
101- }
0 commit comments