-
Notifications
You must be signed in to change notification settings - Fork 0
/
entitlement.js
109 lines (88 loc) · 2.38 KB
/
entitlement.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"use strict";
var _entitlements = {};
var _roles = {};
var _accounts = {};
var RootEntitlement = {
"toString" : function() { return this.description ? this.id + ": " + this.description['en'] : this.id; },
"hasEntitlement" : function(ent) { return this === ent; }
};
function Entitlement(id,options)
{
var defs = { id: {value: id, enumerable: true }};
var key;
for(key in options) {
if(options.hasOwnProperty(key)) {
defs[key] = {value: options[key], enumerable: true };
}
}
return Object.create(RootEntitlement, defs);
}
var RootRole = {
"toString" : function() { return this.id + ': ' + Object.keys(this.rights).join(','); },
"hasEntitlement" : function(ent) { return this.rights[ent]; }
};
function Role(id,rights)
{
var rs = {};
for(var i in rights) {
var e = rights[i];
rs[e] = _entitlements[e];
if(!rs[e]) rs[e] = _entitlements[e] = Entitlement(e);
}
var defs = {
id: { value: id, enumerable: true },
rights: { value: rs, enumerable: true }};
return Object.create(RootRole, defs);
}
var RootAccount = {
"toString" : function() { return this.id + ': ' + Object.keys(this.roles).join(','); },
"hasEntitlement" : function(entitlement) {
for(var i in this.roles) {
if(this.roles[i].hasEntitlement(entitlement))
return true;
}
return false;
}
};
function Account(id,options)
{
var rs = {};
for(var i in options.roles) {
var e = options.roles[i];
rs[e] = _roles[e];
if(!rs[e]) rs[e] = _roles[e] = Role(e);
}
var defs = {
id: { value: id, enumerable: true },
roles: { value: rs, enumerable: true }};
return Object.create(RootAccount, defs);
}
exports.registerEntitlements = function(ents)
{
for(var i in ents) {
_entitlements[i] = Entitlement(i, ents[i]);
}
}
exports.registerRoles = function(roles)
{
for(var i in roles) {
//console.log("registerRoles: " + i + " " + JSON.stringify(roles[i]));
_roles[i] = Role(i, roles[i]);
}
}
exports.registerAccounts = function(accounts)
{
for(var i in accounts) {
_accounts[i] = Account(i, accounts[i]);
}
}
exports.accountHasEntitlement = function(account,entitlement)
{
var e = _entitlements[entitlement];
var a = _accounts[account];
//console.log("accountHasEntitlement: " + a + "," + e + " -> " + (a && a.hasEntitlement(e) ? true : false));
return a && a.hasEntitlement(e) ? true : false;
}
exports.Entitlement = Entitlement;
exports.Role = Role;
exports.Account = Account;