From c0a0b4defc550544dd71096ea2f9476b74b8e348 Mon Sep 17 00:00:00 2001 From: imadtassaoui Date: Wed, 13 Nov 2024 12:01:56 +0100 Subject: [PATCH] fix: persist acl version --- src/App.tsx | 6 ++++++ src/models/ServerConfig.ts | 1 + src/services/BaseService.ts | 3 +++ src/store/auth.ts | 12 +++++++++++- src/store/networks.ts | 3 ++- 5 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 65978156..9c4ae072 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -215,6 +215,12 @@ function App() { }; }, [store.serverStatus.isHealthy]); + useEffect(() => { + setTimeout(() => { + store.setAclVersion(isServerEE ? store.aclVersion : 1); + }, 1000); + }, [isServerEE]); + return (
{' '} diff --git a/src/models/ServerConfig.ts b/src/models/ServerConfig.ts index 003ca27a..ea635db2 100644 --- a/src/models/ServerConfig.ts +++ b/src/models/ServerConfig.ts @@ -57,6 +57,7 @@ export interface TenantConfig { amuiAuthToken?: string; amuiUserId?: string; isNewTenant?: boolean; + aclVersion?: 1 | 2; } export interface ServerStatus { diff --git a/src/services/BaseService.ts b/src/services/BaseService.ts index b3cb31ed..032dd0f8 100644 --- a/src/services/BaseService.ts +++ b/src/services/BaseService.ts @@ -22,6 +22,7 @@ export const NMUI_AMUI_USER_ID_LOCALSTORAGE_KEY = 'nmui-amuiuid-lsk'; export const NMUI_USER_LOCALSTORAGE_KEY = 'nmui-u-lsk'; export const NMUI_USER_PLATFORM_ROLE_LOCALSTORAGE_KEY = 'nmui-upr-lsk'; export const NMUI_SHOW_RAC_BANNER_LOCALSTORAGE_KEY = 'nmui-show-rac-banner'; +export const NMUI_ACL_VERSION = 'nmui-acl-version'; // function to resolve the particular SaaS tenant's backend URL, ... export async function setupTenantConfig(): Promise { @@ -34,6 +35,7 @@ export async function setupTenantConfig(): Promise { username: window?.localStorage?.getItem(NMUI_USERNAME_LOCALSTORAGE_KEY) ?? '', user: JSON.parse(window?.localStorage?.getItem(NMUI_USER_LOCALSTORAGE_KEY) ?? 'null'), userPlatformRole: JSON.parse(window?.localStorage?.getItem(NMUI_USER_PLATFORM_ROLE_LOCALSTORAGE_KEY) ?? 'null'), + aclVersion: Number(window?.localStorage?.getItem(NMUI_ACL_VERSION) ?? '1') as 1 | 2, }); axiosService.defaults.baseURL = resolvedBaseUrl; return; @@ -113,6 +115,7 @@ export async function setupTenantConfig(): Promise { user: user || JSON.parse(window?.localStorage?.getItem(NMUI_USER_LOCALSTORAGE_KEY) ?? '{}'), userPlatformRole: userPlatformRole || JSON.parse(window?.localStorage?.getItem(NMUI_USER_PLATFORM_ROLE_LOCALSTORAGE_KEY) ?? '{}'), + aclVersion: Number(window?.localStorage?.getItem(NMUI_ACL_VERSION) ?? '1') as 1 | 2, }); } diff --git a/src/store/auth.ts b/src/store/auth.ts index 6e3bfb54..41b6234a 100644 --- a/src/store/auth.ts +++ b/src/store/auth.ts @@ -3,6 +3,7 @@ import { TenantConfig } from '../models/ServerConfig'; import { User, UserRole } from '@/models/User'; import { NMUI_ACCESS_TOKEN_LOCALSTORAGE_KEY, + NMUI_ACL_VERSION, NMUI_AMUI_USER_ID_LOCALSTORAGE_KEY, NMUI_BASE_URL_LOCALSTORAGE_KEY, NMUI_TENANT_ID_LOCALSTORAGE_KEY, @@ -28,7 +29,15 @@ export interface IAuthSlice { // methods isLoggedIn: () => boolean; - setStore: (config: Partial) => void; + setStore: ( + config: Partial< + TenantConfig & { + user: User | null; + userPlatformRole: UserRole | null; + aclVersion?: 1 | 2; + } + >, + ) => void; logout: () => void; } @@ -71,6 +80,7 @@ const createAuthSlice: StateCreator = (set, get) window?.localStorage?.removeItem(NMUI_AMUI_USER_ID_LOCALSTORAGE_KEY); window?.localStorage?.removeItem(NMUI_USER_LOCALSTORAGE_KEY); window?.localStorage?.removeItem(NMUI_USER_PLATFORM_ROLE_LOCALSTORAGE_KEY); + window?.localStorage?.removeItem(NMUI_ACL_VERSION); // TODO: consider using localStorage.clear() // window?.localStorage?.clear(); }, diff --git a/src/store/networks.ts b/src/store/networks.ts index 4fdf602a..6e335d68 100644 --- a/src/store/networks.ts +++ b/src/store/networks.ts @@ -2,6 +2,7 @@ import { NetworksService } from '@/services/NetworksService'; import { convertNetworkPayloadToUiNetwork } from '@/utils/NetworkUtils'; import { StateCreator } from 'zustand'; import { Network, NetworkStat } from '../models/Network'; +import { NMUI_ACL_VERSION } from '@/services/BaseService'; export interface INetworkSlice { // state @@ -23,7 +24,6 @@ const createNetworkSlice: StateCreator = ( networks: [], isFetchingNetworks: false, aclVersion: 1, - async fetchNetworks() { try { set(() => ({ isFetchingNetworks: true })); @@ -59,6 +59,7 @@ const createNetworkSlice: StateCreator = ( set((state) => ({ networks: state.networks.filter((network) => network.netid !== networkId) })); }, setAclVersion(version: 1 | 2) { + window?.localStorage?.setItem(NMUI_ACL_VERSION, version.toString()); set(() => ({ aclVersion: version })); }, });