Skip to content
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

Fix only allow owner to delete the workspace #46692

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions src/libs/actions/Policy/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ function deleteWorkspace(policyID: string, policyName: string) {
});
});

const policy = getPolicy(policyID);
// Restore the old report stateNum and statusNum
const failureData: OnyxUpdate[] = [
{
Expand All @@ -328,6 +329,13 @@ function deleteWorkspace(policyID: string, policyName: string) {
errors: reimbursementAccount?.errors ?? null,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
avatarURL: policy?.avatarURL,
},
},
];

reportsToArchive.forEach((report) => {
Expand Down
23 changes: 14 additions & 9 deletions src/pages/workspace/WorkspaceProfilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React, {useCallback, useState} from 'react';
import type {ImageStyle, StyleProp} from 'react-native';
import {Image, StyleSheet, View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import {useOnyx, withOnyx} from 'react-native-onyx';
import Avatar from '@components/Avatar';
import AvatarWithImagePicker from '@components/AvatarWithImagePicker';
import Button from '@components/Button';
Expand Down Expand Up @@ -55,6 +55,8 @@ function WorkspaceProfilePage({policyDraft, policy: policyProp, currencyList = {
const {activeWorkspaceID, setActiveWorkspaceID} = useActiveWorkspace();
const {canUseSpotnanaTravel} = usePermissions();

const [currentUserAccountID = -1] = useOnyx(ONYXKEYS.SESSION, {selector: (session) => session?.accountID});

// When we create a new workspace, the policy prop will be empty on the first render. Therefore, we have to use policyDraft until policy has been set in Onyx.
const policy = policyDraft?.id ? policyDraft : policyProp;
const outputCurrency = policy?.outputCurrency ?? '';
Expand Down Expand Up @@ -84,6 +86,7 @@ function WorkspaceProfilePage({policyDraft, policy: policyProp, currencyList = {
}),
);
const readOnly = !PolicyUtils.isPolicyAdmin(policy);
const isOwner = PolicyUtils.isPolicyOwner(policy, currentUserAccountID);
const imageStyle: StyleProp<ImageStyle> = shouldUseNarrowLayout ? [styles.mhv12, styles.mhn5, styles.mbn5] : [styles.mhv8, styles.mhn8, styles.mbn5];
const shouldShowAddress = !readOnly || formattedAddress;

Expand Down Expand Up @@ -270,14 +273,16 @@ function WorkspaceProfilePage({policyDraft, policy: policyProp, currencyList = {
medium
icon={Expensicons.QrCode}
/>
<Button
accessibilityLabel={translate('common.delete')}
text={translate('common.delete')}
style={[styles.ml2]}
onPress={() => setIsDeleteModalOpen(true)}
medium
icon={Expensicons.Trashcan}
/>
{isOwner && (
<Button
accessibilityLabel={translate('common.delete')}
text={translate('common.delete')}
style={[styles.ml2]}
onPress={() => setIsDeleteModalOpen(true)}
medium
icon={Expensicons.Trashcan}
/>
)}
</View>
)}
</Section>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/workspace/WorkspacesListPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ function WorkspacesListPage({policies, reimbursementAccount, reports, session}:
// In such cases, let us use the available chat report ids from the policy.
const threeDotsMenuItems: PopoverMenuItem[] = [];

if (isAdmin) {
if (isOwner) {
threeDotsMenuItems.push({
icon: Expensicons.Trashcan,
text: translate('workspace.common.delete'),
Expand Down
Loading