-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth_manager.js
174 lines (144 loc) · 4.51 KB
/
auth_manager.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Generated by CoffeeScript 1.8.0
/*
Auth item (permission or role)
*/
(function() {
var Item, Permission, Role,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
Item = (function() {
function Item(options) {
if (options == null) {
options = {};
}
this.type = options.type || null;
this.name = options.name || null;
this.description = options.description || null;
this.data = options.data || null;
}
return Item;
})();
/*
User role auth item
*/
Role = (function(_super) {
__extends(Role, _super);
function Role(options) {
if (options == null) {
options = {};
}
Role.__super__.constructor.call(this, options);
this.type = RbacManager.TYPE_ROLE;
}
return Role;
})(Item);
/*
Access permission
*/
Permission = (function(_super) {
__extends(Permission, _super);
function Permission(options) {
if (options == null) {
options = {};
}
Permission.__super__.constructor.call(this, options);
this.type = RbacManager.TYPE_PERMISSION;
this.rule = options.rule || null;
}
return Permission;
})(Item);
/*
Role base access control manager class
*/
this.RbacManager = (function() {
RbacManager.TYPE_ROLE = 1;
RbacManager.TYPE_PERMISSION = 2;
function RbacManager(options) {
if (options == null) {
options = {};
}
this.defaultRoles = options.defaultRoles || [];
this.items = {};
this.children = {};
this.checkAccessRecursive = function(user, permissionName, params, assignments) {
var fn, item, result, self;
if (!this.items[permissionName]) {
return false;
}
item = this.items[permissionName];
if (!this.executeRule(item, user, params)) {
return false;
}
if (_.contains(assignments, permissionName) || _.contains(this.defaultRoles, permissionName)) {
return true;
}
self = this;
fn = this.checkAccessRecursive;
result = false;
_.each(this.children, function(children, parentName) {
if (children[permissionName] && fn.call(self, user, parentName, params, assignments)) {
return result = true;
}
});
return result;
};
this.addItem = function(item) {
return this.items[item.name] = item;
};
this.executeRule = function(item, user, params) {
if (item instanceof Role) {
return true;
}
if (item.rule) {
return item.rule.call(this, user, params);
} else {
return true;
}
};
}
RbacManager.prototype.createRole = function(roleName) {
var role;
role = new Role();
role.name = roleName;
this.addItem(role);
return role;
};
RbacManager.prototype.createPermission = function(permissionName, ruleCallback) {
var permission;
if (ruleCallback == null) {
ruleCallback = null;
}
permission = new Permission();
permission.name = permissionName;
permission.rule = ruleCallback;
this.addItem(permission);
return permission;
};
RbacManager.prototype.addChild = function(parent, child) {
if (!this.items[parent.name] || !this.items[child.name]) {
throw new Error("Either " + parent.name + " or " + child.name + " does not exist.");
}
if (parent.name === child.name) {
throw new Error("Cannot add " + parent.name + " as a child of itself.");
}
if (parent instanceof Permission && child instanceof Role) {
throw new Error("Cannot add a role as a child of a permission.");
}
if (!this.children[parent.name]) {
this.children[parent.name] = {};
}
return this.children[parent.name][child.name] = this.items[child.name];
};
RbacManager.prototype.checkAccess = function(permissionName, params) {
var assignments, user;
user = Meteor.user();
assignments = [];
if (user) {
assignments = user.roles;
}
return this.checkAccessRecursive(user, permissionName, params, assignments);
};
return RbacManager;
})();
}).call(this);
//# sourceMappingURL=auth_manager.js.map