From 8cf9a530b7d1465894677e16fd02172247ed7884 Mon Sep 17 00:00:00 2001 From: Adrien Castex Date: Wed, 28 Jun 2017 17:27:12 +0200 Subject: [PATCH] Removed the empty 'requirePrivilege' methods in the 'RequestContext' class + Added the 'setCodeFromError' method and the 'defaultStatusCode' method to the 'RequestContext' class --- lib/server/v2/RequestContext.d.ts | 7 +--- lib/server/v2/RequestContext.js | 34 ++++++++++++---- src/server/v2/RequestContext.ts | 67 +++++++++++++++---------------- 3 files changed, 62 insertions(+), 46 deletions(-) diff --git a/lib/server/v2/RequestContext.d.ts b/lib/server/v2/RequestContext.d.ts index 028e756d..28ce8047 100644 --- a/lib/server/v2/RequestContext.d.ts +++ b/lib/server/v2/RequestContext.d.ts @@ -1,5 +1,4 @@ /// -import { BasicPrivilege } from '../../user/v2/privilege/IPrivilegeManager'; import { XMLElement } from '../../helper/XML'; import { WebDAVServer } from './webDAVServer/WebDAVServer'; import { FileSystem } from '../../manager/v2/fileSystem/FileSystem'; @@ -52,10 +51,6 @@ export declare class RequestContext { noBodyExpected(callback: () => void): void; checkIfHeader(resource: Resource, callback: () => void): any; checkIfHeader(fs: FileSystem, path: Path, callback: () => void): any; - requirePrivilegeEx(privileges: BasicPrivilege | BasicPrivilege[], callback: () => void): any; - requirePrivilegeEx(privileges: string | string[], callback: () => void): any; - requirePrivilege(privileges: BasicPrivilege | BasicPrivilege[], callback: (error: Error, can: boolean) => void): any; - requirePrivilege(privileges: string | string[], callback: (error: Error, can: boolean) => void): any; askForAuthentication(checkForUser: boolean, callback: (error: Error) => void): void; getResource(callback: ReturnCallback): any; getResource(path: Path | string, callback: ReturnCallback): any; @@ -63,5 +58,7 @@ export declare class RequestContext { prefixUri(): string; writeBody(xmlObject: XMLElement | object): void; setCode(code: number, message?: string): void; + defaultStatusCode(error: Error): number; + setCodeFromError(error: Error): boolean; } export default RequestContext; diff --git a/lib/server/v2/RequestContext.js b/lib/server/v2/RequestContext.js index 3046a331..bae90ead 100644 --- a/lib/server/v2/RequestContext.js +++ b/lib/server/v2/RequestContext.js @@ -173,13 +173,6 @@ var RequestContext = (function () { callback(); }); }; - RequestContext.prototype.requirePrivilegeEx = function (privileges, callback) { - callback(); - }; - RequestContext.prototype.requirePrivilege = function (privileges, callback) { - //requirePrivilege(privileges, this, resource, callback); - callback(null, true); - }; RequestContext.prototype.askForAuthentication = function (checkForUser, callback) { if (checkForUser && this.user !== null && !this.user.isDefaultUser) { callback(Errors_1.Errors.AlreadyAuthenticated); @@ -277,6 +270,33 @@ var RequestContext = (function () { this.response.statusMessage = message; } }; + RequestContext.prototype.defaultStatusCode = function (error) { + var code = null; + if (error === Errors_1.Errors.ResourceNotFound) + code = HTTPCodes_1.HTTPCodes.NotFound; + else if (error === Errors_1.Errors.Locked) + code = HTTPCodes_1.HTTPCodes.Locked; + else if (error === Errors_1.Errors.BadAuthentication) + code = HTTPCodes_1.HTTPCodes.Unauthorized; + else if (error === Errors_1.Errors.NotEnoughPrivilege) + code = HTTPCodes_1.HTTPCodes.Unauthorized; + else if (error === Errors_1.Errors.ResourceAlreadyExists) + code = HTTPCodes_1.HTTPCodes.Conflict; + else if (error === Errors_1.Errors.IntermediateResourceMissing) + code = HTTPCodes_1.HTTPCodes.Conflict; + else if (error === Errors_1.Errors.WrongParentTypeForCreation) + code = HTTPCodes_1.HTTPCodes.Conflict; + else if (error === Errors_1.Errors.InsufficientStorage) + code = HTTPCodes_1.HTTPCodes.InsufficientStorage; + return code; + }; + RequestContext.prototype.setCodeFromError = function (error) { + var code = this.defaultStatusCode(error); + if (!code) + return false; + this.setCode(code); + return true; + }; return RequestContext; }()); exports.RequestContext = RequestContext; diff --git a/src/server/v2/RequestContext.ts b/src/server/v2/RequestContext.ts index b2cce728..628f7774 100644 --- a/src/server/v2/RequestContext.ts +++ b/src/server/v2/RequestContext.ts @@ -1,5 +1,3 @@ -//import { IResource, ReturnCallback, ResourceType } from '../../resource/IResource' -import { requirePrivilege, BasicPrivilege } from '../../user/v2/privilege/IPrivilegeManager' import { EventsName, DetailsType } from './webDAVServer/Events' import { XML, XMLElement } from '../../helper/XML' import { parseIfHeader } from '../../helper/v2/IfParser' @@ -255,38 +253,6 @@ export class RequestContext }); } - requirePrivilegeEx(privileges : BasicPrivilege | BasicPrivilege[], callback : () => void) - requirePrivilegeEx(privileges : string | string[], callback : () => void) - requirePrivilegeEx(privileges : BasicPrivilege | BasicPrivilege[] | BasicPrivilege | BasicPrivilege[], callback : () => void) - {/* - requirePrivilege(privileges, this, resource, (e, can) => { - if(e) - { - this.setCode(HTTPCodes.InternalServerError); - this.exit(); - return; - } - - if(!can) - { - this.setCode(HTTPCodes.Unauthorized); - this.exit(); - return; - } - - callback(); - });*/ - callback(); - } - - requirePrivilege(privileges : BasicPrivilege | BasicPrivilege[], callback : (error : Error, can : boolean) => void) - requirePrivilege(privileges : string | string[], callback : (error : Error, can : boolean) => void) - requirePrivilege(privileges : BasicPrivilege | BasicPrivilege[] | BasicPrivilege | BasicPrivilege[], callback : (error : Error, can : boolean) => void) - { - //requirePrivilege(privileges, this, resource, callback); - callback(null, true); - } - askForAuthentication(checkForUser : boolean, callback : (error : Error) => void) { if(checkForUser && this.user !== null && !this.user.isDefaultUser) @@ -406,5 +372,38 @@ export class RequestContext this.response.statusMessage = message; } } + defaultStatusCode(error : Error) : number + { + let code = null; + + if(error === Errors.ResourceNotFound) + code = HTTPCodes.NotFound; + else if(error === Errors.Locked) + code = HTTPCodes.Locked; + else if(error === Errors.BadAuthentication) + code = HTTPCodes.Unauthorized; + else if(error === Errors.NotEnoughPrivilege) + code = HTTPCodes.Unauthorized; + else if(error === Errors.ResourceAlreadyExists) + code = HTTPCodes.Conflict; + else if(error === Errors.IntermediateResourceMissing) + code = HTTPCodes.Conflict; + else if(error === Errors.WrongParentTypeForCreation) + code = HTTPCodes.Conflict; + else if(error === Errors.InsufficientStorage) + code = HTTPCodes.InsufficientStorage; + + return code; + } + setCodeFromError(error : Error) : boolean + { + const code = this.defaultStatusCode(error); + + if(!code) + return false; + + this.setCode(code); + return true; + } } export default RequestContext;