Skip to content

Commit 530f749

Browse files
author
nicola
committed
all tests pass, acl attempt has succeeded
1 parent 0f9b42f commit 530f749

2 files changed

Lines changed: 45 additions & 54 deletions

File tree

‎acl.js‎

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ ACL.prototype.findACL = function(mode, address, userId, callback) {
123123
async.whilst(
124124
// Check if we have gone through all the `/` in relativePath
125125
function() {
126-
console.log(i);
127126
return i++ < depth.length;
128127
},
129128
function (done) {
@@ -177,16 +176,16 @@ ACL.prototype.findACL = function(mode, address, userId, callback) {
177176
// result is false when no policy is found
178177
if (!result) {
179178
debug("No ACL policies present - access allowed");
180-
return callback(null, true);
179+
return callback(null);
181180
}
182181

183182
// result is true if ACL statement is found
184183
if (result === true) {
185184
debug("ACL allowed");
186-
return callback(null, true);
185+
return callback(null);
187186
}
188187

189-
// result is an object (since obj !== true)
188+
// result is an object (since result !== true)
190189
// the object is of the type {status: 40[0-9], message: String}
191190
return callback(result);
192191
}
@@ -198,7 +197,6 @@ ACL.prototype.findACL = function(mode, address, userId, callback) {
198197
* @param {Object} err Error occurred when reading the acl file
199198
* @param {Number} err.status Status code of the error (HTTP way)
200199
* @param {String} err.message Reason of the error
201-
* @param {Boolean} result Found valid ACL statement
202200
*/
203201

204202
ACL.prototype.allowMode = function (mode, userId, aclGraph, accessType, pathUri, callback) {
@@ -209,20 +207,16 @@ ACL.prototype.allowMode = function (mode, userId, aclGraph, accessType, pathUri,
209207
debug("Found " + accessType + " policy for <" + mode + ">");
210208

211209
var accessTypeStatements = aclGraph.each(modeElem, ns.acl(accessType), aclGraph.sym(pathUri));
212-
213-
console.log("-- accessTypeStatements", accessTypeStatements.length)
214210
async.some(accessTypeStatements, function(accessTypeElem, next) {
215211
var origins = aclGraph.each(modeElem, ns.acl("origin"), undefined);
216-
217-
console.log("-- ACL origins, ", acl.origin, origins)
218-
console.log("-- ACL origins lengths ", acl.origin.length, origins.length)
219212
if (acl.origin.length > 0 && origins.length > 0) {
220213
debug("Origin set to: " + rdfVocab.brack(acl.origin));
221214
async.some(origins, function(originsElem, done) {
222215
if (rdfVocab.brack(acl.origin) === originsElem.toString()) {
223216
debug("Found policy for origin: " + originsElem.toString());
224-
acl.allowOrigin(mode, userId, aclGraph, modeElem, done);
217+
return acl.allowOrigin(mode, userId, aclGraph, modeElem, done);
225218
}
219+
return done(false);
226220
}, next);
227221
} else {
228222
debug("No origin found, moving on.");
@@ -274,8 +268,8 @@ ACL.prototype.allow = function(mode, address, callback) {
274268
if (err) {
275269
return callback(err);
276270
}
277-
acl.findACL(mode, address, userId, function(err, res) {
278-
return callback(err, res);
271+
acl.findACL(mode, address, userId, function(err) {
272+
return callback(err);
279273
});
280274
});
281275
};
@@ -285,7 +279,6 @@ ACL.prototype.allow = function(mode, address, callback) {
285279
* @param {Object} err Error occurred when reading the acl file
286280
* @param {Number} err.status Status code of the error (HTTP way)
287281
* @param {String} err.message Reason of the error
288-
* @param {Boolean} result Found valid ACL statement
289282
*/
290283

291284
ACL.prototype.allowOrigin = function (mode, userId, aclGraph, subject, callback) {
@@ -383,11 +376,13 @@ ACL.prototype.fetchDocument = function(uri, callback) {
383376
var documentPath = file.uriToFilename(newPath, ldp.root);
384377
var documentUri = url.parse(documentPath);
385378
documentPath = documentUri.pathname;
386-
acl.allow('Read', newPath, function (err, readAllowed) {
387-
if (readAllowed) {
388-
return fs.readFile(documentPath, {encoding: 'utf8'}, cb);
379+
acl.allow('Read', newPath, function (err) {
380+
if (err) {
381+
// return an empty body to be parsed
382+
return cb(null, '');
389383
}
390-
// TODO here should be an error
384+
385+
return fs.readFile(documentPath, {encoding: 'utf8'}, cb);
391386
});
392387
},
393388
function (body, cb) {
@@ -480,21 +475,24 @@ function allowIfACLEnabled(mode, req, res, next) {
480475
if (!ldp.webid) {
481476
return next();
482477
}
483-
return allow(mode, req, next);
478+
return allow(mode, req, res, next);
484479
}
485480

486-
function allow(mode, req, callback) {
481+
function allow(mode, req, res, next) {
487482
var ldp = req.app.locals.ldp;
488483

489484
// Handle glob requests
490485
var filepath = file.uriToFilename(req.path, ldp.root);
491486
if (req.method === 'GET' && glob.hasMagic(filepath)) {
492-
return callback(null, true);
487+
return next();
493488
}
494489

495490
// Check ACL
491+
var reqPath = res && res.locals && res.locals.path ? res.locals.path : req.path;
496492
var acl = reqToACL(req);
497-
acl.allow(mode, req.path, callback);
493+
acl.allow(mode, reqPath, function(err) {
494+
next(err);
495+
});
498496
}
499497

500498
exports.allow = allow;
@@ -517,18 +515,13 @@ exports.allowAppendThenWriteHandler = function(req, res, next) {
517515
return next();
518516
}
519517

520-
allow("Append", req, function(err, allowed) {
521-
if (!err && allowed === true) {
518+
allow("Append", req, res, function(err) {
519+
if (!err) {
522520
return next();
523521
}
524522
// Append failed, maybe user can write
525-
allow("Write", req, function(err, allowed) {
526-
if (!err && allowed === true) {
527-
return next();
528-
}
529-
return res
530-
.status(err.status)
531-
.send(err.message || '');
523+
allow("Write", req, res, function(err) {
524+
next(err);
532525
});
533526
});
534527

‎handlers/get.js‎

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -143,19 +143,21 @@ function globHandler(req, res) {
143143
debug('GET -- Error in globHandler' + err);
144144
return done(null);
145145
}
146-
if (!S(match).endsWith(".ttl") || !aclAllow(match, req, res)) {
146+
aclAllow(match, req, res, function (allowed) {
147+
if (!S(match).endsWith(".ttl") || !allowed) {
148+
return done(null);
149+
}
150+
try {
151+
$rdf.parse(
152+
fileData,
153+
globGraph,
154+
baseUri,
155+
'text/turtle');
156+
} catch(parseErr) {
157+
debug('GET -- Error in globHandler' + parseErr);
158+
}
147159
return done(null);
148-
}
149-
try {
150-
$rdf.parse(
151-
fileData,
152-
globGraph,
153-
baseUri,
154-
'text/turtle');
155-
} catch(parseErr) {
156-
debug('GET -- Error in globHandler' + parseErr);
157-
}
158-
return done(null);
160+
});
159161
});
160162
}, function () {
161163
var data = $rdf.serialize(
@@ -170,24 +172,20 @@ function globHandler(req, res) {
170172
});
171173
}
172174

173-
function aclAllow(match, req, res) {
175+
function aclAllow(match, req, res, callback) {
174176
var ldp = req.app.locals.ldp;
175177

176178
if (!ldp.webid) {
177-
return true;
179+
return callback(true);
178180
}
179181

180-
var relativePath = '/' +
181-
path.relative(ldp.root, match);
182+
var relativePath = '/' + path.relative(ldp.root, match);
182183

184+
// TODO find a better hack, maybe pass as a param?
183185
res.locals.path = relativePath;
184-
var allow = acl.allow("Read", req, res);
185-
186-
if (allow.status === 200) {
187-
return true;
188-
} else {
189-
return false;
190-
}
186+
acl.allow("Read", req, res, function(err) {
187+
callback(!err);
188+
});
191189
}
192190

193191
function parseLinkedData(req, res) {

0 commit comments

Comments
 (0)