diff --git a/config/services.yaml b/config/services.yaml index d308dce85..d4c65d209 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -33,6 +33,7 @@ services: $themeImageSupportedTypes: '%themes.image_supported_types%' $frontendExcludedModules: '%legacy.frontend_excluded_modules%' $datetimeFormatMap: '%legacy.datetime_format_map%' + $cacheResetActions: '%legacy.cache_reset_actions%' _instanceof: App\Service\ProcessHandlerInterface: tags: ['app.process.handler'] diff --git a/config/services/legacy/cache_reset_actions.yaml b/config/services/legacy/cache_reset_actions.yaml new file mode 100644 index 000000000..b62d16fb9 --- /dev/null +++ b/config/services/legacy/cache_reset_actions.yaml @@ -0,0 +1,4 @@ +parameters: + legacy.cache_reset_actions: + users: + - edit diff --git a/config/services/legacy/exposed_system_configs.yaml b/config/services/legacy/exposed_system_configs.yaml index 68a975547..f88784382 100644 --- a/config/services/legacy/exposed_system_configs.yaml +++ b/config/services/legacy/exposed_system_configs.yaml @@ -16,3 +16,4 @@ parameters: timef: true currency: true list_max_entries_per_page: true + cache_reset_actions: true diff --git a/core/app/src/app/app.component.ts b/core/app/src/app/app.component.ts index 42df6101a..202d874c9 100644 --- a/core/app/src/app/app.component.ts +++ b/core/app/src/app/app.component.ts @@ -1,27 +1,33 @@ -import {Component, ViewChild, ViewContainerRef, OnInit} from '@angular/core'; +import {Component, ViewChild, ViewContainerRef} from '@angular/core'; import {Event, NavigationCancel, NavigationEnd, NavigationError, NavigationStart, Router} from '@angular/router'; import {AppState, AppStateStore} from '@store/app-state/app-state.store'; import {Observable} from 'rxjs'; +import {StateManager} from '@store/state-manager'; +import {SystemConfigStore} from '@store/system-config/system-config.store'; @Component({ selector: 'app-root', templateUrl: './app.component.html' }) -export class AppComponent implements OnInit { +export class AppComponent { @ViewChild('mainOutlet', {read: ViewContainerRef, static: true}) mainOutlet: ViewContainerRef | undefined; appState$: Observable = this.appStateStore.vm$; - constructor(private router: Router, private appStateStore: AppStateStore) { + constructor( + private router: Router, + private appStateStore: AppStateStore, + protected stateManager: StateManager, + protected systemConfigs: SystemConfigStore + ) { router.events.subscribe((routerEvent: Event) => this.checkRouterEvent(routerEvent)); } - ngOnInit() { - } - - private checkRouterEvent(routerEvent: Event) { + protected checkRouterEvent(routerEvent: Event): void { if (routerEvent instanceof NavigationStart) { this.appStateStore.updateLoading('router-navigation', true); + this.conditionalCacheReset(); + } if (routerEvent instanceof NavigationEnd || @@ -30,4 +36,27 @@ export class AppComponent implements OnInit { this.appStateStore.updateLoading('router-navigation', false); } } + + protected conditionalCacheReset(): void { + const cacheClearActions = this.systemConfigs.getConfigValue('cache_reset_actions'); + const previousModule = this.appStateStore.getModule(); + const view = this.appStateStore.getView(); + + if (!cacheClearActions || !previousModule) { + return; + } + + const resetCacheActions: string[] = cacheClearActions[previousModule]; + + if (!resetCacheActions || !resetCacheActions.length) { + return; + } + + resetCacheActions.some(action => { + if (action === 'any' || action === view) { + this.stateManager.clearAuthBased(); + return true; + } + }); + } } diff --git a/core/legacy/SystemConfigHandler.php b/core/legacy/SystemConfigHandler.php index ea57560e4..4bdaaf6d9 100644 --- a/core/legacy/SystemConfigHandler.php +++ b/core/legacy/SystemConfigHandler.php @@ -47,6 +47,7 @@ class SystemConfigHandler extends LegacyHandler implements SystemConfigProviderI * @param ClassicViewRoutingExclusionsHandler $exclusionsManager * @param SystemConfigMappers $mappers * @param array $systemConfigKeyMap + * @param array $cacheResetActions */ public function __construct( string $projectDir, @@ -59,7 +60,8 @@ class SystemConfigHandler extends LegacyHandler implements SystemConfigProviderI ModuleNameMapperInterface $moduleNameMapper, ClassicViewRoutingExclusionsHandler $exclusionsManager, SystemConfigMappers $mappers, - array $systemConfigKeyMap + array $systemConfigKeyMap, + array $cacheResetActions ) { parent::__construct($projectDir, $legacyDir, $legacySessionName, $defaultSessionName, $legacyScopeState); $this->exposedSystemConfigs = $exposedSystemConfigs; @@ -67,6 +69,7 @@ class SystemConfigHandler extends LegacyHandler implements SystemConfigProviderI $this->injectedSystemConfigs['module_name_map'] = $moduleNameMapper->getLegacyToFrontendMap(); $this->injectedSystemConfigs['action_name_map'] = $actionNameMapper->getMap(); $this->injectedSystemConfigs['classicview_routing_exclusions'] = $exclusionsManager->get(); + $this->injectedSystemConfigs['cache_reset_actions'] = $cacheResetActions; $this->mappers = $mappers; $this->systemConfigKeyMap = $systemConfigKeyMap; } diff --git a/tests/unit/core/legacy/SystemConfigHandlerTest.php b/tests/unit/core/legacy/SystemConfigHandlerTest.php index c870edd04..ad350dfb4 100644 --- a/tests/unit/core/legacy/SystemConfigHandlerTest.php +++ b/tests/unit/core/legacy/SystemConfigHandlerTest.php @@ -56,6 +56,7 @@ class SystemConfigHandlerTest extends Unit 'datef' => true, 'timef' => true, 'currency' => true, + 'cache_reset_actions' => true ]; $moduleMapper = new ModuleNameMapperHandler( @@ -130,6 +131,12 @@ class SystemConfigHandlerTest extends Unit $sugar_config['datef'] = 'm/d/Y'; $sugar_config['timef'] = 'H:i'; + $cacheResetActions = [ + 'users' => [ + 'edit' + ] + ]; + $this->handler = new SystemConfigHandler( $projectDir, $legacyDir, @@ -141,7 +148,8 @@ class SystemConfigHandlerTest extends Unit $moduleMapper, $classicViewExclusionHandler, $mappers, - $systemConfigKeyMap + $systemConfigKeyMap, + $cacheResetActions ); } @@ -293,4 +301,25 @@ class SystemConfigHandlerTest extends Unit static::assertArrayHasKey('iso4217', $currency); static::assertNotEmpty($currency['iso4217']); } + + /** + * Test cache reset actions + */ + public function testCacheResetActionsConfig(): void + { + $cacheClearActionsConfig = $this->handler->getSystemConfig('cache_reset_actions'); + static::assertNotNull($cacheClearActionsConfig); + static::assertEquals('cache_reset_actions', $cacheClearActionsConfig->getId()); + static::assertNull($cacheClearActionsConfig->getValue()); + static::assertIsArray($cacheClearActionsConfig->getItems()); + static::assertNotEmpty($cacheClearActionsConfig->getItems()); + + $cacheClearActions = $cacheClearActionsConfig->getItems(); + static::assertNotNull($cacheClearActions); + static::assertNotEmpty($cacheClearActions); + + static::assertArrayHasKey('users', $cacheClearActions); + static::assertNotEmpty($cacheClearActions['users']); + static::assertContains('edit', $cacheClearActions['users']); + } }