-
Notifications
You must be signed in to change notification settings - Fork 49
/
angular-acl.js
339 lines (306 loc) · 7.55 KB
/
angular-acl.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
'use strict';
var NG_HIDE_CLASS = 'ng-hide';
angular.module('mm.acl', []);
angular.module('mm.acl').provider('AclService', [
function () {
/**
* Polyfill for IE8
*
* http://stackoverflow.com/a/1181586
*/
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (needle) {
var l = this.length;
for (; l--;) {
if (this[l] === needle) {
return l;
}
}
return -1;
};
}
var config = {
storage: 'sessionStorage',
storageKey: 'AclService'
};
var data = {
roles: [],
abilities: {}
};
/**
* Does the given role have abilities granted to it?
*
* @param role
* @returns {boolean}
*/
var roleHasAbilities = function (role) {
return (typeof data.abilities[role] === 'object');
};
/**
* Retrieve the abilities array for the given role
*
* @param role
* @returns {Array}
*/
var getRoleAbilities = function (role) {
return (roleHasAbilities(role)) ? data.abilities[role] : [];
};
/**
* Persist data to storage based on config
*/
var save = function () {
switch (config.storage) {
case 'sessionStorage':
saveToStorage('sessionStorage');
break;
case 'localStorage':
saveToStorage('localStorage');
break;
default:
// Don't save
return;
}
};
var unset = function () {
switch (config.storage) {
case 'sessionStorage':
unsetFromStorage('sessionStorage');
break;
case 'localStorage':
unsetFromStorage('localStorage');
break;
default:
// Don't save
return;
}
};
/**
* Persist data to web storage
*/
var saveToStorage = function (storagetype) {
window[storagetype].setItem(config.storageKey, JSON.stringify(data));
};
/**
* Unset data from web storage
*/
var unsetFromStorage = function (storagetype) {
window[storagetype].removeItem(config.storageKey);
};
/**
* Retrieve data from web storage
*/
var fetchFromStorage = function (storagetype) {
var data = window[storagetype].getItem(config.storageKey);
return (data) ? JSON.parse(data) : false;
};
var AclService = {};
AclService.resume = resume;
/**
* Restore data from web storage.
*
* Returns true if web storage exists and false if it doesn't.
*
* @returns {boolean}
*/
function resume() {
var storedData;
switch (config.storage) {
case 'sessionStorage':
storedData = fetchFromStorage('sessionStorage');
break;
case 'localStorage':
storedData = fetchFromStorage('localStorage');
break;
default:
storedData = null;
}
if (storedData) {
angular.extend(data, storedData);
return true;
}
return false;
}
/**
* Remove data from web storage
*/
AclService.flushStorage = function () {
unset();
};
/**
* Attach a role to the current user
*
* @param role
*/
AclService.attachRole = function (role) {
if (data.roles.indexOf(role) === -1) {
data.roles.push(role);
save();
}
};
/**
* Remove role from current user
*
* @param role
*/
AclService.detachRole = function (role) {
var i = data.roles.indexOf(role);
if (i > -1) {
data.roles.splice(i, 1);
save();
}
};
/**
* Remove all roles from current user
*/
AclService.flushRoles = function () {
data.roles = [];
save();
};
/**
* Check if the current user has role(s) attached
*
* @param role
* @returns {boolean}
*/
AclService.hasRole = function (role) {
var roles = angular.isArray(role) ? role : [role];
for (var l = roles.length; l--;) {
if (data.roles.indexOf(roles[l]) === -1) {
return false;
}
}
return !!roles.length;
};
/**
* Check if the current user any of the given roles
*
* @param roles
* @returns {boolean}
*/
AclService.hasAnyRole = function (roles) {
for (var l = roles.length; l--;) {
if (AclService.hasRole(roles[l])) {
return true;
}
}
return false;
};
/**
* Returns the current user roles
* @returns {Array}
*/
AclService.getRoles = function () {
return data.roles;
};
/**
* Set the abilities object (overwriting previous abilities)
*
* Each property on the abilities object should be a role.
* Each role should have a value of an array. The array should contain
* a list of all of the roles abilities.
*
* Example:
*
* {
* guest: ['login'],
* user: ['logout', 'view_content'],
* admin: ['logout', 'view_content', 'manage_users']
* }
*
* @param abilities
*/
AclService.setAbilities = function (abilities) {
data.abilities = abilities;
save();
};
/**
* Add an ability to a role
*
* @param role
* @param ability
*/
AclService.addAbility = function (role, ability) {
if (!data.abilities[role]) {
data.abilities[role] = [];
}
data.abilities[role].push(ability);
save();
};
/**
* Does current user have permission to do something?
*
* @param ability
* @returns {boolean}
*/
AclService.can = function (ability) {
var role, abilities;
// Loop through roles
var l = data.roles.length;
for (; l--;) {
// Grab the the current role
role = data.roles[l];
abilities = getRoleAbilities(role);
if (abilities.indexOf(ability) > -1) {
// Ability is in role abilities
return true;
}
}
// We made it here, so the ability wasn't found in attached roles
return false;
};
/**
* Does current user have any of the required permission to do something?
*
* @param abilities [array]
* @returns {boolean}
*/
AclService.canAny = function (abilities) {
var role, roleAbilities;
// Loop through roles
var l = data.roles.length;
var j = abilities.length;
for (; l--;) {
// Grab the the current role
role = data.roles[l];
roleAbilities = getRoleAbilities(role);
for (; j--;) {
if (roleAbilities.indexOf(abilities[j]) > -1) {
// Ability is in role abilities
return true;
}
}
}
// We made it here, so the ability wasn't found in attached roles
return false;
};
return {
config: function (userConfig) {
angular.extend(config, userConfig);
},
resume: resume,
$get: function () {
return AclService;
}
};
}
]).directive('aclShow', function (AclService) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
scope.$watch(attrs.aclShow, function aclShowWatchAction(value) {
var permissions, can;
if (!value) {
element.addClass(NG_HIDE_CLASS);
return;
}
permissions = value.split(',');
can = AclService.canAny(permissions);
if (!can) {
element.addClass(NG_HIDE_CLASS);
} else {
element.removeClass(NG_HIDE_CLASS);
}
});
}
};
});