1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-21 11:35:34 +01:00

Update policy service to clear its own state (#8564)

This commit is contained in:
Addison Beck 2024-04-05 13:10:24 -05:00 committed by GitHub
parent edf35a9ad1
commit 2ff990edd2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 3 additions and 71 deletions

View File

@ -1139,7 +1139,6 @@ export default class MainBackground {
this.cipherService.clear(userId),
this.folderService.clear(userId),
this.collectionService.clear(userId),
this.policyService.clear(userId),
this.passwordGenerationService.clear(userId),
this.vaultTimeoutSettingsService.clear(userId),
this.vaultFilterService.clear(),

View File

@ -702,7 +702,6 @@ export class Main {
this.cipherService.clear(userId),
this.folderService.clear(userId),
this.collectionService.clear(userId as UserId),
this.policyService.clear(userId as UserId),
this.passwordGenerationService.clear(),
this.providerService.save(null, userId as UserId),
]);

View File

@ -583,7 +583,6 @@ export class AppComponent implements OnInit, OnDestroy {
await this.collectionService.clear(userBeingLoggedOut);
await this.passwordGenerationService.clear(userBeingLoggedOut);
await this.vaultTimeoutSettingsService.clear(userBeingLoggedOut);
await this.policyService.clear(userBeingLoggedOut);
await this.biometricStateService.logout(userBeingLoggedOut as UserId);
await this.providerService.save(null, userBeingLoggedOut as UserId);

View File

@ -274,7 +274,6 @@ export class AppComponent implements OnDestroy, OnInit {
this.cipherService.clear(userId),
this.folderService.clear(userId),
this.collectionService.clear(userId),
this.policyService.clear(userId),
this.passwordGenerationService.clear(),
this.biometricStateService.logout(userId as UserId),
this.paymentMethodWarningService.clear(),

View File

@ -78,5 +78,4 @@ export abstract class PolicyService {
export abstract class InternalPolicyService extends PolicyService {
upsert: (policy: PolicyData) => Promise<void>;
replace: (policies: { [id: string]: PolicyData }) => Promise<void>;
clear: (userId?: string) => Promise<void>;
}

View File

@ -102,66 +102,6 @@ describe("PolicyService", () => {
]);
});
describe("clear", () => {
beforeEach(() => {
activeUserState.nextState(
arrayToRecord([
policyData("1", "test-organization", PolicyType.MaximumVaultTimeout, true, {
minutes: 14,
}),
]),
);
});
it("clears state for the active user", async () => {
await policyService.clear();
expect(await firstValueFrom(policyService.policies$)).toEqual([]);
expect(await firstValueFrom(activeUserState.state$)).toEqual(null);
expect(stateProvider.activeUser.getFake(POLICIES).nextMock).toHaveBeenCalledWith([
"userId",
null,
]);
});
it("clears state for an inactive user", async () => {
const inactiveUserId = "someOtherUserId" as UserId;
const inactiveUserState = stateProvider.singleUser.getFake(inactiveUserId, POLICIES);
inactiveUserState.nextState(
arrayToRecord([
policyData("10", "another-test-organization", PolicyType.PersonalOwnership, true),
]),
);
await policyService.clear(inactiveUserId);
// Active user is not affected
const expectedActiveUserPolicy: Partial<Policy> = {
id: "1" as PolicyId,
organizationId: "test-organization",
type: PolicyType.MaximumVaultTimeout,
enabled: true,
data: { minutes: 14 },
};
expect(await firstValueFrom(policyService.policies$)).toEqual([expectedActiveUserPolicy]);
expect(await firstValueFrom(activeUserState.state$)).toEqual({
"1": expectedActiveUserPolicy,
});
expect(stateProvider.activeUser.getFake(POLICIES).nextMock).not.toHaveBeenCalled();
// Non-active user is cleared
expect(
await firstValueFrom(
policyService.getAll$(PolicyType.PersonalOwnership, "someOtherUserId" as UserId),
),
).toEqual([]);
expect(await firstValueFrom(inactiveUserState.state$)).toEqual(null);
expect(
stateProvider.singleUser.getFake("someOtherUserId" as UserId, POLICIES).nextMock,
).toHaveBeenCalledWith(null);
});
});
describe("masterPasswordPolicyOptions", () => {
it("returns default policy options", async () => {
const data: any = {

View File

@ -1,6 +1,6 @@
import { combineLatest, firstValueFrom, map, Observable, of } from "rxjs";
import { KeyDefinition, POLICIES_DISK, StateProvider } from "../../../platform/state";
import { UserKeyDefinition, POLICIES_DISK, StateProvider } from "../../../platform/state";
import { PolicyId, UserId } from "../../../types/guid";
import { OrganizationService } from "../../abstractions/organization/organization.service.abstraction";
import { InternalPolicyService as InternalPolicyServiceAbstraction } from "../../abstractions/policy/policy.service.abstraction";
@ -14,8 +14,9 @@ import { ResetPasswordPolicyOptions } from "../../models/domain/reset-password-p
const policyRecordToArray = (policiesMap: { [id: string]: PolicyData }) =>
Object.values(policiesMap || {}).map((f) => new Policy(f));
export const POLICIES = KeyDefinition.record<PolicyData, PolicyId>(POLICIES_DISK, "policies", {
export const POLICIES = UserKeyDefinition.record<PolicyData, PolicyId>(POLICIES_DISK, "policies", {
deserializer: (policyData) => policyData,
clearOn: ["logout"],
});
export class PolicyService implements InternalPolicyServiceAbstraction {
@ -222,10 +223,6 @@ export class PolicyService implements InternalPolicyServiceAbstraction {
await this.activeUserPolicyState.update(() => policies);
}
async clear(userId?: UserId): Promise<void> {
await this.stateProvider.setUserState(POLICIES, null, userId);
}
/**
* Determines whether an orgUser is exempt from a specific policy because of their role
* Generally orgUsers who can manage policies are exempt from them, but some policies are stricter