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

[EC-739 / EC-740] Add null check to policyFilter on PolicyService #4039

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
58 changes: 57 additions & 1 deletion libs/common/spec/services/policy.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ describe("PolicyService", () => {
organizationService.getAll(null).resolves([]);
activeAccount = new BehaviorSubject("123");
activeAccountUnlocked = new BehaviorSubject(true);
stateService.getDecryptedPolicies({ userId: "user" }).resolves(null);
stateService.getEncryptedPolicies({ userId: "user" }).resolves({
"1": policyData("1", "test-organization", PolicyType.MaximumVaultTimeout, true, {
minutes: 14,
}),
});
stateService.getEncryptedPolicies().resolves({
"1": policyData("1", "test-organization", PolicyType.MaximumVaultTimeout, true, {
minutes: 14,
Expand Down Expand Up @@ -296,7 +302,7 @@ describe("PolicyService", () => {
});
});

describe("policyAppliesToActiveUser", () => {
describe("policyAppliesToActiveUser$", () => {
it("MasterPassword does not apply", async () => {
const result = await firstValueFrom(
policyService.policyAppliesToActiveUser$(PolicyType.MasterPassword)
Expand All @@ -313,6 +319,14 @@ describe("PolicyService", () => {
expect(result).toEqual(true);
});

it("PolicyFilter filters result", async () => {
const result = await firstValueFrom(
policyService.policyAppliesToActiveUser$(PolicyType.MaximumVaultTimeout, (p) => false)
);

expect(result).toEqual(false);
});

it("DisablePersonalVaultExport does not apply", async () => {
const result = await firstValueFrom(
policyService.policyAppliesToActiveUser$(PolicyType.DisablePersonalVaultExport)
Expand All @@ -322,6 +336,48 @@ describe("PolicyService", () => {
});
});

describe("policyAppliesToUser", () => {
it("MasterPassword does not apply", async () => {
const result = await policyService.policyAppliesToUser(
PolicyType.MasterPassword,
null,
"user"
);

expect(result).toEqual(false);
});

it("MaximumVaultTimeout applies", async () => {
const result = await policyService.policyAppliesToUser(
PolicyType.MaximumVaultTimeout,
null,
"user"
);

expect(result).toEqual(true);
});

it("PolicyFilter filters result", async () => {
const result = await policyService.policyAppliesToUser(
PolicyType.MaximumVaultTimeout,
(p) => false,
"user"
);

expect(result).toEqual(false);
});

it("DisablePersonalVaultExport does not apply", async () => {
const result = await policyService.policyAppliesToUser(
PolicyType.DisablePersonalVaultExport,
null,
"user"
);

expect(result).toEqual(false);
});
});

function policyData(
id: string,
organizationId: string,
Expand Down
9 changes: 3 additions & 6 deletions libs/common/src/services/policy/policy.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,7 @@ export class PolicyService implements InternalPolicyServiceAbstraction {
);
}

policyAppliesToActiveUser$(
policyType: PolicyType,
policyFilter: (policy: Policy) => boolean = (p) => true
) {
policyAppliesToActiveUser$(policyType: PolicyType, policyFilter?: (policy: Policy) => boolean) {
return this.policies$.pipe(
concatMap(async (policies) => {
const userId = await this.stateService.getUserId();
Expand Down Expand Up @@ -257,12 +254,12 @@ export class PolicyService implements InternalPolicyServiceAbstraction {
private async checkPoliciesThatApplyToUser(
policies: Policy[],
policyType: PolicyType,
policyFilter: (policy: Policy) => boolean = (p) => true,
policyFilter?: (policy: Policy) => boolean,
userId?: string
) {
const organizations = await this.organizationService.getAll(userId);
const filteredPolicies = policies.filter(
(p) => p.type === policyType && p.enabled && policyFilter(p)
(p) => p.type === policyType && p.enabled && (policyFilter == null || policyFilter(p))
);
const policySet = new Set(filteredPolicies.map((p) => p.organizationId));

Expand Down