@@ -5,8 +5,9 @@ const Parse = require('parse/node');
5
5
// An Auth object tells you who is requesting something and whether
6
6
// the master key was used.
7
7
// userObject is a Parse.User and can be null if there's no user.
8
- function Auth ( { config, isMaster = false , isReadOnly = false , user, installationId } = { } ) {
8
+ function Auth ( { config, cacheController = undefined , isMaster = false , isReadOnly = false , user, installationId } ) {
9
9
this . config = config ;
10
+ this . cacheController = cacheController || ( config && config . cacheController ) ;
10
11
this . installationId = installationId ;
11
12
this . isMaster = isMaster ;
12
13
this . user = user ;
@@ -48,43 +49,54 @@ function nobody(config) {
48
49
49
50
50
51
// Returns a promise that resolves to an Auth object
51
- var getAuthForSessionToken = function ( { config, sessionToken, installationId } = { } ) {
52
- return config . cacheController . user . get ( sessionToken ) . then ( ( userJSON ) => {
52
+ var getAuthForSessionToken = async function ( { config, cacheController, sessionToken, installationId } ) {
53
+ cacheController = cacheController || ( config && config . cacheController ) ;
54
+ if ( cacheController ) {
55
+ const userJSON = await cacheController . user . get ( sessionToken ) ;
53
56
if ( userJSON ) {
54
57
const cachedUser = Parse . Object . fromJSON ( userJSON ) ;
55
58
return Promise . resolve ( new Auth ( { config, isMaster : false , installationId, user : cachedUser } ) ) ;
56
59
}
60
+ }
57
61
62
+ let results ;
63
+ if ( config ) {
58
64
var restOptions = {
59
65
limit : 1 ,
60
66
include : 'user'
61
67
} ;
62
68
63
69
var query = new RestQuery ( config , master ( config ) , '_Session' , { sessionToken} , restOptions ) ;
64
- return query . execute ( ) . then ( ( response ) => {
65
- var results = response . results ;
66
- if ( results . length !== 1 || ! results [ 0 ] [ 'user' ] ) {
67
- throw new Parse . Error ( Parse . Error . INVALID_SESSION_TOKEN , 'Invalid session token' ) ;
68
- }
70
+ results = ( await query . execute ( ) ) . results ;
71
+ } else {
72
+ results = ( await new Parse . Query ( Parse . Session )
73
+ . limit ( 1 )
74
+ . include ( 'user' )
75
+ . equalTo ( 'sessionToken' , sessionToken )
76
+ . find ( { useMasterKey : true } ) ) . map ( ( obj ) => obj . toJSON ( ) )
77
+ }
69
78
70
- var now = new Date ( ) ,
71
- expiresAt = results [ 0 ] . expiresAt ? new Date ( results [ 0 ] . expiresAt . iso ) : undefined ;
72
- if ( expiresAt < now ) {
73
- throw new Parse . Error ( Parse . Error . INVALID_SESSION_TOKEN ,
74
- 'Session token is expired.' ) ;
75
- }
76
- var obj = results [ 0 ] [ 'user' ] ;
77
- delete obj . password ;
78
- obj [ 'className' ] = '_User' ;
79
- obj [ 'sessionToken' ] = sessionToken ;
80
- config . cacheController . user . put ( sessionToken , obj ) ;
81
- const userObject = Parse . Object . fromJSON ( obj ) ;
82
- return new Auth ( { config, isMaster : false , installationId, user : userObject } ) ;
83
- } ) ;
84
- } ) ;
79
+ if ( results . length !== 1 || ! results [ 0 ] [ 'user' ] ) {
80
+ throw new Parse . Error ( Parse . Error . INVALID_SESSION_TOKEN , 'Invalid session token' ) ;
81
+ }
82
+ var now = new Date ( ) ,
83
+ expiresAt = results [ 0 ] . expiresAt ? new Date ( results [ 0 ] . expiresAt . iso ) : undefined ;
84
+ if ( expiresAt < now ) {
85
+ throw new Parse . Error ( Parse . Error . INVALID_SESSION_TOKEN ,
86
+ 'Session token is expired.' ) ;
87
+ }
88
+ var obj = results [ 0 ] [ 'user' ] ;
89
+ delete obj . password ;
90
+ obj [ 'className' ] = '_User' ;
91
+ obj [ 'sessionToken' ] = sessionToken ;
92
+ if ( cacheController ) {
93
+ cacheController . user . put ( sessionToken , obj ) ;
94
+ }
95
+ const userObject = Parse . Object . fromJSON ( obj ) ;
96
+ return new Auth ( { config, isMaster : false , installationId, user : userObject } ) ;
85
97
} ;
86
98
87
- var getAuthForLegacySessionToken = function ( { config, sessionToken, installationId } = { } ) {
99
+ var getAuthForLegacySessionToken = function ( { config, sessionToken, installationId } ) {
88
100
var restOptions = {
89
101
limit : 1
90
102
} ;
@@ -116,84 +128,115 @@ Auth.prototype.getUserRoles = function() {
116
128
return this . rolePromise ;
117
129
} ;
118
130
119
- // Iterates through the role tree and compiles a users roles
120
- Auth . prototype . _loadRoles = function ( ) {
121
- var cacheAdapter = this . config . cacheController ;
122
- return cacheAdapter . role . get ( this . user . id ) . then ( ( cachedRoles ) => {
123
- if ( cachedRoles != null ) {
124
- this . fetchedRoles = true ;
125
- this . userRoles = cachedRoles ;
126
- return Promise . resolve ( cachedRoles ) ;
127
- }
128
-
129
- var restWhere = {
131
+ Auth . prototype . getRolesForUser = function ( ) {
132
+ if ( this . config ) {
133
+ const restWhere = {
130
134
'users' : {
131
135
__type : 'Pointer' ,
132
136
className : '_User' ,
133
137
objectId : this . user . id
134
138
}
135
139
} ;
136
- // First get the role ids this user is directly a member of
137
- var query = new RestQuery ( this . config , master ( this . config ) , '_Role' , restWhere , { } ) ;
138
- return query . execute ( ) . then ( ( response ) => {
139
- var results = response . results ;
140
- if ( ! results . length ) {
141
- this . userRoles = [ ] ;
142
- this . fetchedRoles = true ;
143
- this . rolePromise = null ;
144
-
145
- cacheAdapter . role . put ( this . user . id , Array ( ...this . userRoles ) ) ;
146
- return Promise . resolve ( this . userRoles ) ;
147
- }
148
- var rolesMap = results . reduce ( ( m , r ) => {
149
- m . names . push ( r . name ) ;
150
- m . ids . push ( r . objectId ) ;
151
- return m ;
152
- } , { ids : [ ] , names : [ ] } ) ;
153
-
154
- // run the recursive finding
155
- return this . _getAllRolesNamesForRoleIds ( rolesMap . ids , rolesMap . names )
156
- . then ( ( roleNames ) => {
157
- this . userRoles = roleNames . map ( ( r ) => {
158
- return 'role:' + r ;
159
- } ) ;
160
- this . fetchedRoles = true ;
161
- this . rolePromise = null ;
162
- cacheAdapter . role . put ( this . user . id , Array ( ...this . userRoles ) ) ;
163
- return Promise . resolve ( this . userRoles ) ;
164
- } ) ;
140
+ const query = new RestQuery ( this . config , master ( this . config ) , '_Role' , restWhere , { } ) ;
141
+ return query . execute ( ) . then ( ( { results } ) => {
142
+ return results ;
165
143
} ) ;
144
+ }
145
+
146
+ return new Parse . Query ( Parse . Role )
147
+ . equalTo ( 'users' , this . user )
148
+ . find ( { useMasterKey : true } )
149
+ . then ( ( results ) => results . map ( ( obj ) => obj . toJSON ( ) ) ) ;
150
+ }
151
+
152
+ // Iterates through the role tree and compiles a users roles
153
+ Auth . prototype . _loadRoles = async function ( ) {
154
+ if ( this . cacheController ) {
155
+ const cachedRoles = await this . cacheController . role . get ( this . user . id ) ;
156
+ if ( cachedRoles != null ) {
157
+ this . fetchedRoles = true ;
158
+ this . userRoles = cachedRoles ;
159
+ return cachedRoles ;
160
+ }
161
+ }
162
+
163
+ // First get the role ids this user is directly a member of
164
+ const results = await this . getRolesForUser ( ) ;
165
+ if ( ! results . length ) {
166
+ this . userRoles = [ ] ;
167
+ this . fetchedRoles = true ;
168
+ this . rolePromise = null ;
169
+
170
+ this . cacheRoles ( ) ;
171
+ return this . userRoles ;
172
+ }
173
+
174
+ const rolesMap = results . reduce ( ( m , r ) => {
175
+ m . names . push ( r . name ) ;
176
+ m . ids . push ( r . objectId ) ;
177
+ return m ;
178
+ } , { ids : [ ] , names : [ ] } ) ;
179
+
180
+ // run the recursive finding
181
+ const roleNames = await this . _getAllRolesNamesForRoleIds ( rolesMap . ids , rolesMap . names ) ;
182
+ this . userRoles = roleNames . map ( ( r ) => {
183
+ return 'role:' + r ;
166
184
} ) ;
185
+ this . fetchedRoles = true ;
186
+ this . rolePromise = null ;
187
+ this . cacheRoles ( ) ;
188
+ return this . userRoles ;
167
189
} ;
168
190
169
- // Given a list of roleIds, find all the parent roles, returns a promise with all names
170
- Auth . prototype . _getAllRolesNamesForRoleIds = function ( roleIDs , names = [ ] , queriedRoles = { } ) {
171
- const ins = roleIDs . filter ( ( roleID ) => {
172
- return queriedRoles [ roleID ] !== true ;
173
- } ) . map ( ( roleID ) => {
174
- // mark as queried
175
- queriedRoles [ roleID ] = true ;
191
+ Auth . prototype . cacheRoles = function ( ) {
192
+ if ( ! this . cacheController ) {
193
+ return false ;
194
+ }
195
+ this . cacheController . role . put ( this . user . id , Array ( ...this . userRoles ) ) ;
196
+ return true ;
197
+ }
198
+
199
+ Auth . prototype . getRolesByIds = function ( ins ) {
200
+ const roles = ins . map ( ( id ) => {
176
201
return {
177
202
__type : 'Pointer' ,
178
203
className : '_Role' ,
179
- objectId : roleID
204
+ objectId : id
180
205
}
181
206
} ) ;
207
+ const restWhere = { 'roles' : { '$in' : roles } } ;
208
+
209
+ // Build an OR query across all parentRoles
210
+ if ( ! this . config ) {
211
+ return new Parse . Query ( Parse . Role )
212
+ . containedIn ( 'roles' , ins . map ( ( id ) => {
213
+ const role = new Parse . Object ( Parse . Role ) ;
214
+ role . id = id ;
215
+ return role ;
216
+ } ) )
217
+ . find ( { useMasterKey : true } )
218
+ . then ( ( results ) => results . map ( ( obj ) => obj . toJSON ( ) ) ) ;
219
+ }
220
+
221
+ return new RestQuery ( this . config , master ( this . config ) , '_Role' , restWhere , { } )
222
+ . execute ( )
223
+ . then ( ( { results } ) => results ) ;
224
+ }
225
+
226
+ // Given a list of roleIds, find all the parent roles, returns a promise with all names
227
+ Auth . prototype . _getAllRolesNamesForRoleIds = function ( roleIDs , names = [ ] , queriedRoles = { } ) {
228
+ const ins = roleIDs . filter ( ( roleID ) => {
229
+ const wasQueried = queriedRoles [ roleID ] !== true ;
230
+ queriedRoles [ roleID ] = true ;
231
+ return wasQueried ;
232
+ } ) ;
182
233
183
234
// all roles are accounted for, return the names
184
235
if ( ins . length == 0 ) {
185
236
return Promise . resolve ( [ ...new Set ( names ) ] ) ;
186
237
}
187
- // Build an OR query across all parentRoles
188
- let restWhere ;
189
- if ( ins . length == 1 ) {
190
- restWhere = { 'roles' : ins [ 0 ] } ;
191
- } else {
192
- restWhere = { 'roles' : { '$in' : ins } }
193
- }
194
- const query = new RestQuery ( this . config , master ( this . config ) , '_Role' , restWhere , { } ) ;
195
- return query . execute ( ) . then ( ( response ) => {
196
- var results = response . results ;
238
+
239
+ return this . getRolesByIds ( ins ) . then ( ( results ) => {
197
240
// Nothing found
198
241
if ( ! results . length ) {
199
242
return Promise . resolve ( names ) ;
0 commit comments