Skip to content

Commit

Permalink
Implemented the lock check + Implemented the privilege checks
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed Jun 28, 2017
1 parent 467e91c commit 0b41e28
Show file tree
Hide file tree
Showing 23 changed files with 1,539 additions and 909 deletions.
116 changes: 63 additions & 53 deletions lib/manager/v2/fileSystem/FileSystem.d.ts

Large diffs are not rendered by default.

867 changes: 575 additions & 292 deletions lib/manager/v2/fileSystem/FileSystem.js

Large diffs are not rendered by default.

11 changes: 4 additions & 7 deletions lib/server/v2/commands/Delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@ var default_1 = (function () {
default_1.prototype.unchunked = function (ctx, data, callback) {
ctx.noBodyExpected(function () {
ctx.getResource(function (e, r) {
if (e) {
ctx.setCode(WebDAVRequest_1.HTTPCodes.NotFound);
callback();
return;
}
ctx.checkIfHeader(r, function () {
//ctx.requirePrivilege([ 'canDelete' ], r, () => {
r.delete(function (e) { return process.nextTick(function () {
if (e)
ctx.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
if (e) {
if (!ctx.setCodeFromError(e))
ctx.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
}
else {
ctx.setCode(WebDAVRequest_1.HTTPCodes.OK);
//ctx.invokeEvent('delete', r);
Expand Down
9 changes: 6 additions & 3 deletions lib/server/v2/commands/Get.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ var default_1 = (function () {
//ctx.requirePrivilegeEx(targetSource ? [ 'canRead', 'canSource', 'canGetMimeType' ] : [ 'canRead', 'canGetMimeType' ], () => {
r.type(function (e, type) {
if (e) {
ctx.setCode(e === Errors_1.Errors.ResourceNotFound ? WebDAVRequest_1.HTTPCodes.NotFound : WebDAVRequest_1.HTTPCodes.InternalServerError);
if (!ctx.setCodeFromError(e))
ctx.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
return callback();
}
if (!type.isFile) {
Expand All @@ -65,12 +66,14 @@ var default_1 = (function () {
}
r.mimeType(targetSource, function (e, mimeType) { return process.nextTick(function () {
if (e) {
ctx.setCode(e === Errors_1.Errors.ResourceNotFound ? WebDAVRequest_1.HTTPCodes.NotFound : WebDAVRequest_1.HTTPCodes.InternalServerError);
if (!ctx.setCodeFromError(e))
ctx.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
return callback();
}
r.openReadStream(targetSource, function (e, rstream) { return process.nextTick(function () {
if (e) {
ctx.setCode(WebDAVRequest_1.HTTPCodes.MethodNotAllowed);
if (!ctx.setCodeFromError(e))
ctx.setCode(WebDAVRequest_1.HTTPCodes.MethodNotAllowed);
return callback();
}
//ctx.invokeEvent('read', r);
Expand Down
13 changes: 8 additions & 5 deletions lib/server/v2/commands/Head.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var WebDAVRequest_1 = require("../WebDAVRequest");
var Errors_1 = require("../../../Errors");
var default_1 = (function () {
function default_1() {
}
Expand All @@ -13,7 +12,8 @@ var default_1 = (function () {
//ctx.requirePrivilege(targetSource ? [ 'canRead', 'canSource', 'canGetMimeType' ] : [ 'canRead', 'canGetMimeType' ], r, () => {
r.type(function (e, type) {
if (e) {
ctx.setCode(e === Errors_1.Errors.ResourceNotFound ? WebDAVRequest_1.HTTPCodes.NotFound : WebDAVRequest_1.HTTPCodes.InternalServerError);
if (!ctx.setCodeFromError(e))
ctx.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
return callback();
}
if (!type.isFile) {
Expand All @@ -22,12 +22,15 @@ var default_1 = (function () {
}
r.mimeType(targetSource, function (e, mimeType) { return process.nextTick(function () {
if (e) {
ctx.setCode(e === Errors_1.Errors.ResourceNotFound ? WebDAVRequest_1.HTTPCodes.NotFound : WebDAVRequest_1.HTTPCodes.InternalServerError);
if (!ctx.setCodeFromError(e))
ctx.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
return callback();
}
r.size(targetSource, function (e, size) {
if (e)
ctx.setCode(e === Errors_1.Errors.ResourceNotFound ? WebDAVRequest_1.HTTPCodes.NotFound : WebDAVRequest_1.HTTPCodes.InternalServerError);
if (e) {
if (!ctx.setCodeFromError(e))
ctx.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
}
else {
ctx.setCode(WebDAVRequest_1.HTTPCodes.OK);
ctx.response.setHeader('Accept-Ranges', 'bytes');
Expand Down
20 changes: 5 additions & 15 deletions lib/server/v2/commands/Lock.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,7 @@ function createLock(ctx, data, callback) {
var _callback_1 = callback;
callback = function (e) {
if (e) {
if (e === Errors_1.Errors.Locked)
ctx.setCode(WebDAVRequest_1.HTTPCodes.Locked);
else if (e === Errors_1.Errors.IntermediateResourceMissing || e === Errors_1.Errors.WrongParentTypeForCreation)
ctx.setCode(WebDAVRequest_1.HTTPCodes.Conflict);
else
if (!ctx.setCodeFromError(e))
ctx.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
}
else
Expand Down Expand Up @@ -94,15 +90,11 @@ function createLock(ctx, data, callback) {
}
function refreshLock(ctx, lockUUID, callback) {
ctx.getResource(function (e, r) {
if (e) {
ctx.setCode(e === Errors_1.Errors.ResourceNotFound ? WebDAVRequest_1.HTTPCodes.NotFound : WebDAVRequest_1.HTTPCodes.InternalServerError);
callback();
return;
}
//ctx.requirePrivilege([ 'canSetLock', 'canGetLock' ], r, () => {
r.lockManager(function (e, lm) {
if (e) {
ctx.setCode(e === Errors_1.Errors.ResourceNotFound ? WebDAVRequest_1.HTTPCodes.NotFound : WebDAVRequest_1.HTTPCodes.InternalServerError);
if (!ctx.setCodeFromError(e))
ctx.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
return callback();
}
lm.refresh(lockUUID, ctx.server.options.lockTimeout, function (e, lock) {
Expand All @@ -126,8 +118,7 @@ var default_1 = (function () {
default_1.prototype.unchunked = function (ctx, data, callback) {
if (!ctx.user) {
ctx.setCode(WebDAVRequest_1.HTTPCodes.Forbidden);
callback();
return;
return callback();
}
if (ctx.headers.contentLength > 0) {
createLock(ctx, data, callback);
Expand All @@ -136,8 +127,7 @@ var default_1 = (function () {
var ifHeader = ctx.headers.find('If');
if (!ifHeader) {
ctx.setCode(WebDAVRequest_1.HTTPCodes.PreconditionRequired);
callback();
return;
return callback();
}
refreshLock(ctx, IfParser_1.extractOneToken(ifHeader), callback);
};
Expand Down
15 changes: 6 additions & 9 deletions lib/server/v2/commands/Mkcol.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,19 @@ var default_1 = (function () {
ctx.getResource(r.path.getParent(), function (e, rParent) {
rParent.type(function (e, parentType) {
if (e) {
ctx.setCode(e === Errors_1.Errors.ResourceNotFound ? WebDAVRequest_1.HTTPCodes.Conflict : WebDAVRequest_1.HTTPCodes.InternalServerError);
callback();
return;
if (!ctx.setCodeFromError(e))
ctx.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
return callback();
}
if (!parentType.isDirectory) {
ctx.setCode(WebDAVRequest_1.HTTPCodes.Forbidden);
callback();
return;
return callback();
}
r.create(CommonTypes_1.ResourceType.Directory, function (e) {
if (e) {
if (e === Errors_1.Errors.WrongParentTypeForCreation)
ctx.setCode(WebDAVRequest_1.HTTPCodes.Conflict);
else if (e === Errors_1.Errors.ResourceAlreadyExists)
if (e === Errors_1.Errors.ResourceAlreadyExists)
ctx.setCode(WebDAVRequest_1.HTTPCodes.MethodNotAllowed);
else
else if (!ctx.setCodeFromError(e))
ctx.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
}
else
Expand Down
18 changes: 5 additions & 13 deletions lib/server/v2/commands/Move.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
var WebDAVRequest_1 = require("../WebDAVRequest");
var StandardMethods_1 = require("../../../manager/v2/fileSystem/StandardMethods");
var Path_1 = require("../../../manager/v2/Path");
var Errors_1 = require("../../../Errors");
function execute(ctx, methodName, privilegeName, callback) {
ctx.noBodyExpected(function () {
ctx.getResource(function (e, r) {
Expand All @@ -13,8 +12,7 @@ function execute(ctx, methodName, privilegeName, callback) {
var destination = ctx.headers.find('destination');
if (!destination) {
ctx.setCode(WebDAVRequest_1.HTTPCodes.BadRequest);
callback();
return;
return callback();
}
var startIndex = destination.indexOf('://');
if (startIndex !== -1) {
Expand All @@ -27,16 +25,10 @@ function execute(ctx, methodName, privilegeName, callback) {
return callback();
}
var cb = function (e, overwritten) {
if (e === Errors_1.Errors.ResourceNotFound)
ctx.setCode(WebDAVRequest_1.HTTPCodes.NotFound);
else if (e === Errors_1.Errors.InsufficientStorage)
ctx.setCode(WebDAVRequest_1.HTTPCodes.InsufficientStorage);
else if (e === Errors_1.Errors.Locked)
ctx.setCode(WebDAVRequest_1.HTTPCodes.Locked);
else if (e === Errors_1.Errors.ResourceAlreadyExists)
ctx.setCode(WebDAVRequest_1.HTTPCodes.Conflict);
else if (e)
ctx.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
if (e) {
if (!ctx.setCodeFromError(e))
ctx.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
}
else if (overwritten)
ctx.setCode(WebDAVRequest_1.HTTPCodes.NoContent);
else
Expand Down
12 changes: 2 additions & 10 deletions lib/server/v2/commands/Propfind.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,7 @@ var default_1 = (function () {
if (!e)
done(multistatus);
else {
if (e === Errors_1.Errors.ResourceNotFound)
ctx.setCode(WebDAVRequest_1.HTTPCodes.NotFound);
else if (e === Errors_1.Errors.BadAuthentication)
ctx.setCode(WebDAVRequest_1.HTTPCodes.Unauthorized);
else
if (!ctx.setCodeFromError(e))
ctx.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
callback();
}
Expand All @@ -98,11 +94,7 @@ var default_1 = (function () {
//ctx.requirePrivilege('canGetChildren', resource, () => {
resource.readDir(true, function (e, children) { return process.nextTick(function () {
function err(e) {
if (e === Errors_1.Errors.ResourceNotFound)
ctx.setCode(WebDAVRequest_1.HTTPCodes.NotFound);
else if (e === Errors_1.Errors.BadAuthentication)
ctx.setCode(WebDAVRequest_1.HTTPCodes.Unauthorized);
else
if (!ctx.setCodeFromError(e))
ctx.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
callback();
}
Expand Down
31 changes: 21 additions & 10 deletions lib/server/v2/commands/Proppatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var WebDAVRequest_1 = require("../WebDAVRequest");
var http_1 = require("http");
var Workflow_1 = require("../../../helper/Workflow");
var XML_1 = require("../../../helper/XML");
var Errors_1 = require("../../../Errors");
var default_1 = (function () {
function default_1() {
}
Expand All @@ -28,7 +27,7 @@ var default_1 = (function () {
};
};
var notify_1 = function (el, error) {
var code = error ? WebDAVRequest_1.HTTPCodes.Conflict : WebDAVRequest_1.HTTPCodes.OK;
var code = error ? WebDAVRequest_1.HTTPCodes.Forbidden : WebDAVRequest_1.HTTPCodes.OK;
var propstat = response.ele('D:propstat');
propstat.ele('D:prop').ele(el.name);
propstat.ele('D:status').add('HTTP/1.1 ' + code + ' ' + http_1.STATUS_CODES[code]);
Expand All @@ -51,16 +50,28 @@ var default_1 = (function () {
.done(function () { return finalize_1(); });
});
};
r.propertyManager(function (e, pm) {
if (e) {
ctx.setCode(e === Errors_1.Errors.ResourceNotFound ? WebDAVRequest_1.HTTPCodes.NotFound : WebDAVRequest_1.HTTPCodes.InternalServerError);
r.fs.isLocked(ctx, r.path, function (e, locked) {
if (e || locked) {
if (e) {
if (!ctx.setCodeFromError(e))
ctx.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
}
else if (locked)
ctx.setCode(WebDAVRequest_1.HTTPCodes.Locked);
return callback();
}
execute_1('DAV:set', 'setProperty', function (el, callback) {
pm.setProperty(el.name, el.elements, callback);
});
execute_1('DAV:remove', 'removeProperty', function (el, callback) {
pm.removeProperty(el.name, callback);
r.propertyManager(function (e, pm) {
if (e) {
if (!ctx.setCodeFromError(e))
ctx.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
return callback();
}
execute_1('DAV:set', 'setProperty', function (el, callback) {
pm.setProperty(el.name, el.elements, callback);
});
execute_1('DAV:remove', 'removeProperty', function (el, callback) {
pm.removeProperty(el.name, callback);
});
});
});
}
Expand Down
18 changes: 9 additions & 9 deletions lib/server/v2/commands/Put.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@ var default_1 = (function () {
mode = 'mustCreate';
}
else if (e) {
ctx.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
callback();
return;
if (!ctx.setCodeFromError(e))
ctx.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
return callback();
}
else if (!type.isFile) {
ctx.setCode(WebDAVRequest_1.HTTPCodes.MethodNotAllowed);
callback();
return;
return callback();
}
r.openWriteStream(mode, targetSource, ctx.headers.contentLength, function (e, wStream, created) {
if (e) {
ctx.setCode(e === Errors_1.Errors.IntermediateResourceMissing || e === Errors_1.Errors.WrongParentTypeForCreation ? WebDAVRequest_1.HTTPCodes.Conflict : WebDAVRequest_1.HTTPCodes.InternalServerError);
callback();
return;
if (!ctx.setCodeFromError(e))
ctx.setCode(e === Errors_1.Errors.IntermediateResourceMissing || e === Errors_1.Errors.WrongParentTypeForCreation ? WebDAVRequest_1.HTTPCodes.Conflict : WebDAVRequest_1.HTTPCodes.InternalServerError);
return callback();
}
inputStream.pipe(wStream);
wStream.on('finish', function (e) {
Expand All @@ -44,7 +43,8 @@ var default_1 = (function () {
callback();
});
wStream.on('error', function (e) {
ctx.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
if (!ctx.setCodeFromError(e))
ctx.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
callback();
});
});
Expand Down
Loading

0 comments on commit 0b41e28

Please sign in to comment.