Skip to content

Commit 533f20e

Browse files
committed
feat(client:auth): implement Auth.hasRole
1 parent f350412 commit 533f20e

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

app/templates/client/components/auth(auth)/auth.module(js).js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
angular.module('<%= scriptAppName %>.auth', [
4+
'<%= scriptAppName %>.constants',
45
'ngCookies'<% if (filters.ngroute) { %>,
56
'ngRoute'<% } if (filters.uirouter) { %>,
67
'ui.router'<% } %>

app/templates/client/components/auth(auth)/auth.service(js).js

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
(function() {
44

5-
function AuthService($http, User, $cookies, $q) {
5+
function AuthService($http, $cookies, $q, appConfig, User) {
66
/**
77
* Return a callback or noop function
88
*
@@ -13,7 +13,8 @@
1313
return (angular.isFunction(cb)) ? cb : angular.noop;
1414
},
1515

16-
currentUser = {};
16+
currentUser = {},
17+
userRoles = appConfig.userRoles || [];
1718

1819
if ($cookies.get('token')) {
1920
currentUser = User.get();
@@ -108,7 +109,8 @@
108109
return currentUser;
109110
}
110111

111-
var value = (currentUser.hasOwnProperty('$promise')) ? currentUser.$promise : currentUser;
112+
var value = (currentUser.hasOwnProperty('$promise')) ?
113+
currentUser.$promise : currentUser;
112114
return $q.when(value)
113115
.then(function(user) {
114116
safeCb(callback)(user);
@@ -140,25 +142,43 @@
140142
},
141143

142144
/**
143-
* Check if a user is an admin
145+
* Check if a user has a specified role or higher
144146
* (synchronous|asynchronous)
145147
*
146-
* @param {Function|*} callback - optional, function(is)
148+
* @param {String} role - the role to check against
149+
* @param {Function|*} callback - optional, function(has)
147150
* @return {Bool|Promise}
148151
*/
149-
isAdmin: function(callback) {
150-
if (arguments.length === 0) {
151-
return currentUser.role === 'admin';
152+
hasRole: function(role, callback) {
153+
var hasRole = function(r, h) {
154+
return userRoles.indexOf(r) >= userRoles.indexOf(h);
155+
};
156+
157+
if (arguments.length < 2) {
158+
return hasRole(currentUser.role, role);
152159
}
153160

154161
return Auth.getCurrentUser(null)
155162
.then(function(user) {
156-
var is = user.role === 'admin';
157-
safeCb(callback)(is);
158-
return is;
163+
var has = (user.hasOwnProperty('role')) ?
164+
hasRole(user.role, role) : false;
165+
safeCb(callback)(has);
166+
return has;
159167
});
160168
},
161169

170+
/**
171+
* Check if a user is an admin
172+
* (synchronous|asynchronous)
173+
*
174+
* @param {Function|*} callback - optional, function(is)
175+
* @return {Bool|Promise}
176+
*/
177+
isAdmin: function() {
178+
return Auth.hasRole
179+
.apply(Auth, [].concat.apply(['admin'], arguments));
180+
},
181+
162182
/**
163183
* Get auth token
164184
*

0 commit comments

Comments
 (0)