Skip to content

fix: copilot with removed access can still edit profile details #52865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/libs/Navigation/AppNavigator/AuthScreens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import {findFocusedRoute} from '@react-navigation/native';
import React, {memo, useEffect, useMemo, useRef, useState} from 'react';
import {NativeModules, View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import Onyx, {withOnyx} from 'react-native-onyx';
import Onyx, {useOnyx, withOnyx} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import ActiveGuidesEventListener from '@components/ActiveGuidesEventListener';
import ComposeProviders from '@components/ComposeProviders';
import DelegateNoAccessModal from '@components/DelegateNoAccessModal';
import OptionsListContextProvider from '@components/OptionListContextProvider';
import {SearchContextProvider} from '@components/Search/SearchContext';
import {useSearchRouterContext} from '@components/Search/SearchRouter/SearchRouterContext';
Expand All @@ -17,6 +18,7 @@ import usePermissions from '@hooks/usePermissions';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
import {disconnect} from '@libs/actions/Delegate';
import {READ_COMMANDS} from '@libs/API/types';
import HttpUtils from '@libs/HttpUtils';
import KeyboardShortcut from '@libs/KeyboardShortcut';
Expand Down Expand Up @@ -234,6 +236,7 @@ function AuthScreens({session, lastOpenedPublicRoomID, initialLastUpdateIDApplie
const {canUseDefaultRooms} = usePermissions();
const {activeWorkspaceID} = useActiveWorkspace();
const {toggleSearchRouter} = useSearchRouterContext();
const [account] = useOnyx(ONYXKEYS.ACCOUNT);

const onboardingModalScreenOptions = useMemo(() => screenOptions.onboardingModalNavigator(onboardingIsMediumOrLargerScreenWidth), [screenOptions, onboardingIsMediumOrLargerScreenWidth]);
const onboardingScreenOptions = useMemo(
Expand All @@ -242,6 +245,7 @@ function AuthScreens({session, lastOpenedPublicRoomID, initialLastUpdateIDApplie
);
const modal = useRef<OnyxTypes.Modal>({});
const [didPusherInit, setDidPusherInit] = useState(false);
const [isNoDelegateAccessMenuVisible, setIsNoDelegateAccessMenuVisible] = useState(false);
const {isOnboardingCompleted} = useOnboardingFlowRouter();
const [initialReportID] = useState(() => {
const currentURL = getCurrentUrl();
Expand Down Expand Up @@ -398,6 +402,17 @@ function AuthScreens({session, lastOpenedPublicRoomID, initialLastUpdateIDApplie
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, []);

useEffect(() => {
const isActingAsDelegate = !!account?.delegatedAccess?.delegate;
const delegates = account?.delegatedAccess?.delegates ?? [];
const isAccessRemoved = delegates.findIndex((delegate) => delegate.email === session?.email) === -1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use session. The email session is never in account?.delegatedAccess?.delegates. instead look for account?.delegatedAccess?.delegate in account?.delegatedAccess?.delegates

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@s77rt Here is an example: I have two accounts [email protected] (account 1) and [email protected] (account 2) and account 1 is delegating to account 2. And here is the Onyx data:

  1. account?.delegatedAccess?.delegate is account 1
  2. session.email is account 2
Screen.Recording.2024-11-26.at.15.52.20.mov

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct and we should use account 1 otherwise isAccessRemoved would always be true.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@s77rt Got it, A bit of chaos when doing PR 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated.

if (!isActingAsDelegate || !isAccessRemoved) {
return;
}
disconnect();
setIsNoDelegateAccessMenuVisible(true);
}, [account, session?.email]);

const CentralPaneScreenOptions = {
headerShown: false,
title: 'New Expensify',
Expand Down Expand Up @@ -578,6 +593,14 @@ function AuthScreens({session, lastOpenedPublicRoomID, initialLastUpdateIDApplie
</RootStack.Navigator>
<TestToolsModal />
<SearchRouterModal />
<DelegateNoAccessModal
isNoDelegateAccessMenuVisible={isNoDelegateAccessMenuVisible}
onClose={() => {
setIsNoDelegateAccessMenuVisible(false);
Session.signOutAndRedirectToSignIn(true);
}}
delegatorEmail={account?.delegatedAccess?.delegate ?? ''}
/>
</View>
{didPusherInit && <ActiveGuidesEventListener />}
</ComposeProviders>
Expand Down
Loading