Skip to content

Commit

Permalink
Fixed the 'SimplePathPrivilegeManager' implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed Jun 29, 2017
1 parent 008af5c commit 489cf28
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/user/v2/privilege/SimplePathPrivilegeManager.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ export declare class SimplePathPrivilegeManager extends PrivilegeManager {
constructor();
setRights(user: IUser, path: string, rights: BasicPrivilege[] | string[]): void;
getRights(user: IUser, path: string): string[];
_can(fuullPath: Path, resource: Resource, privilege: BasicPrivilege | string, callback: PrivilegeManagerCallback): void;
_can(fullPath: Path, resource: Resource, privilege: BasicPrivilege | string, callback: PrivilegeManagerCallback): void;
}
30 changes: 25 additions & 5 deletions lib/user/v2/privilege/SimplePathPrivilegeManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var __extends = (this && this.__extends) || (function () {
Object.defineProperty(exports, "__esModule", { value: true });
var PrivilegeManager_1 = require("./PrivilegeManager");
var export_1 = require("../../../manager/v2/export");
var Errors_1 = require("../../../Errors");
function standarizePath(path) {
if (!path)
path = '/';
Expand All @@ -38,18 +39,37 @@ var SimplePathPrivilegeManager = (function (_super) {
return _this;
}
SimplePathPrivilegeManager.prototype.setRights = function (user, path, rights) {
if (!user)
throw Errors_1.Errors.IllegalArguments;
if (!this.rights[user.uid])
this.rights[user.uid] = {};
this.rights[user.uid][standarizePath(path)] = rights;
};
SimplePathPrivilegeManager.prototype.getRights = function (user, path) {
if (!this.rights[user.uid])
if (!user)
return [];
var allRights = this.rights[user.uid];
if (!allRights)
return [];
return this.rights[user.uid][standarizePath(path)];
path = standarizePath(path.toString());
var rights = {};
for (var superPath in allRights)
if (path.indexOf(superPath) === 0)
for (var _i = 0, _a = allRights[superPath]; _i < _a.length; _i++) {
var right = _a[_i];
rights[right] = true;
}
return Object.keys(rights);
};
SimplePathPrivilegeManager.prototype._can = function (fuullPath, resource, privilege, callback) {
var rights = this.getRights(resource.context.user, export_1.Path.toString());
callback(null, rights && (rights.indexOf('all') !== -1 || rights.some(function (r) { return r === 'all' || r === privilege; })));
SimplePathPrivilegeManager.prototype._can = function (fullPath, resource, privilege, callback) {
var user = resource.context.user;
if (!user)
return callback(null, false);
if (user.isAdministrator)
return callback(null, true);
var rights = this.getRights(user, export_1.Path.toString());
var can = !!rights && rights.some(function (r) { return r === 'all' || r === privilege; });
callback(null, can);
};
return SimplePathPrivilegeManager;
}(PrivilegeManager_1.PrivilegeManager));
Expand Down
33 changes: 28 additions & 5 deletions src/user/v2/privilege/SimplePathPrivilegeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BasicPrivilege, PrivilegeManager, PrivilegeManagerCallback } from './Pr
import { RequestContext } from '../../../server/v2/RequestContext'
import { Resource, Path } from '../../../manager/v2/export'
import { IUser } from '../IUser'
import { Errors } from '../../../Errors'

function standarizePath(path : string)
{
Expand Down Expand Up @@ -40,22 +41,44 @@ export class SimplePathPrivilegeManager extends PrivilegeManager

setRights(user : IUser, path : string, rights : BasicPrivilege[] | string[])
{
if(!user)
throw Errors.IllegalArguments;

if(!this.rights[user.uid])
this.rights[user.uid] = {};

this.rights[user.uid][standarizePath(path)] = rights;
}
getRights(user : IUser, path : string) : string[]
{
if(!this.rights[user.uid])
if(!user)
return [];

const allRights = this.rights[user.uid];
if(!allRights)
return [];

path = standarizePath(path.toString());

const rights = {};
for(const superPath in allRights)
if(path.indexOf(superPath) === 0)
for(const right of allRights[superPath])
rights[right] = true;

return this.rights[user.uid][standarizePath(path)];
return Object.keys(rights);
}

_can(fuullPath : Path, resource : Resource, privilege : BasicPrivilege | string, callback : PrivilegeManagerCallback) : void
_can(fullPath : Path, resource : Resource, privilege : BasicPrivilege | string, callback : PrivilegeManagerCallback) : void
{
const rights = this.getRights(resource.context.user, Path.toString());
callback(null, rights && (rights.indexOf('all') !== -1 || rights.some((r) => r === 'all' || r === privilege)));
const user = resource.context.user;
if(!user)
return callback(null, false);
if(user.isAdministrator)
return callback(null, true);

const rights = this.getRights(user, Path.toString());
const can = !!rights && rights.some((r) => r === 'all' || r === privilege);
callback(null, can);
}
}

0 comments on commit 489cf28

Please sign in to comment.