12
12
* Copyright (c) 2014-2019. All rights reserved.
13
13
*/
14
14
15
- var _ = require ( 'lodash' )
16
- var winston = require ( '../logger' )
17
- var roleSchema = require ( '../models/role' )
18
- var roleOrder = require ( '../models/roleorder' )
15
+ const _ = require ( 'lodash' )
16
+ const winston = require ( '../logger' )
17
+ const roleSchema = require ( '../models/role' )
18
+ const roleOrder = require ( '../models/roleorder' )
19
19
20
- var register = function ( callback ) {
20
+ const register = function ( callback ) {
21
21
// Register Roles
22
22
roleSchema . getRolesLean ( function ( err , roles ) {
23
23
if ( err ) return callback ( err )
@@ -42,23 +42,23 @@ var register = function (callback) {
42
42
* @returns {boolean }
43
43
*/
44
44
45
- var canThis = function ( role , a , adminOverride = false ) {
45
+ const canThis = function ( role , a , adminOverride = false ) {
46
46
if ( _ . isUndefined ( role ) ) return false
47
47
if ( adminOverride === true && role . isAdmin ) return true
48
48
49
- var roles = global . roles
49
+ const roles = global . roles
50
50
if ( _ . isUndefined ( roles ) ) return false
51
51
if ( _ . hasIn ( role , '_id' ) ) role = role . _id
52
- var rolePerm = _ . find ( roles , { _id : role } )
52
+ const rolePerm = _ . find ( roles , { _id : role } )
53
53
if ( _ . isUndefined ( rolePerm ) ) return false
54
54
if ( _ . indexOf ( rolePerm . grants , '*' ) !== - 1 ) return true
55
55
56
- var actionType = a . split ( ':' ) [ 0 ]
57
- var action = a . split ( ':' ) [ 1 ]
56
+ const actionType = a . split ( ':' ) [ 0 ]
57
+ const action = a . split ( ':' ) [ 1 ]
58
58
59
59
if ( _ . isUndefined ( actionType ) || _ . isUndefined ( action ) ) return false
60
60
61
- var result = _ . filter ( rolePerm . grants , function ( value ) {
61
+ const result = _ . filter ( rolePerm . grants , function ( value ) {
62
62
if ( _ . startsWith ( value , actionType + ':' ) ) return value
63
63
} )
64
64
@@ -67,32 +67,32 @@ var canThis = function (role, a, adminOverride = false) {
67
67
if ( result [ 0 ] === '*' ) return true
68
68
}
69
69
70
- var typePerm = result [ 0 ] . split ( ':' ) [ 1 ] . split ( ' ' )
70
+ let typePerm = result [ 0 ] . split ( ':' ) [ 1 ] . split ( ' ' )
71
71
typePerm = _ . uniq ( typePerm )
72
72
73
73
if ( _ . indexOf ( typePerm , '*' ) !== - 1 ) return true
74
74
75
75
return _ . indexOf ( typePerm , action ) !== - 1
76
76
}
77
77
78
- var getRoles = function ( action ) {
78
+ const getRoles = function ( action ) {
79
79
if ( _ . isUndefined ( action ) ) return false
80
80
81
- var rolesWithAction = [ ]
82
- var roles = global . roles
81
+ let rolesWithAction = [ ]
82
+ const roles = global . roles
83
83
if ( _ . isUndefined ( roles ) ) return [ ]
84
84
85
85
_ . each ( roles , function ( role ) {
86
- var actionType = action . split ( ':' ) [ 0 ]
87
- var theAction = action . split ( ':' ) [ 1 ]
86
+ const actionType = action . split ( ':' ) [ 0 ]
87
+ const theAction = action . split ( ':' ) [ 1 ]
88
88
89
89
if ( _ . isUndefined ( actionType ) || _ . isUndefined ( theAction ) ) return
90
90
if ( _ . indexOf ( role . grants , '*' ) !== - 1 ) {
91
91
rolesWithAction . push ( role )
92
92
return
93
93
}
94
94
95
- var result = _ . filter ( role . grants , function ( value ) {
95
+ const result = _ . filter ( role . grants , function ( value ) {
96
96
if ( _ . startsWith ( value , actionType + ':' ) ) return value
97
97
} )
98
98
@@ -104,7 +104,7 @@ var getRoles = function (action) {
104
104
}
105
105
}
106
106
107
- var typePerm = result [ 0 ] . split ( ':' ) [ 1 ] . split ( ' ' )
107
+ let typePerm = result [ 0 ] . split ( ':' ) [ 1 ] . split ( ' ' )
108
108
typePerm = _ . uniq ( typePerm )
109
109
110
110
if ( _ . indexOf ( typePerm , '*' ) !== - 1 ) {
@@ -123,17 +123,17 @@ var getRoles = function (action) {
123
123
}
124
124
125
125
function hasHierarchyEnabled ( roleId ) {
126
- var role = _ . find ( global . roles , function ( o ) {
126
+ const role = _ . find ( global . roles , function ( o ) {
127
127
return o . _id . toString ( ) === roleId . toString ( )
128
128
} )
129
129
if ( _ . isUndefined ( role ) || _ . isUndefined ( role . hierarchy ) ) return true
130
130
return role . hierarchy
131
131
}
132
132
133
133
function parseRoleHierarchy ( roleId ) {
134
- var roleOrder = global . roleOrder . order
134
+ const roleOrder = global . roleOrder . order
135
135
136
- var idx = _ . findIndex ( roleOrder , function ( i ) {
136
+ const idx = _ . findIndex ( roleOrder , function ( i ) {
137
137
return i . toString ( ) === roleId . toString ( )
138
138
} )
139
139
if ( idx === - 1 ) return [ ]
@@ -142,9 +142,9 @@ function parseRoleHierarchy (roleId) {
142
142
}
143
143
144
144
function hasPermOverRole ( ownRole , extRole ) {
145
- var roles = parseRoleHierarchy ( extRole )
145
+ const roles = parseRoleHierarchy ( extRole )
146
146
147
- var i = _ . find ( roles , function ( o ) {
147
+ const i = _ . find ( roles , function ( o ) {
148
148
return o . toString ( ) === ownRole . toString ( )
149
149
} )
150
150
@@ -160,9 +160,9 @@ function isAdmin (roleId, callback) {
160
160
}
161
161
162
162
function isAdminSync ( roleId ) {
163
- var roles = global . roles
163
+ const roles = global . roles
164
164
if ( ! roles ) return false
165
- var role = _ . find ( roles , function ( r ) {
165
+ const role = _ . find ( roles , function ( r ) {
166
166
return r . _id . toString ( ) === roleId . toString ( )
167
167
} )
168
168
@@ -178,15 +178,15 @@ function buildGrants (obj) {
178
178
}
179
179
180
180
module . exports = {
181
- register : register ,
181
+ register,
182
182
flushRoles : register ,
183
- canThis : canThis ,
184
- hasHierarchyEnabled : hasHierarchyEnabled ,
185
- parseRoleHierarchy : parseRoleHierarchy ,
186
- hasPermOverRole : hasPermOverRole ,
187
-
188
- getRoles : getRoles ,
189
- isAdmin : isAdmin ,
190
- isAdminSync : isAdminSync ,
191
- buildGrants : buildGrants
183
+ canThis,
184
+ hasHierarchyEnabled,
185
+ parseRoleHierarchy,
186
+ hasPermOverRole,
187
+
188
+ getRoles,
189
+ isAdmin,
190
+ isAdminSync,
191
+ buildGrants
192
192
}
0 commit comments