@@ -6,8 +6,16 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
6
6
var safeCb = Util . safeCb ;
7
7
var currentUser = { } ;
8
8
var userRoles = appConfig . userRoles || [ ] ;
9
+ /**
10
+ * Check if userRole is >= role
11
+ * @param {String } userRole - role of current user
12
+ * @param {String } role - role to check against
13
+ */
14
+ var hasRole = function ( userRole , role ) {
15
+ return userRoles . indexOf ( userRole ) >= userRoles . indexOf ( role ) ;
16
+ } ;
9
17
10
- if ( $cookies . get ( 'token' ) && $location . path ( ) !== '/logout' ) {
18
+ if ( $cookies . get ( 'token' ) && $location . path ( ) !== '/logout' ) {
11
19
currentUser = User . get ( ) ;
12
20
}
13
21
@@ -90,18 +98,15 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
90
98
91
99
/**
92
100
* Gets all available info on a user
93
- * (synchronous|asynchronous)
94
101
*
95
- * @param {Function|* } callback - optional, funciton(user)
96
- * @return {Object| Promise }
102
+ * @param {Function } [ callback] - funciton(user)
103
+ * @return {Promise }
97
104
*/
98
105
getCurrentUser ( callback ) {
99
- if ( arguments . length === 0 ) {
100
- return currentUser ;
101
- }
106
+ var value = currentUser . hasOwnProperty ( '$promise' )
107
+ ? currentUser . $promise
108
+ : currentUser ;
102
109
103
- var value = ( currentUser . hasOwnProperty ( '$promise' ) ) ?
104
- currentUser . $promise : currentUser ;
105
110
return $q . when ( value )
106
111
. then ( user => {
107
112
safeCb ( callback ) ( user ) ;
@@ -123,16 +128,11 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
123
128
124
129
/**
125
130
* Check if a user is logged in
126
- * (synchronous|asynchronous)
127
131
*
128
- * @param {Function|* } callback - optional, function(is)
132
+ * @param {Function } [ callback] - function(is)
129
133
* @return {Bool|Promise }
130
134
*/
131
135
isLoggedIn ( callback ) {
132
- if ( arguments . length === 0 ) {
133
- return currentUser . hasOwnProperty ( 'role' ) ;
134
- }
135
-
136
136
return Auth . getCurrentUser ( null )
137
137
. then ( user => {
138
138
var is = user . hasOwnProperty ( 'role' ) ;
@@ -152,25 +152,18 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
152
152
153
153
/**
154
154
* Check if a user has a specified role or higher
155
- * (synchronous|asynchronous)
156
155
*
157
156
* @param {String } role - the role to check against
158
- * @param {Function|* } callback - optional, function(has)
157
+ * @param {Function } [ callback] - function(has)
159
158
* @return {Bool|Promise }
160
159
*/
161
160
hasRole ( role , callback ) {
162
- var hasRole = function ( r , h ) {
163
- return userRoles . indexOf ( r ) >= userRoles . indexOf ( h ) ;
164
- } ;
165
-
166
- if ( arguments . length < 2 ) {
167
- return hasRole ( currentUser . role , role ) ;
168
- }
169
-
170
161
return Auth . getCurrentUser ( null )
171
162
. then ( user => {
172
- var has = ( user . hasOwnProperty ( 'role' ) ) ?
173
- hasRole ( user . role , role ) : false ;
163
+ var has = user . hasOwnProperty ( 'role' )
164
+ ? hasRole ( user . role , role )
165
+ : false ;
166
+
174
167
safeCb ( callback ) ( has ) ;
175
168
return has ;
176
169
} ) ;
@@ -183,10 +176,6 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
183
176
* @return {Bool }
184
177
*/
185
178
hasRoleSync ( role ) {
186
- var hasRole = function ( r , h ) {
187
- return userRoles . indexOf ( r ) >= userRoles . indexOf ( h ) ;
188
- } ;
189
-
190
179
return hasRole ( currentUser . role , role ) ;
191
180
} ,
192
181
0 commit comments