Skip to content

Commit

Permalink
Allow to override the privilege checking in the context to allow spec…
Browse files Browse the repository at this point in the history
…ial operations (list all locks, for instance)
  • Loading branch information
AdrienCastex committed Jul 3, 2017
1 parent 2023611 commit b96fde6
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/server/v2/RequestContext.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export declare class DefaultRequestContextExternalOptions implements RequestCont
user: IUser;
}
export declare class RequestContext {
overridePrivileges: boolean;
requested: RequestedResource;
headers: RequestContextHeaders;
server: WebDAVServer;
Expand Down
1 change: 1 addition & 0 deletions lib/server/v2/RequestContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ var DefaultRequestContextExternalOptions = (function () {
exports.DefaultRequestContextExternalOptions = DefaultRequestContextExternalOptions;
var RequestContext = (function () {
function RequestContext(server, uri, headers) {
this.overridePrivileges = false;
this.headers = new RequestContextHeaders(headers);
this.server = server;
uri = url.parse(uri).pathname;
Expand Down
3 changes: 3 additions & 0 deletions lib/server/v2/commands/Lock.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ function createLock(ctx, data, callback) {
var owner = ownerElement ? ownerElement.elements : null;
var lock_1 = new Lock_1.Lock(new LockKind_1.LockKind(scope, type_1, ctx.server.options.lockTimeout), ctx.user ? ctx.user.uid : undefined, owner, ctx.headers.depth === undefined ? -1 : ctx.headers.depth);
var go_1 = function (r, callback) {
ctx.overridePrivileges = true;
r.listDeepLocks(function (e, locks) {
ctx.overridePrivileges = false;
if (e)
return callback(e);
if (Object.keys(locks).length > 0) {
Expand Down Expand Up @@ -83,6 +85,7 @@ function createLock(ctx, data, callback) {
});
}
catch (ex) {
console.log(ex);
ctx.setCode(WebDAVRequest_1.HTTPCodes.BadRequest);
callback();
return;
Expand Down
2 changes: 1 addition & 1 deletion lib/user/v2/privilege/PrivilegeManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var PrivilegeManager = (function () {
PrivilegeManager.prototype.can = function (_fullPath, resource, _privilege, callback) {
var _this = this;
var user = resource.context.user;
if (user && user.isAdministrator)
if (resource.context.overridePrivileges || user && user.isAdministrator)
return callback(null, true);
if (_privilege.constructor !== String) {
new Workflow_1.Workflow()
Expand Down
2 changes: 2 additions & 0 deletions src/server/v2/RequestContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,15 @@ export class DefaultRequestContextExternalOptions implements RequestContextExter

export class RequestContext
{
overridePrivileges : boolean
requested : RequestedResource
headers : RequestContextHeaders
server : WebDAVServer
user : IUser

protected constructor(server : WebDAVServer, uri : string, headers : { [name : string] : string })
{
this.overridePrivileges = false;
this.headers = new RequestContextHeaders(headers);
this.server = server;

Expand Down
4 changes: 4 additions & 0 deletions src/server/v2/commands/Lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { LockType } from '../../../resource/lock/LockType'
import { Errors } from '../../../Errors'
import { Lock } from '../../../resource/lock/Lock'
import { XML } from '../../../helper/XML'
import { IUser } from '../../../user/v2/IUser'
import * as path from 'path'

function createResponse(ctx : HTTPRequestContext, lock : Lock)
Expand Down Expand Up @@ -44,7 +45,9 @@ function createLock(ctx : HTTPRequestContext, data : Buffer, callback)

const go = (r : Resource, callback : SimpleCallback) =>
{
ctx.overridePrivileges = true;
r.listDeepLocks((e, locks) => {
ctx.overridePrivileges = false;
if(e)
return callback(e);

Expand Down Expand Up @@ -105,6 +108,7 @@ function createLock(ctx : HTTPRequestContext, data : Buffer, callback)
}
catch(ex)
{
console.log(ex);
ctx.setCode(HTTPCodes.BadRequest);
callback();
return;
Expand Down
2 changes: 1 addition & 1 deletion src/user/v2/privilege/PrivilegeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class PrivilegeManager
can(_fullPath : Path | string, resource : Resource, _privilege : BasicPrivilege | string | BasicPrivilege[] | string[], callback : PrivilegeManagerCallback) : void
{
const user = resource.context.user;
if(user && user.isAdministrator)
if(resource.context.overridePrivileges || user && user.isAdministrator)
return callback(null, true);

if(_privilege.constructor !== String)
Expand Down

0 comments on commit b96fde6

Please sign in to comment.