diff --git a/apps/browser/src/services/families-policy.service.spec.ts b/apps/browser/src/services/families-policy.service.spec.ts new file mode 100644 index 0000000000..19291bcd82 --- /dev/null +++ b/apps/browser/src/services/families-policy.service.spec.ts @@ -0,0 +1,83 @@ +import { TestBed } from "@angular/core/testing"; +import { mock, MockProxy } from "jest-mock-extended"; +import { firstValueFrom, of } from "rxjs"; + +import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; +import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction"; +import { Organization } from "@bitwarden/common/admin-console/models/domain/organization"; +import { Policy } from "@bitwarden/common/admin-console/models/domain/policy"; + +import { FamiliesPolicyService } from "./families-policy.service"; // Adjust the import as necessary + +describe("FamiliesPolicyService", () => { + let service: FamiliesPolicyService; + let organizationService: MockProxy; + let policyService: MockProxy; + + beforeEach(() => { + organizationService = mock(); + policyService = mock(); + + TestBed.configureTestingModule({ + providers: [ + FamiliesPolicyService, + { provide: OrganizationService, useValue: organizationService }, + { provide: PolicyService, useValue: policyService }, + ], + }); + + service = TestBed.inject(FamiliesPolicyService); + }); + + it("should return false when there are no enterprise organizations", async () => { + jest.spyOn(service, "hasSingleEnterpriseOrg$").mockReturnValue(of(false)); + + const result = await firstValueFrom(service.isFreeFamilyPolicyEnabled$()); + expect(result).toBe(false); + }); + + it("should return true when the policy is enabled for the one enterprise organization", async () => { + jest.spyOn(service, "hasSingleEnterpriseOrg$").mockReturnValue(of(true)); + + const organizations = [{ id: "org1", canManageSponsorships: true }] as Organization[]; + organizationService.getAll$.mockReturnValue(of(organizations)); + + const policies = [{ organizationId: "org1", enabled: true }] as Policy[]; + policyService.getAll$.mockReturnValue(of(policies)); + + const result = await firstValueFrom(service.isFreeFamilyPolicyEnabled$()); + expect(result).toBe(true); + }); + + it("should return false when the policy is not enabled for the one enterprise organization", async () => { + jest.spyOn(service, "hasSingleEnterpriseOrg$").mockReturnValue(of(true)); + + const organizations = [{ id: "org1", canManageSponsorships: true }] as Organization[]; + organizationService.getAll$.mockReturnValue(of(organizations)); + + const policies = [{ organizationId: "org1", enabled: false }] as Policy[]; + policyService.getAll$.mockReturnValue(of(policies)); + + const result = await firstValueFrom(service.isFreeFamilyPolicyEnabled$()); + expect(result).toBe(false); + }); + + it("should return true when there is exactly one enterprise organization that can manage sponsorships", async () => { + const organizations = [{ id: "org1", canManageSponsorships: true }] as Organization[]; + organizationService.getAll$.mockReturnValue(of(organizations)); + + const result = await firstValueFrom(service.hasSingleEnterpriseOrg$()); + expect(result).toBe(true); + }); + + it("should return false when there are multiple organizations that can manage sponsorships", async () => { + const organizations = [ + { id: "org1", canManageSponsorships: true }, + { id: "org2", canManageSponsorships: true }, + ] as Organization[]; + organizationService.getAll$.mockReturnValue(of(organizations)); + + const result = await firstValueFrom(service.hasSingleEnterpriseOrg$()); + expect(result).toBe(false); + }); +}); diff --git a/apps/browser/src/services/families-policy.service.ts b/apps/browser/src/services/families-policy.service.ts new file mode 100644 index 0000000000..426f39dcfd --- /dev/null +++ b/apps/browser/src/services/families-policy.service.ts @@ -0,0 +1,54 @@ +import { Injectable } from "@angular/core"; +import { map, Observable, of, switchMap } from "rxjs"; + +import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; +import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction"; +import { PolicyType } from "@bitwarden/common/admin-console/enums"; + +@Injectable({ providedIn: "root" }) +export class FamiliesPolicyService { + constructor( + private policyService: PolicyService, + private organizationService: OrganizationService, + ) {} + + hasSingleEnterpriseOrg$(): Observable { + // Retrieve all organizations the user is part of + return this.organizationService.getAll$().pipe( + map((organizations) => { + // Filter to only those organizations that can manage sponsorships + const sponsorshipOrgs = organizations.filter((org) => org.canManageSponsorships); + + // Check if there is exactly one organization that can manage sponsorships. + // This is important because users that are part of multiple organizations + // may always access free bitwarden family menu. We want to restrict access + // to the policy only when there is a single enterprise organization and the free family policy is turn. + return sponsorshipOrgs.length === 1; + }), + ); + } + + isFreeFamilyPolicyEnabled$(): Observable { + return this.hasSingleEnterpriseOrg$().pipe( + switchMap((hasSingleEnterpriseOrg) => { + if (!hasSingleEnterpriseOrg) { + return of(false); + } + return this.organizationService.getAll$().pipe( + map((organizations) => organizations.find((org) => org.canManageSponsorships)?.id), + switchMap((enterpriseOrgId) => + this.policyService + .getAll$(PolicyType.FreeFamiliesSponsorshipPolicy) + .pipe( + map( + (policies) => + policies.find((policy) => policy.organizationId === enterpriseOrgId)?.enabled ?? + false, + ), + ), + ), + ); + }), + ); + } +} diff --git a/apps/browser/src/tools/popup/settings/about-page/more-from-bitwarden-page-v2.component.html b/apps/browser/src/tools/popup/settings/about-page/more-from-bitwarden-page-v2.component.html index 9322ab5113..a2d01ce752 100644 --- a/apps/browser/src/tools/popup/settings/about-page/more-from-bitwarden-page-v2.component.html +++ b/apps/browser/src/tools/popup/settings/about-page/more-from-bitwarden-page-v2.component.html @@ -12,7 +12,12 @@ - +