From 400360801915c561ec1cd7971314db30ee1c9f98 Mon Sep 17 00:00:00 2001 From: Robyn MacCallum Date: Wed, 25 Jun 2025 12:51:04 -0400 Subject: [PATCH] Hide card option from context menu when user is affected by card policy (#15272) * Hide card option from context menu when user is affected by card policy * Remove unused code --- .../browser/main-context-menu-handler.spec.ts | 31 ++++++++++++++++++- .../browser/main-context-menu-handler.ts | 11 ++++++- .../browser/src/background/main.background.ts | 10 ++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/apps/browser/src/autofill/browser/main-context-menu-handler.spec.ts b/apps/browser/src/autofill/browser/main-context-menu-handler.spec.ts index 267a832a67..901d6595fc 100644 --- a/apps/browser/src/autofill/browser/main-context-menu-handler.spec.ts +++ b/apps/browser/src/autofill/browser/main-context-menu-handler.spec.ts @@ -1,5 +1,5 @@ import { mock, MockProxy } from "jest-mock-extended"; -import { of } from "rxjs"; +import { BehaviorSubject, of } from "rxjs"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; import { @@ -22,6 +22,10 @@ import { UserId } from "@bitwarden/common/types/guid"; import { CipherType } from "@bitwarden/common/vault/enums"; import { Cipher } from "@bitwarden/common/vault/models/domain/cipher"; import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view"; +import { + RestrictedCipherType, + RestrictedItemTypesService, +} from "@bitwarden/common/vault/services/restricted-item-types.service"; import { MainContextMenuHandler } from "./main-context-menu-handler"; @@ -69,6 +73,8 @@ describe("context-menu", () => { let logService: MockProxy; let billingAccountProfileStateService: MockProxy; let accountService: MockProxy; + let restricted$: BehaviorSubject; + let restrictedItemTypesService: RestrictedItemTypesService; let removeAllSpy: jest.SpyInstance void]>; let createSpy: jest.SpyInstance< @@ -85,6 +91,10 @@ describe("context-menu", () => { logService = mock(); billingAccountProfileStateService = mock(); accountService = mock(); + restricted$ = new BehaviorSubject([]); + restrictedItemTypesService = { + restricted$, + } as Partial as RestrictedItemTypesService; removeAllSpy = jest .spyOn(chrome.contextMenus, "removeAll") @@ -105,6 +115,7 @@ describe("context-menu", () => { logService, billingAccountProfileStateService, accountService, + restrictedItemTypesService, ); jest.spyOn(MainContextMenuHandler, "remove"); @@ -147,6 +158,24 @@ describe("context-menu", () => { expect(createdMenu).toBeTruthy(); expect(createSpy).toHaveBeenCalledTimes(11); }); + + it("has menu enabled and has premium, but card type is restricted", async () => { + billingAccountProfileStateService.hasPremiumFromAnySource$.mockReturnValue(of(true)); + + restricted$.next([{ cipherType: CipherType.Card, allowViewOrgIds: [] }]); + + const createdMenu = await sut.init(); + expect(createdMenu).toBeTruthy(); + expect(createSpy).toHaveBeenCalledTimes(10); + }); + it("has menu enabled, does not have premium, and card type is restricted", async () => { + billingAccountProfileStateService.hasPremiumFromAnySource$.mockReturnValue(of(false)); + restricted$.next([{ cipherType: CipherType.Card, allowViewOrgIds: [] }]); + + const createdMenu = await sut.init(); + expect(createdMenu).toBeTruthy(); + expect(createSpy).toHaveBeenCalledTimes(9); + }); }); describe("loadOptions", () => { diff --git a/apps/browser/src/autofill/browser/main-context-menu-handler.ts b/apps/browser/src/autofill/browser/main-context-menu-handler.ts index ad9dc34e50..abfa2465c5 100644 --- a/apps/browser/src/autofill/browser/main-context-menu-handler.ts +++ b/apps/browser/src/autofill/browser/main-context-menu-handler.ts @@ -25,8 +25,9 @@ import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.servic import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; import { StateService } from "@bitwarden/common/platform/abstractions/state.service"; import { Utils } from "@bitwarden/common/platform/misc/utils"; -import { CipherType } from "@bitwarden/common/vault/enums"; +import { CipherType } from "@bitwarden/common/vault/enums/cipher-type"; import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view"; +import { RestrictedItemTypesService } from "@bitwarden/common/vault/services/restricted-item-types.service"; import { InitContextMenuItems } from "./abstractions/main-context-menu-handler"; @@ -157,6 +158,7 @@ export class MainContextMenuHandler { private logService: LogService, private billingAccountProfileStateService: BillingAccountProfileStateService, private accountService: AccountService, + private restrictedItemTypesService: RestrictedItemTypesService, ) {} /** @@ -181,6 +183,10 @@ export class MainContextMenuHandler { this.billingAccountProfileStateService.hasPremiumFromAnySource$(account.id), ); + const isCardRestricted = ( + await firstValueFrom(this.restrictedItemTypesService.restricted$) + ).some((rt) => rt.cipherType === CipherType.Card); + for (const menuItem of this.initContextMenuItems) { const { requiresPremiumAccess, @@ -192,6 +198,9 @@ export class MainContextMenuHandler { if (requiresPremiumAccess && !hasPremium) { continue; } + if (menuItem.id.startsWith(AUTOFILL_CARD_ID) && isCardRestricted) { + continue; + } await MainContextMenuHandler.create({ ...otherOptions, contexts: ["all"] }); } diff --git a/apps/browser/src/background/main.background.ts b/apps/browser/src/background/main.background.ts index 4ba869768f..3f448ca1b0 100644 --- a/apps/browser/src/background/main.background.ts +++ b/apps/browser/src/background/main.background.ts @@ -437,6 +437,8 @@ export default class MainBackground { private popupViewCacheBackgroundService: PopupViewCacheBackgroundService; + private restrictedItemTypesService: RestrictedItemTypesService; + constructor() { // Services const lockedCallback = async (userId: UserId) => { @@ -1307,6 +1309,13 @@ export default class MainBackground { this.stateProvider, ); + this.restrictedItemTypesService = new RestrictedItemTypesService( + this.configService, + this.accountService, + this.organizationService, + this.policyService, + ); + this.mainContextMenuHandler = new MainContextMenuHandler( this.stateService, this.autofillSettingsService, @@ -1314,6 +1323,7 @@ export default class MainBackground { this.logService, this.billingAccountProfileStateService, this.accountService, + this.restrictedItemTypesService, ); this.cipherContextMenuHandler = new CipherContextMenuHandler(