@@ -10,7 +10,13 @@ import { ParsePubSub } from './ParsePubSub';
10
10
import SchemaController from '../Controllers/SchemaController' ;
11
11
import _ from 'lodash' ;
12
12
import { v4 as uuidv4 } from 'uuid' ;
13
- import { runLiveQueryEventHandlers , getTrigger , runTrigger , resolveError , toJSONwithObjects } from '../triggers' ;
13
+ import {
14
+ runLiveQueryEventHandlers ,
15
+ getTrigger ,
16
+ runTrigger ,
17
+ resolveError ,
18
+ toJSONwithObjects ,
19
+ } from '../triggers' ;
14
20
import { getAuthForSessionToken , Auth } from '../Auth' ;
15
21
import { getCacheController } from '../Controllers' ;
16
22
import LRU from 'lru-cache' ;
@@ -71,6 +77,7 @@ class ParseLiveQueryServer {
71
77
this . subscriber = ParsePubSub . createSubscriber ( config ) ;
72
78
this . subscriber . subscribe ( Parse . applicationId + 'afterSave' ) ;
73
79
this . subscriber . subscribe ( Parse . applicationId + 'afterDelete' ) ;
80
+ this . subscriber . subscribe ( Parse . applicationId + 'clearCache' ) ;
74
81
// Register message handler for subscriber. When publisher get messages, it will publish message
75
82
// to the subscribers and the handler will be called.
76
83
this . subscriber . on ( 'message' , ( channel , messageStr ) => {
@@ -82,6 +89,10 @@ class ParseLiveQueryServer {
82
89
logger . error ( 'unable to parse message' , messageStr , e ) ;
83
90
return ;
84
91
}
92
+ if ( channel === Parse . applicationId + 'clearCache' ) {
93
+ this . _clearCachedRoles ( message . userId ) ;
94
+ return ;
95
+ }
85
96
this . _inflateParseObject ( message ) ;
86
97
if ( channel === Parse . applicationId + 'afterSave' ) {
87
98
this . _onAfterSave ( message ) ;
@@ -468,6 +479,32 @@ class ParseLiveQueryServer {
468
479
return matchesQuery ( parseObject , subscription . query ) ;
469
480
}
470
481
482
+ async _clearCachedRoles ( userId : string ) {
483
+ try {
484
+ const validTokens = await new Parse . Query ( Parse . Session )
485
+ . equalTo ( 'user' , Parse . User . createWithoutData ( userId ) )
486
+ . find ( { useMasterKey : true } ) ;
487
+ await Promise . all (
488
+ validTokens . map ( async token => {
489
+ const sessionToken = token . get ( 'sessionToken' ) ;
490
+ const authPromise = this . authCache . get ( sessionToken ) ;
491
+ if ( ! authPromise ) {
492
+ return ;
493
+ }
494
+ const [ auth1 , auth2 ] = await Promise . all ( [
495
+ authPromise ,
496
+ getAuthForSessionToken ( { cacheController : this . cacheController , sessionToken } ) ,
497
+ ] ) ;
498
+ auth1 . auth ?. clearRoleCache ( sessionToken ) ;
499
+ auth2 . auth ?. clearRoleCache ( sessionToken ) ;
500
+ this . authCache . del ( sessionToken ) ;
501
+ } )
502
+ ) ;
503
+ } catch ( e ) {
504
+ logger . verbose ( `Could not clear role cache. ${ e } ` ) ;
505
+ }
506
+ }
507
+
471
508
getAuthForSessionToken ( sessionToken : ?string ) : Promise < { auth: ?Auth , userId : ?string } > {
472
509
if ( ! sessionToken ) {
473
510
return Promise . resolve ( { } ) ;
@@ -574,7 +611,6 @@ class ParseLiveQueryServer {
574
611
if ( ! acl_has_roles ) {
575
612
return false ;
576
613
}
577
-
578
614
const roleNames = await auth . getUserRoles ( ) ;
579
615
// Finally, see if any of the user's roles allow them read access
580
616
for ( const role of roleNames ) {
0 commit comments