Skip to content

Commit

Permalink
possibility to change app/role separator string
Browse files Browse the repository at this point in the history
  • Loading branch information
dresende committed Sep 23, 2011
1 parent 3560d02 commit 67c6759
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions lib/roles.js
Expand Up @@ -3,9 +3,16 @@
*
* User -> Profile -> Roles <- Application
**/

// CONSTANTS
var APP_ROLE_SEPARATOR = ".";

// INCLUDES
var util = require("util"),
events = require("events"),
applications = {},
events = require("events");

// VARIABLES
var applications = {},
profiles = {},
users = {},
monitor;
Expand Down Expand Up @@ -61,11 +68,11 @@ Profile.prototype.addRoles = function () {
for (var i = 0; i < arguments.length; i++) {
var role = arguments[i];

if (role.indexOf(".") <= 0) {
if (role.indexOf(APP_ROLE_SEPARATOR) <= 0) {
throw new Error("A role must have an associated application");
}

role = role.split(".", 2);
role = role.split(APP_ROLE_SEPARATOR, 2);

if (!applications.hasOwnProperty(role[0])) {
throw new Error("Application not found");
Expand Down Expand Up @@ -100,11 +107,11 @@ Profile.prototype.removeRoles = function () {
for (var i = 0; i < arguments.length; i++) {
var role = arguments[i];

if (role.indexOf(".") <= 0) {
if (role.indexOf(APP_ROLE_SEPARATOR) <= 0) {
throw new Error("A role must have an associated application");
}

role = role.split(".", 2);
role = role.split(APP_ROLE_SEPARATOR, 2);

if (!applications.hasOwnProperty(role[0])) {
throw new Error("Application not found");
Expand Down Expand Up @@ -140,15 +147,15 @@ Profile.prototype.hasRoles = function () {
for (var i = 0; i < arguments.length; i++) {
var role = arguments[i];

if (role.indexOf(".") <= 0) return false;
if (role.indexOf(APP_ROLE_SEPARATOR) <= 0) return false;

role = role.split(".", 2);
role = role.split(APP_ROLE_SEPARATOR, 2);

if (!applications.hasOwnProperty(role[0])) return false;

if (role[1] == "*") {
for (var j = 0; j < applications[role[0]].roles.length; j++) {
if (this.roles.indexOf(role[0] + "." + applications[role[0]].roles[j]) == -1) return false;
if (this.roles.indexOf(role[0] + APP_ROLE_SEPARATOR + applications[role[0]].roles[j]) == -1) return false;
}
} else {
if (applications[role[0]].roles.indexOf(role[1]) == -1) {
Expand All @@ -158,7 +165,7 @@ Profile.prototype.hasRoles = function () {
monitor.emit("role.invalid", role[0], role[1]);
}
}
if (this.roles.indexOf(role.join(".")) == -1) return false;
if (this.roles.indexOf(role.join(APP_ROLE_SEPARATOR)) == -1) return false;
}
}

Expand All @@ -168,15 +175,15 @@ Profile.prototype.hasAnyRoles = function () {
for (var i = 0; i < arguments.length; i++) {
var role = arguments[i];

if (role.indexOf(".") <= 0) continue;
if (role.indexOf(APP_ROLE_SEPARATOR) <= 0) continue;

role = role.split(".", 2);
role = role.split(APP_ROLE_SEPARATOR, 2);

if (!applications.hasOwnProperty(role[0])) continue;

if (role[1] == "*") {
for (var j = 0; j < applications[role[0]].roles.length; j++) {
if (this.roles.indexOf(role[0] + "." + applications[role[0]].roles[j]) != -1) return true;
if (this.roles.indexOf(role[0] + APP_ROLE_SEPARATOR + applications[role[0]].roles[j]) != -1) return true;
}
} else {
if (applications[role[0]].roles.indexOf(role[1]) == -1) {
Expand All @@ -186,22 +193,22 @@ Profile.prototype.hasAnyRoles = function () {
monitor.emit("role.invalid", role[0], role[1]);
}
}
if (this.roles.indexOf(role.join(".")) != -1) return true;
if (this.roles.indexOf(role.join(APP_ROLE_SEPARATOR)) != -1) return true;
}
}

return false;
};
Profile.prototype.__addRole = function (app, role) {
var app_role = app + "." + role;
var app_role = app + APP_ROLE_SEPARATOR + role;

if (this.roles.indexOf(app_role) == -1) {
this.roles.push(app_role);
monitor.emit("profile.addrole", this.name, app, role);
}
};
Profile.prototype.__removeRole = function (app, role) {
var app_role = app + "." + role;
var app_role = app + APP_ROLE_SEPARATOR + role;

if (this.roles.indexOf(app_role) != -1) {
this.roles.splice(this.roles.indexOf(app_role), 1);
Expand All @@ -210,15 +217,15 @@ Profile.prototype.__removeRole = function (app, role) {
};
Profile.prototype.__watchNewRoles = function (profile, app) {
return function (role) {
var app_role = app + "." + role;
var app_role = app + APP_ROLE_SEPARATOR + role;
if (profile.roles.indexOf(app_role) == -1) {
profile.roles.push(app_role);
}
};
};
Profile.prototype.__watchRemoveRoles = function (profile, app) {
return function (role) {
var app_role = app + "." + role;
var app_role = app + APP_ROLE_SEPARATOR + role;
if (profile.roles.indexOf(app_role) != -1) {
profile.roles.splice(profile.roles.indexOf(app_role), 1);
}
Expand Down Expand Up @@ -313,3 +320,8 @@ exports.import = function (data) {
exports.getMonitor = function () {
return monitor;
};
exports.setSeparator = function (sep) {
APP_ROLE_SEPARATOR = sep;

return this;
};

0 comments on commit 67c6759

Please sign in to comment.