Skip to content

Commit 8908c87

Browse files
committed
Added the user as argument in the privilege managers
1 parent 6d41b1b commit 8908c87

16 files changed

+90
-87
lines changed

lib/user/v2/IUser.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export interface IUser {
22
uid: string;
3-
isAdministrator: boolean;
4-
isDefaultUser: boolean;
5-
password: string;
3+
isAdministrator?: boolean;
4+
isDefaultUser?: boolean;
65
username: string;
6+
password?: string;
77
}

lib/user/v2/authentication/HTTPAuthentication.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ export interface HTTPAuthentication {
44
askForAuthentication(): {
55
[headeName: string]: string;
66
};
7-
getUser(arg: HTTPRequestContext, callback: (error: Error, user?: IUser) => void): void;
7+
getUser(ctx: HTTPRequestContext, callback: (error: Error, user?: IUser) => void): void;
88
}

lib/user/v2/authentication/HTTPBasicAuthentication.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ export declare class HTTPBasicAuthentication implements HTTPAuthentication {
99
askForAuthentication(): {
1010
'WWW-Authenticate': string;
1111
};
12-
getUser(arg: HTTPRequestContext, callback: (error: Error, user: IUser) => void): void;
12+
getUser(ctx: HTTPRequestContext, callback: (error: Error, user: IUser) => void): void;
1313
}

lib/user/v2/authentication/HTTPBasicAuthentication.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ var HTTPBasicAuthentication = (function () {
1212
'WWW-Authenticate': 'Basic realm="' + this.realm + '"'
1313
};
1414
};
15-
HTTPBasicAuthentication.prototype.getUser = function (arg, callback) {
15+
HTTPBasicAuthentication.prototype.getUser = function (ctx, callback) {
1616
var _this = this;
1717
var onError = function (error) {
1818
_this.userManager.getDefaultUser(function (defaultUser) {
1919
callback(error, defaultUser);
2020
});
2121
};
22-
var authHeader = arg.headers.find('Authorization');
22+
var authHeader = ctx.headers.find('Authorization');
2323
if (!authHeader) {
2424
onError(Errors_1.Errors.MissingAuthorisationHeader);
2525
return;

lib/user/v2/authentication/HTTPDigestAuthentication.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ export declare class HTTPDigestAuthentication implements HTTPAuthentication {
1111
askForAuthentication(): {
1212
'WWW-Authenticate': string;
1313
};
14-
getUser(arg: HTTPRequestContext, callback: (error: Error, user: IUser) => void): void;
14+
getUser(ctx: HTTPRequestContext, callback: (error: Error, user: IUser) => void): void;
1515
}

lib/user/v2/authentication/HTTPDigestAuthentication.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ var HTTPDigestAuthentication = (function () {
2424
'WWW-Authenticate': 'Digest realm="' + this.realm + '", qop="auth", nonce="' + this.generateNonce() + '", opaque="' + this.generateNonce() + '"'
2525
};
2626
};
27-
HTTPDigestAuthentication.prototype.getUser = function (arg, callback) {
27+
HTTPDigestAuthentication.prototype.getUser = function (ctx, callback) {
2828
var _this = this;
2929
var onError = function (error) {
3030
_this.userManager.getDefaultUser(function (defaultUser) {
3131
callback(error, defaultUser);
3232
});
3333
};
34-
var authHeader = arg.headers.find('Authorization');
34+
var authHeader = ctx.headers.find('Authorization');
3535
if (!authHeader)
3636
return onError(Errors_1.Errors.MissingAuthorisationHeader);
3737
if (!/^Digest (\s*[a-zA-Z]+\s*=\s*(("(\\"|[^"])+")|([^,\s]+))?\s*(,|$))+$/.test(authHeader))
@@ -56,9 +56,9 @@ var HTTPDigestAuthentication = (function () {
5656
ha1 = md5(ha1 + ':' + authProps.nonce + ':' + authProps.cnonce);
5757
var ha2;
5858
if (authProps.qop === 'auth-int')
59-
return onError(Errors_1.Errors.WrongHeaderFormat); // ha2 = md5(arg.request.method.toString().toUpperCase() + ':' + arg.requested.uri + ':' + md5(...));
59+
return onError(Errors_1.Errors.WrongHeaderFormat); // ha2 = md5(ctx.request.method.toString().toUpperCase() + ':' + ctx.requested.uri + ':' + md5(...));
6060
else
61-
ha2 = md5(arg.request.method.toString().toUpperCase() + ':' + arg.requested.uri);
61+
ha2 = md5(ctx.request.method.toString().toUpperCase() + ':' + ctx.requested.uri);
6262
var result;
6363
if (authProps.qop === 'auth-int' || authProps.qop === 'auth')
6464
result = md5(ha1 + ':' + authProps.nonce + ':' + authProps.nc + ':' + authProps.cnonce + ':' + authProps.qop + ':' + ha2);
Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
import { Resource, Path } from '../../../manager/v2/export';
2+
import { IUser } from '../IUser';
23
export declare type PrivilegeManagerCallback = (error: Error, hasAccess: boolean) => void;
3-
export declare type PrivilegeManagerMethod = (fullPath: Path, resource: Resource, callback: PrivilegeManagerCallback) => void;
4+
export declare type PrivilegeManagerMethod = (fullPath: Path, user: IUser, resource: Resource, callback: PrivilegeManagerCallback) => void;
45
export declare type BasicPrivilege = 'canWrite' | 'canWriteLocks' | 'canWriteContent' | 'canWriteContentTranslated' | 'canWriteContentSource' | 'canWriteProperties' | 'canRead' | 'canReadLocks' | 'canReadContent' | 'canReadContentTranslated' | 'canReadContentSource' | 'canReadProperties';
56
export declare class PrivilegeManager {
67
can(fullPath: Path | string, resource: Resource, privilege: BasicPrivilege, callback: PrivilegeManagerCallback): void;
78
can(fullPath: Path | string, resource: Resource, privilege: string, callback: PrivilegeManagerCallback): void;
89
can(fullPath: Path | string, resource: Resource, privilege: BasicPrivilege[], callback: PrivilegeManagerCallback): void;
910
can(fullPath: Path | string, resource: Resource, privilege: string[], callback: PrivilegeManagerCallback): void;
10-
protected _can?(fullPath: Path, resource: Resource, privilege: string, callback: PrivilegeManagerCallback): void;
11-
protected canWrite(fullPath: Path, resource: Resource, callback: PrivilegeManagerCallback): void;
12-
protected canWriteLocks(fullPath: Path, resource: Resource, callback: PrivilegeManagerCallback): void;
13-
protected canWriteContent(fullPath: Path, resource: Resource, callback: PrivilegeManagerCallback): void;
14-
protected canWriteContentTranslated(fullPath: Path, resource: Resource, callback: PrivilegeManagerCallback): void;
15-
protected canWriteContentSource(fullPath: Path, resource: Resource, callback: PrivilegeManagerCallback): void;
16-
protected canWriteProperties(fullPath: Path, resource: Resource, callback: PrivilegeManagerCallback): void;
17-
protected canRead(fullPath: Path, resource: Resource, callback: PrivilegeManagerCallback): void;
18-
protected canReadLocks(fullPath: Path, resource: Resource, callback: PrivilegeManagerCallback): void;
19-
protected canReadContent(fullPath: Path, resource: Resource, callback: PrivilegeManagerCallback): void;
20-
protected canReadContentTranslated(fullPath: Path, resource: Resource, callback: PrivilegeManagerCallback): void;
21-
protected canReadContentSource(fullPath: Path, resource: Resource, callback: PrivilegeManagerCallback): void;
22-
protected canReadProperties(fullPath: Path, resource: Resource, callback: PrivilegeManagerCallback): void;
11+
protected _can?(fullPath: Path, user: IUser, resource: Resource, privilege: string, callback: PrivilegeManagerCallback): void;
12+
protected canWrite(fullPath: Path, user: IUser, resource: Resource, callback: PrivilegeManagerCallback): void;
13+
protected canWriteLocks(fullPath: Path, user: IUser, resource: Resource, callback: PrivilegeManagerCallback): void;
14+
protected canWriteContent(fullPath: Path, user: IUser, resource: Resource, callback: PrivilegeManagerCallback): void;
15+
protected canWriteContentTranslated(fullPath: Path, user: IUser, resource: Resource, callback: PrivilegeManagerCallback): void;
16+
protected canWriteContentSource(fullPath: Path, user: IUser, resource: Resource, callback: PrivilegeManagerCallback): void;
17+
protected canWriteProperties(fullPath: Path, user: IUser, resource: Resource, callback: PrivilegeManagerCallback): void;
18+
protected canRead(fullPath: Path, user: IUser, resource: Resource, callback: PrivilegeManagerCallback): void;
19+
protected canReadLocks(fullPath: Path, user: IUser, resource: Resource, callback: PrivilegeManagerCallback): void;
20+
protected canReadContent(fullPath: Path, user: IUser, resource: Resource, callback: PrivilegeManagerCallback): void;
21+
protected canReadContentTranslated(fullPath: Path, user: IUser, resource: Resource, callback: PrivilegeManagerCallback): void;
22+
protected canReadContentSource(fullPath: Path, user: IUser, resource: Resource, callback: PrivilegeManagerCallback): void;
23+
protected canReadProperties(fullPath: Path, user: IUser, resource: Resource, callback: PrivilegeManagerCallback): void;
2324
}

lib/user/v2/privilege/PrivilegeManager.js

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
Object.defineProperty(exports, "__esModule", { value: true });
33
var export_1 = require("../../../manager/v2/export");
44
var Workflow_1 = require("../../../helper/Workflow");
5-
function checkAll(pm, fns, fullPath, resource, callback) {
5+
function checkAll(pm, fns, fullPath, user, resource, callback) {
66
new Workflow_1.Workflow()
7-
.each(fns, function (fn, cb) { return fn.bind(pm)(fullPath, resource, cb); })
7+
.each(fns, function (fn, cb) { return fn.bind(pm)(fullPath, user, resource, cb); })
88
.error(function (e) { return callback(e, false); })
99
.done(function (successes) { return callback(null, successes.every(function (b) { return !!b; })); });
1010
}
@@ -13,6 +13,9 @@ var PrivilegeManager = (function () {
1313
}
1414
PrivilegeManager.prototype.can = function (_fullPath, resource, _privilege, callback) {
1515
var _this = this;
16+
var user = resource.context.user;
17+
if (user && user.isAdministrator)
18+
return callback(null, true);
1619
if (_privilege.constructor !== String) {
1720
new Workflow_1.Workflow()
1821
.each(_privilege, function (privilege, cb) { return _this.can(_fullPath, resource, privilege, cb); })
@@ -23,61 +26,61 @@ var PrivilegeManager = (function () {
2326
var fullPath = new export_1.Path(_fullPath);
2427
var privilege = _privilege;
2528
if (this._can)
26-
return this._can(fullPath, resource, privilege, callback);
29+
return this._can(fullPath, user, resource, privilege, callback);
2730
var method = this[privilege];
2831
if (method)
29-
method.bind(this)(fullPath, resource, callback);
32+
method.bind(this)(fullPath, user, resource, callback);
3033
else
3134
callback(null, true);
3235
};
33-
PrivilegeManager.prototype.canWrite = function (fullPath, resource, callback) {
36+
PrivilegeManager.prototype.canWrite = function (fullPath, user, resource, callback) {
3437
checkAll(this, [
3538
this.canWriteLocks,
3639
this.canWriteContent,
3740
this.canWriteProperties
38-
], fullPath, resource, callback);
41+
], fullPath, user, resource, callback);
3942
};
40-
PrivilegeManager.prototype.canWriteLocks = function (fullPath, resource, callback) {
43+
PrivilegeManager.prototype.canWriteLocks = function (fullPath, user, resource, callback) {
4144
callback(null, true);
4245
};
43-
PrivilegeManager.prototype.canWriteContent = function (fullPath, resource, callback) {
46+
PrivilegeManager.prototype.canWriteContent = function (fullPath, user, resource, callback) {
4447
checkAll(this, [
4548
this.canWriteContentSource,
4649
this.canWriteContentTranslated
47-
], fullPath, resource, callback);
50+
], fullPath, user, resource, callback);
4851
};
49-
PrivilegeManager.prototype.canWriteContentTranslated = function (fullPath, resource, callback) {
52+
PrivilegeManager.prototype.canWriteContentTranslated = function (fullPath, user, resource, callback) {
5053
callback(null, true);
5154
};
52-
PrivilegeManager.prototype.canWriteContentSource = function (fullPath, resource, callback) {
55+
PrivilegeManager.prototype.canWriteContentSource = function (fullPath, user, resource, callback) {
5356
callback(null, true);
5457
};
55-
PrivilegeManager.prototype.canWriteProperties = function (fullPath, resource, callback) {
58+
PrivilegeManager.prototype.canWriteProperties = function (fullPath, user, resource, callback) {
5659
callback(null, true);
5760
};
58-
PrivilegeManager.prototype.canRead = function (fullPath, resource, callback) {
61+
PrivilegeManager.prototype.canRead = function (fullPath, user, resource, callback) {
5962
checkAll(this, [
6063
this.canReadLocks,
6164
this.canReadContent,
6265
this.canReadProperties
63-
], fullPath, resource, callback);
66+
], fullPath, user, resource, callback);
6467
};
65-
PrivilegeManager.prototype.canReadLocks = function (fullPath, resource, callback) {
68+
PrivilegeManager.prototype.canReadLocks = function (fullPath, user, resource, callback) {
6669
callback(null, true);
6770
};
68-
PrivilegeManager.prototype.canReadContent = function (fullPath, resource, callback) {
71+
PrivilegeManager.prototype.canReadContent = function (fullPath, user, resource, callback) {
6972
checkAll(this, [
7073
this.canReadContentSource,
7174
this.canReadContentTranslated
72-
], fullPath, resource, callback);
75+
], fullPath, user, resource, callback);
7376
};
74-
PrivilegeManager.prototype.canReadContentTranslated = function (fullPath, resource, callback) {
77+
PrivilegeManager.prototype.canReadContentTranslated = function (fullPath, user, resource, callback) {
7578
callback(null, true);
7679
};
77-
PrivilegeManager.prototype.canReadContentSource = function (fullPath, resource, callback) {
80+
PrivilegeManager.prototype.canReadContentSource = function (fullPath, user, resource, callback) {
7881
callback(null, true);
7982
};
80-
PrivilegeManager.prototype.canReadProperties = function (fullPath, resource, callback) {
83+
PrivilegeManager.prototype.canReadProperties = function (fullPath, user, resource, callback) {
8184
callback(null, true);
8285
};
8386
return PrivilegeManager;

lib/user/v2/privilege/SimplePathPrivilegeManager.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ export declare class SimplePathPrivilegeManager extends PrivilegeManager {
66
constructor();
77
setRights(user: IUser, path: string, rights: BasicPrivilege[] | string[]): void;
88
getRights(user: IUser, path: string): string[];
9-
_can(fullPath: Path, resource: Resource, privilege: BasicPrivilege | string, callback: PrivilegeManagerCallback): void;
9+
_can(fullPath: Path, user: IUser, resource: Resource, privilege: BasicPrivilege | string, callback: PrivilegeManagerCallback): void;
1010
}

lib/user/v2/privilege/SimplePathPrivilegeManager.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,9 @@ var SimplePathPrivilegeManager = (function (_super) {
6161
}
6262
return Object.keys(rights);
6363
};
64-
SimplePathPrivilegeManager.prototype._can = function (fullPath, resource, privilege, callback) {
65-
var user = resource.context.user;
64+
SimplePathPrivilegeManager.prototype._can = function (fullPath, user, resource, privilege, callback) {
6665
if (!user)
6766
return callback(null, false);
68-
if (user.isAdministrator)
69-
return callback(null, true);
7067
var rights = this.getRights(user, export_1.Path.toString());
7168
var can = !!rights && rights.some(function (r) { return r === 'all' || r === privilege; });
7269
callback(null, can);

0 commit comments

Comments
 (0)