Skip to content
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
22 changes: 4 additions & 18 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ import {
isPendingDeletePolicy,
isPolicyAdmin as isPolicyAdminPolicyUtils,
isPolicyAuditor,
isPolicyMemberWithoutPendingDelete,
isPolicyOwner,
isSubmitAndClose,
shouldShowPolicy,
Expand Down Expand Up @@ -2648,23 +2647,10 @@ function isPayer(
reportPolicy?: OnyxInputOrEntry<Policy>,
onlyShowPayElsewhere = false,
) {
const policy = reportPolicy ?? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${iouReport?.policyID}`];

// If the report belongs to a workspace, verify the user is still a member
// When a user leaves a workspace, they may no longer have access to the policy data,
// or the policy.role/employeeList may be stale
// Skip this check for IOU reports (personal 1:1 expenses) since they can inherit a policyID
// from the chat report but the payer may not be a member of that workspace
if (!isIOUReport(iouReport) && iouReport?.policyID && iouReport.policyID !== CONST.POLICY.ID_FAKE) {
// No policy data means user likely left the workspace
if (!policy) {
return false;
}
// For paid group policies, verify membership via employeeList (only when loaded)
// employeeList may be lazily loaded/partial, so only check if it's present
if (isPaidGroupPolicy(iouReport) && !isEmptyObject(policy?.employeeList) && !isPolicyMemberWithoutPendingDelete(currentUserEmailParam, policy)) {
return false;
}
const policy = reportPolicy ?? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${iouReport?.policyID}`] ?? null;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Restore the workspace policy guard for payers

When a user leaves a workspace and the report's policy is no longer available locally, this now falls through instead of rejecting the workspace report. Because isMoneyRequestReport() also includes expense reports, the final manager check can still return true for an expense report whose managerID is the former user, and callers such as ReportPreviewActionUtils.canPay treat missing policy payments as enabled. In that leave-workspace/missing-policy state, the former approver can still see pay-related UI for workspace expenses, which is the regression the removed test covered.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@MelvinBot add this condition here:

if no policy is found, let’s return early.

if (!policy && !isIOUReport(iouReport) && iouReport?.policyID && iouReport.policyID !== CONST.POLICY.ID_FAKE) {
    return false;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 24f26c3. Added the early return right after the policy resolution in isPayer:

if (!policy && !isIOUReport(iouReport) && iouReport?.policyID && iouReport.policyID !== CONST.POLICY.ID_FAKE) {
    return false;
}

This rejects workspace reports when the policy isn't available locally (e.g. after the user leaves the workspace), which also addresses the regression the codex bot flagged above.


if (!policy && !isIOUReport(iouReport) && iouReport?.policyID && iouReport.policyID !== CONST.POLICY.ID_FAKE) {
return false;
}

const policyType = policy?.type;
Expand Down
46 changes: 0 additions & 46 deletions tests/unit/ReportUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8393,52 +8393,6 @@ describe('ReportUtils', () => {
expect(isPayer(adminAccountID, adminEmail, report, bankAccountList, policyWithAch, false)).toBe(false);
});

it('should return false when user left workspace and policy is not available', async () => {
// Clear the policy to simulate user leaving workspace
await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}left-workspace`, null);

const reportWithMissingPolicy: Report = {
...createRandomReport(6, undefined),
type: CONST.REPORT.TYPE.EXPENSE,
stateNum: CONST.REPORT.STATE_NUM.APPROVED,
statusNum: CONST.REPORT.STATUS_NUM.APPROVED,
policyID: 'left-workspace',
managerID: currentUserAccountID,
};

// User should not be a payer when policy is not available (they likely left the workspace)
expect(isPayer(currentUserAccountID, currentUserEmail, reportWithMissingPolicy, undefined, undefined, false)).toBe(false);
});

it('should return false when user is not in employeeList of a paid group policy', async () => {
const otherUserEmail = 'other@test.com';
const policyWithoutCurrentUser: Policy = {
...createRandomPolicy(2),
id: 'policy-without-user',
type: CONST.POLICY.TYPE.CORPORATE,
role: CONST.POLICY.ROLE.ADMIN, // Role might be stale
employeeList: {
[otherUserEmail]: {
role: CONST.POLICY.ROLE.ADMIN,
},
},
};

await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}policy-without-user`, policyWithoutCurrentUser);

const reportForNonMember: Report = {
...createRandomReport(7, undefined),
type: CONST.REPORT.TYPE.EXPENSE,
stateNum: CONST.REPORT.STATE_NUM.APPROVED,
statusNum: CONST.REPORT.STATUS_NUM.APPROVED,
policyID: 'policy-without-user',
managerID: currentUserAccountID,
};

// User should not be a payer even if role says admin, because they're not in employeeList
expect(isPayer(currentUserAccountID, currentUserEmail, reportForNonMember, undefined, policyWithoutCurrentUser, false)).toBe(false);
});

it('should return true for IOU report manager even when policy is not available', async () => {
// Simulate an IOU report that inherited a policyID from the chat report,
// but the receiver (payer) does not have access to that workspace policy
Expand Down
Loading