-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsecurity.js
64 lines (52 loc) · 2.18 KB
/
security.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
'use strict';
angular.module('app.security', [])
.factory('security', ['$http', '$q', function($http, $q) {
var that = this;
function populateUserAccessLevels(role){
service.currentUserAccessLevels = {};
_.each(securityConfig.accessLevels, function(value, key){
service.currentUserAccessLevels[key] = _.contains(value, role) || value === "*";
});
};
// The public API of the service
var service = {
// Information about the current user
currentUserAccessLevels: null,
currentUser: null,
authenticated: false,
authorize: function(accessLevelReq) {
if (_.isNull(service.currentUserAccessLevels)) {
populateUserAccessLevels('public');
}
var authenticated = false;
_.each(accessLevelReq, function(value){
if(service.currentUserAccessLevels[value]) {
authenticated = true;
}
});
return authenticated;
},
// Ask the backend to see if a user is already authenticated - this may be from a previous session.
requestCurrentUser: function() {
if (_.isNull(service.currentUserAccessLevels)) {
populateUserAccessLevels('public');
}
if ( service.isAuthenticated() ) {
return $q.when(service.currentUser);
} else {
return $http.get('/current-user').then(function(response) {
service.currentUser = response.data.user;
service.currentUser.role = 'user';
populateUserAccessLevels(service.currentUser.role);
return service.currentUser;
});
}
},
// Is the current user authenticated?
isAuthenticated: function(){
service.authenticated = !!service.currentUser;
return service.authenticated;
}
};
return service;
}]);