33"use strict" ;
44
55var fs = require ( 'fs' ) ;
6+ var glob = require ( 'glob' ) ;
67var path = require ( 'path' ) ;
78var $rdf = require ( 'rdflib' ) ;
89var S = require ( 'string' ) ;
@@ -26,15 +27,19 @@ function allow(mode, req, res) {
2627 var relativePath = file . uriToRelativeFilename ( req . path ) ;
2728 var depth = relativePath . split ( '/' ) ;
2829
30+ //Handle glob requests
31+ if ( req . method === 'GET' && glob . hasMagic ( filepath ) ) {
32+ return {
33+ status : 200 ,
34+ err : null
35+ } ;
36+ }
37+
2938 for ( var i = 0 ; i < depth . length ; i ++ ) {
3039 var pathAcl = S ( filepath ) . endsWith ( aclExtension ) ?
3140 filepath : filepath + aclExtension ;
3241 var pathUri = file . filenameToBaseUri ( filepath ) ;
33- var pathAclUri = file . filenameToBaseUri ( pathAcl ) ;
34-
35- if ( ! fs . existsSync ( filepath ) || ! fs . existsSync ( pathAcl ) ) {
36- continue ;
37- }
42+ relativePath = path . relative ( options . fileBase , filepath ) ;
3843
3944 logging . log ( "ACL -- Checking " + accessType + "<" + mode + "> to " +
4045 pathUri + " for WebID: " + req . session . userId ) ;
@@ -198,25 +203,28 @@ function allow(mode, req, res) {
198203
199204 accessType = "defaultForNew" ;
200205 if ( i === 0 ) {
201- if ( S ( filepath ) . endsWith ( '/' ) ) {
202- if ( path . dirname ( path . dirname ( filepath ) ) === "." ) {
203- filepath = options . fileBase ;
204- } else {
205- filepath = options . fileBase + path . dirname ( path . dirname ( filepath ) ) ;
206- }
206+ if ( path . dirname ( path . dirname ( relativePath ) ) == '.' ) {
207+ filepath = options . fileBase ;
208+ } else if ( S ( relativePath ) . endsWith ( "/" ) ) {
209+ filepath = options . fileBase + path . dirname ( path . dirname ( relativePath ) ) ;
210+ } else {
211+ filepath = options . fileBase + path . dirname ( relativePath ) ;
207212 }
208213 } else {
209- if ( filepath . length === 0 ) {
214+ if ( relativePath . length === 0 ) {
210215 break ;
211- } else if ( path . dirname ( path . dirname ( filepath ) ) === "." ) {
216+ } else if ( path . dirname ( path . dirname ( relativePath ) ) === '.' ) {
212217 filepath = options . fileBase ;
213218 } else {
214- filepath = options . fileBase + path . dirname ( path . dirname ( filepath ) ) ;
219+ filepath = options . fileBase + path . dirname ( path . dirname ( relativePath ) ) ;
215220 }
216221 }
217222
218- filepath += "/" ;
223+ if ( ! S ( filepath ) . endsWith ( "/" ) ) {
224+ filepath += "/" ;
225+ }
219226 }
227+
220228 logging . log ( "ACL -- No ACL policies present - access allowed" ) ;
221229 return {
222230 status : 200 ,
@@ -237,10 +245,6 @@ function allowOrigin(mode, req, res, aclGraph, subject) {
237245 }
238246 var agentStatements = aclGraph . each ( subject , ns . acl ( "agent" ) ,
239247 aclGraph . sym ( req . session . userId ) ) ;
240- logging . log ( subject ) ;
241- logging . log ( ns . acl ( "agent" ) ) ;
242- logging . log ( aclGraph . sym ( req . session . userId ) ) ;
243- logging . log ( "agentStatements " + agentStatements ) ;
244248 for ( var agentIndex in agentStatements ) {
245249 logging . log ( "ACL -- " + mode + " access allowed (as agent) for: " + req . session . userId ) ;
246250 return {
@@ -292,6 +296,8 @@ function allowIfACLEnabled(mode, req, res, next) {
292296 }
293297}
294298
299+ exports . allow = allow ;
300+
295301exports . allowReadHandler = function ( req , res , next ) {
296302 allowIfACLEnabled ( "Read" , req , res , next ) ;
297303} ;
@@ -300,6 +306,28 @@ exports.allowWriteHandler = function(req, res, next) {
300306 allowIfACLEnabled ( "Write" , req , res , next ) ;
301307} ;
302308
309+ exports . allowAppendHandler = function ( req , res , next ) {
310+ allowIfACLEnabled ( "Append" , req , res , next ) ;
311+ } ;
312+
313+ exports . allowAppendThenWriteHandler = function ( req , res , next ) {
314+ if ( ! options . webid ) {
315+ return next ( ) ;
316+ } else {
317+ var allowAppendValue = allow ( "Append" , req , res , next ) ;
318+ if ( allowAppendValue . status == 200 ) {
319+ return next ( ) ;
320+ } else {
321+ var allowWriteValue = allow ( "Write" , req , res , next ) ;
322+ if ( allowWriteValue . status == 200 ) {
323+ return next ( ) ;
324+ } else {
325+ return res . status ( allowWriteValue . status ) . send ( allowWriteValue . err ) ;
326+ }
327+ }
328+ }
329+ } ;
330+
303331exports . allowControlHandler = function ( req , res , next ) {
304332 allowIfACLEnabled ( "Control" , req , res , next ) ;
305333} ;
0 commit comments