@@ -8,23 +8,22 @@ var path = require('path');
88var $rdf = require ( 'rdflib' ) ;
99var S = require ( 'string' ) ;
1010
11+ var debug = require ( './logging' ) . ACL ;
1112var file = require ( './fileStore.js' ) ;
12- var logging = require ( './logging.js' ) ;
13- var options = require ( './options.js' ) ;
14-
1513var ns = require ( './vocab/ns.js' ) . ns ;
1614var rdfVocab = require ( './vocab/rdf.js' ) ;
1715
1816var aclExtension = ".acl" ;
1917
2018function allow ( mode , req , res ) {
19+ var options = req . app . locals . ldp ;
2120 var origin = req . get ( 'origin' ) ;
2221 origin = origin ? origin : '' ;
2322
2423 var accessType = "accessTo" ;
2524
26- var filepath = file . uriToFilename ( req . path ) ;
27- var relativePath = file . uriToRelativeFilename ( req . path ) ;
25+ var filepath = file . uriToFilename ( req . path , options . fileBase ) ;
26+ var relativePath = file . uriToRelativeFilename ( req . path , options . fileBase ) ;
2827 var depth = relativePath . split ( '/' ) ;
2928
3029 //Handle glob requests
@@ -38,27 +37,27 @@ function allow(mode, req, res) {
3837 for ( var i = 0 ; i < depth . length ; i ++ ) {
3938 var pathAcl = S ( filepath ) . endsWith ( aclExtension ) ?
4039 filepath : filepath + aclExtension ;
41- var pathUri = file . filenameToBaseUri ( filepath ) ;
40+ var pathUri = file . filenameToBaseUri ( filepath , options . uriBase , options . fileBase ) ;
4241 relativePath = path . relative ( options . fileBase , filepath ) ;
4342
44- logging . log ( "ACL -- Checking " + accessType + "<" + mode + "> to " +
43+ debug ( " Checking " + accessType + "<" + mode + "> to " +
4544 pathUri + " for WebID: " + req . session . userId ) ;
46- logging . log ( "ACL -- Looking for policies in " + pathAcl ) ;
45+ debug ( " Looking for policies in " + pathAcl ) ;
4746
4847 var aclData ;
4948 var aclGraph = $rdf . graph ( ) ;
5049 try {
5150 aclData = fs . readFileSync ( pathAcl , { encoding : 'utf8' } ) ;
5251 $rdf . parse ( aclData , aclGraph , pathUri , 'text/turtle' ) ;
5352 } catch ( parseErr ) {
54- logging . log ( "ACL -- Error parsing ACL policy: " + parseErr ) ;
53+ debug ( " Error parsing ACL policy: " + parseErr ) ;
5554 //Resetting graph to prevent the code from taking the next if brach.
5655 aclGraph = $rdf . graph ( ) ;
5756 }
5857
5958
6059 if ( aclGraph . statements . length > 0 ) {
61- logging . log ( "ACL -- Found policies in " + pathAcl ) ;
60+ debug ( " Found policies in " + pathAcl ) ;
6261 var controlStatements = aclGraph . each ( undefined , ns . acl ( "mode" ) ,
6362 ns . acl ( "Control" ) ) ;
6463 for ( var controlIndex in controlStatements ) {
@@ -72,11 +71,11 @@ function allow(mode, req, res) {
7271 var originsControl = aclGraph . each ( modeElem , ns . acl ( "origin" ) , undefined ) ;
7372 var originControlValue ;
7473 if ( origin . length > 0 && originsControl . length > 0 ) {
75- logging . log ( "ACL -- Origin set to: " + rdfVocab . brack ( origin ) ) ;
74+ debug ( " Origin set to: " + rdfVocab . brack ( origin ) ) ;
7675 for ( var originsControlIndex in originsControl ) {
7776 var originsControlElem = originsControl [ originsControlIndex ] ;
7877 if ( rdfVocab . brack ( origin ) === originsControlElem . toString ( ) ) {
79- logging . log ( "ACL -- Found policy for origin: " +
78+ debug ( " Found policy for origin: " +
8079 originsControlElem . toString ( ) ) ;
8180 originControlValue = allowOrigin ( mode , req , res , aclGraph , controlElem ) ;
8281 if ( originControlValue ) {
@@ -86,7 +85,7 @@ function allow(mode, req, res) {
8685 }
8786 continue ;
8887 } else {
89- logging . log ( "ACL -- No origin found, moving on.") ;
88+ debug ( " No origin found, moving on.") ;
9089 }
9190 originControlValue = allowOrigin ( mode , req , res , aclGraph , controlElem ) ;
9291 if ( originControlValue ) {
@@ -96,7 +95,7 @@ function allow(mode, req, res) {
9695 var ownerStatements = aclGraph . each ( accessElem ,
9796 ns . acl ( "owner" ) , aclGraph . sym ( req . session . userId ) ) ;
9897 for ( var ownerIndex in ownerStatements ) {
99- logging . log ( "ACL -- " + mode + " access allowed (as owner)" +
98+ debug ( mode + " access allowed (as owner)" +
10099 " for: " + req . session . userId ) ;
101100 return {
102101 status : 200 ,
@@ -107,7 +106,7 @@ function allow(mode, req, res) {
107106 var agentStatements = aclGraph . each ( controlElem ,
108107 ns . acl ( "agent" ) , aclGraph . sym ( req . session . userId ) ) ;
109108 for ( var agentIndex in agentStatements ) {
110- logging . log ( "ACL -- " + mode + " access allowed (as agent)" +
109+ debug ( mode + " access allowed (as agent)" +
111110 " for: " + req . session . userId ) ;
112111 return {
113112 status : 200 ,
@@ -119,9 +118,9 @@ function allow(mode, req, res) {
119118 ns . acl ( "agentClass" ) , undefined ) ;
120119 for ( var agentClassIndex in agentClassStatements ) {
121120 var agentClassElem = agentClassStatements [ agentClassIndex ] ;
122- logging . log ( "ACL -- Found agentClass policy") ;
121+ debug ( " Found agentClass policy") ;
123122 if ( agentClassElem . sameTerm ( ns . foaf ( "Agent" ) ) ) {
124- logging . log ( "ACL -- " + mode +
123+ debug ( mode +
125124 " access allowed as FOAF agent" ) ;
126125 return {
127126 status : 200 ,
@@ -141,7 +140,7 @@ function allow(mode, req, res) {
141140 agentClassElem , ns . foaf ( "member" ) ,
142141 groupGraph . sym ( req . session . userId ) ) ;
143142 for ( var memberIndex in memberStatements ) {
144- logging . log ( "ACL -- " + req . session . userId +
143+ debug ( req . session . userId +
145144 " listed as member of the group " + groupURI ) ;
146145 return {
147146 status : 200 ,
@@ -156,19 +155,19 @@ function allow(mode, req, res) {
156155 var modeStatements = aclGraph . each ( undefined , ns . acl ( "mode" ) , ns . acl ( mode ) ) ;
157156 for ( var modeIndex in modeStatements ) {
158157 var modeElem = modeStatements [ modeIndex ] ;
159- logging . log ( "ACL -- Found " + accessType + " policy for <" + mode + ">" ) ;
158+ debug ( " Found " + accessType + " policy for <" + mode + ">" ) ;
160159 var accessTypeStatements = aclGraph . each ( modeElem , ns . acl ( accessType ) ,
161160 aclGraph . sym ( pathUri ) ) ;
162161 for ( var accessTypeIndex in accessTypeStatements ) {
163162 var accessTypeElem = accessTypeStatements [ accessTypeIndex ] ;
164163 var origins = aclGraph . each ( modeElem , ns . acl ( "origin" ) , undefined ) ;
165164 var originValue ;
166165 if ( origin . length > 0 && origins . length > 0 ) {
167- logging . log ( "ACL -- Origin set to: " + rdfVocab . brack ( origin ) ) ;
166+ debug ( " Origin set to: " + rdfVocab . brack ( origin ) ) ;
168167 for ( var originsIndex in origins ) {
169168 var originsElem = origins [ originsIndex ] ;
170169 if ( rdfVocab . brack ( origin ) === originsElem . toString ( ) ) {
171- logging . log ( "ACL -- Found policy for origin: " +
170+ debug ( " Found policy for origin: " +
172171 originsElem . toString ( ) ) ;
173172 originValue = allowOrigin ( mode , req , res , aclGraph , modeElem ) ;
174173 if ( originValue ) {
@@ -178,7 +177,7 @@ function allow(mode, req, res) {
178177 }
179178 continue ;
180179 } else {
181- logging . log ( "ACL -- No origin found, moving on.") ;
180+ debug ( " No origin found, moving on.") ;
182181 }
183182 originValue = allowOrigin ( mode , req , res , aclGraph , modeElem ) ;
184183 if ( originValue ) {
@@ -188,13 +187,13 @@ function allow(mode, req, res) {
188187 }
189188
190189 if ( req . session . userId . length === 0 || req . session . identified === false ) {
191- logging . log ( "ACL -- Authentication required") ;
190+ debug ( " Authentication required") ;
192191 return {
193192 status : 401 ,
194193 err : "Access to " + pathUri + " requires authorization"
195194 } ;
196195 }
197- logging . log ( "ACL -- " + mode + " access denied for: " + req . session . userId ) ;
196+ debug ( mode + " access denied for: " + req . session . userId ) ;
198197 return {
199198 status : 403 ,
200199 err : "Access denied for " + req . session . userId
@@ -225,19 +224,19 @@ function allow(mode, req, res) {
225224 }
226225 }
227226
228- logging . log ( "ACL -- No ACL policies present - access allowed") ;
227+ debug ( " No ACL policies present - access allowed") ;
229228 return {
230229 status : 200 ,
231230 err : null
232231 } ;
233232}
234233
235234function allowOrigin ( mode , req , res , aclGraph , subject ) {
236- logging . log ( "ACL -- In allow origin") ;
235+ debug ( " In allow origin") ;
237236 var ownerStatements = aclGraph . each ( subject , ns . acl ( "owner" ) ,
238237 aclGraph . sym ( req . session . userId ) ) ;
239238 for ( var ownerIndex in ownerStatements ) {
240- logging . log ( "ACL -- " + mode + " access allowed (as owner) for: " + req . session . userId ) ;
239+ debug ( mode + " access allowed (as owner) for: " + req . session . userId ) ;
241240 return {
242241 status : 200 ,
243242 err : null
@@ -246,7 +245,7 @@ function allowOrigin(mode, req, res, aclGraph, subject) {
246245 var agentStatements = aclGraph . each ( subject , ns . acl ( "agent" ) ,
247246 aclGraph . sym ( req . session . userId ) ) ;
248247 for ( var agentIndex in agentStatements ) {
249- logging . log ( "ACL -- " + mode + " access allowed (as agent) for: " + req . session . userId ) ;
248+ debug ( mode + " access allowed (as agent) for: " + req . session . userId ) ;
250249 return {
251250 status : 200 ,
252251 return : null
@@ -256,9 +255,9 @@ function allowOrigin(mode, req, res, aclGraph, subject) {
256255 for ( var agentClassIndex in agentClassStatements ) {
257256 var agentClassElem = agentClassStatements [ agentClassIndex ] ;
258257 //Check for FOAF groups
259- logging . log ( "ACL -- Found agentClass policy") ;
258+ debug ( " Found agentClass policy") ;
260259 if ( agentClassElem . sameTerm ( ns . foaf ( "Agent" ) ) ) {
261- logging . log ( "ACL -- " + mode + " allowed access as FOAF agent" ) ;
260+ debug ( mode + " allowed access as FOAF agent" ) ;
262261 return {
263262 status : 200 ,
264263 err : null
@@ -273,7 +272,7 @@ function allowOrigin(mode, req, res, aclGraph, subject) {
273272 var memberStatements = groupGraph . each ( agentClassElem , ns . foaf ( "member" ) ,
274273 groupGraph . sym ( req . session . userId ) ) ;
275274 for ( var memberIndex in memberStatements ) {
276- logging . log ( "ACL -- " + req . session . userId + " listed as member of the group " + groupURI ) ;
275+ debug ( req . session . userId + " listed as member of the group " + groupURI ) ;
277276 return {
278277 status : 200 ,
279278 err : null
@@ -284,6 +283,7 @@ function allowOrigin(mode, req, res, aclGraph, subject) {
284283}
285284
286285function allowIfACLEnabled ( mode , req , res , next ) {
286+ var options = req . app . locals . ldp ;
287287 if ( ! options . webid ) {
288288 return next ( ) ;
289289 } else {
@@ -311,6 +311,7 @@ exports.allowAppendHandler = function(req, res, next) {
311311} ;
312312
313313exports . allowAppendThenWriteHandler = function ( req , res , next ) {
314+ var options = req . app . locals . ldp ;
314315 if ( ! options . webid ) {
315316 return next ( ) ;
316317 } else {
0 commit comments