@@ -10,7 +10,13 @@ import { ParsePubSub } from './ParsePubSub';
1010import SchemaController from '../Controllers/SchemaController' ;
1111import _ from 'lodash' ;
1212import { 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' ;
1420import { getAuthForSessionToken , Auth } from '../Auth' ;
1521import { getCacheController } from '../Controllers' ;
1622import LRU from 'lru-cache' ;
@@ -71,6 +77,7 @@ class ParseLiveQueryServer {
7177 this . subscriber = ParsePubSub . createSubscriber ( config ) ;
7278 this . subscriber . subscribe ( Parse . applicationId + 'afterSave' ) ;
7379 this . subscriber . subscribe ( Parse . applicationId + 'afterDelete' ) ;
80+ this . subscriber . subscribe ( Parse . applicationId + 'clearCache' ) ;
7481 // Register message handler for subscriber. When publisher get messages, it will publish message
7582 // to the subscribers and the handler will be called.
7683 this . subscriber . on ( 'message' , ( channel , messageStr ) => {
@@ -82,6 +89,10 @@ class ParseLiveQueryServer {
8289 logger . error ( 'unable to parse message' , messageStr , e ) ;
8390 return ;
8491 }
92+ if ( channel === Parse . applicationId + 'clearCache' ) {
93+ this . _clearCachedRoles ( message . userId ) ;
94+ return ;
95+ }
8596 this . _inflateParseObject ( message ) ;
8697 if ( channel === Parse . applicationId + 'afterSave' ) {
8798 this . _onAfterSave ( message ) ;
@@ -468,6 +479,32 @@ class ParseLiveQueryServer {
468479 return matchesQuery ( parseObject , subscription . query ) ;
469480 }
470481
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+
471508 getAuthForSessionToken ( sessionToken : ?string ) : Promise < { auth: ?Auth , userId : ?string } > {
472509 if ( ! sessionToken ) {
473510 return Promise . resolve ( { } ) ;
@@ -574,7 +611,6 @@ class ParseLiveQueryServer {
574611 if ( ! acl_has_roles ) {
575612 return false ;
576613 }
577-
578614 const roleNames = await auth . getUserRoles ( ) ;
579615 // Finally, see if any of the user's roles allow them read access
580616 for ( const role of roleNames ) {
0 commit comments