[PAM-617] Look up collection names by id in the access rules filter (#24745)

* PAM-617: count the Shared folders options in one pass and look up collection names by id

* PAM-617: drop the manual Shared folders counts, since the table's own count is fast with the id map
This commit is contained in:
Oscar Hinton
2026-11-13 12:43:29 +01:00
committed by GitHub
parent cd7189c113
commit 7cb2858c26
3 changed files with 37 additions and 26 deletions

View File

@@ -8,6 +8,8 @@ describe("accessRuleMatchesFilter", () => {
...overrides,
});
const noNames = new Map<string, string>();
const filter = (overrides: Partial<AccessRuleFilter> = {}): AccessRuleFilter => ({
text: "",
status: null,
@@ -16,21 +18,23 @@ describe("accessRuleMatchesFilter", () => {
});
it("includes everything under an empty filter", () => {
expect(accessRuleMatchesFilter(rule({}), ["Engineering"], filter())).toBe(true);
expect(accessRuleMatchesFilter(rule({}), new Map([["col-1", "Engineering"]]), filter())).toBe(
true,
);
});
it("drops disabled rules when filtering to enabled", () => {
expect(
accessRuleMatchesFilter(rule({ enabled: false }), [], filter({ status: "enabled" })),
accessRuleMatchesFilter(rule({ enabled: false }), noNames, filter({ status: "enabled" })),
).toBe(false);
expect(
accessRuleMatchesFilter(rule({ enabled: true }), [], filter({ status: "enabled" })),
accessRuleMatchesFilter(rule({ enabled: true }), noNames, filter({ status: "enabled" })),
).toBe(true);
});
it("drops enabled rules when filtering to disabled", () => {
expect(
accessRuleMatchesFilter(rule({ enabled: true }), [], filter({ status: "disabled" })),
accessRuleMatchesFilter(rule({ enabled: true }), noNames, filter({ status: "disabled" })),
).toBe(false);
});
@@ -38,14 +42,14 @@ describe("accessRuleMatchesFilter", () => {
expect(
accessRuleMatchesFilter(
rule({ collections: ["col-1"] }),
[],
noNames,
filter({ collectionIds: ["col-2"] }),
),
).toBe(false);
expect(
accessRuleMatchesFilter(
rule({ collections: ["col-2"] }),
[],
noNames,
filter({ collectionIds: ["col-2"] }),
),
).toBe(true);
@@ -55,31 +59,34 @@ describe("accessRuleMatchesFilter", () => {
expect(
accessRuleMatchesFilter(
rule({ collections: ["col-3"] }),
[],
noNames,
filter({ collectionIds: ["col-2", "col-3"] }),
),
).toBe(true);
expect(
accessRuleMatchesFilter(
rule({ collections: ["col-1"] }),
[],
noNames,
filter({ collectionIds: ["col-2", "col-3"] }),
),
).toBe(false);
});
it("matches search text against the rule name", () => {
expect(accessRuleMatchesFilter(rule({ name: "VPN access" }), [], filter({ text: "vpn" }))).toBe(
true,
);
expect(accessRuleMatchesFilter(rule({ name: "VPN access" }), [], filter({ text: "ssh" }))).toBe(
false,
);
expect(
accessRuleMatchesFilter(rule({ name: "VPN access" }), noNames, filter({ text: "vpn" })),
).toBe(true);
expect(
accessRuleMatchesFilter(rule({ name: "VPN access" }), noNames, filter({ text: "ssh" })),
).toBe(false);
});
it("matches search text against resolved collection names", () => {
expect(accessRuleMatchesFilter(rule({}), ["Engineering"], filter({ text: "engineer" }))).toBe(
true,
);
const names = new Map([["col-1", "Engineering"]]);
expect(accessRuleMatchesFilter(rule({}), names, filter({ text: "engineer" }))).toBe(true);
});
it("matches search text against the id of a collection it can't name", () => {
expect(accessRuleMatchesFilter(rule({}), noNames, filter({ text: "col-1" }))).toBe(true);
});
});

View File

@@ -14,7 +14,8 @@ export type AccessRuleFilter = {
*/
export function accessRuleMatchesFilter(
rule: { name: string; enabled: boolean; collections: readonly string[] },
collectionNames: string[],
/** Collection name by id. A collection missing from it is searched by its id. */
collectionNames: ReadonlyMap<string, string>,
filter: AccessRuleFilter,
): boolean {
if (filter.status === "enabled" && !rule.enabled) {
@@ -30,7 +31,8 @@ export function accessRuleMatchesFilter(
return false;
}
if (filter.text.length > 0) {
const haystack = `${rule.name} ${collectionNames.join(" ")}`.toLowerCase();
const names = rule.collections.map((id) => collectionNames.get(id) ?? id);
const haystack = `${rule.name} ${names.join(" ")}`.toLowerCase();
if (!haystack.includes(filter.text)) {
return false;
}

View File

@@ -20,7 +20,6 @@ import {
accessRuleMatchesFilter,
classifyAccessRuleError,
copyRuleName,
resolveCollectionNames,
filterRulesToToggle,
} from "@bitwarden/bit-pam";
import {
@@ -130,18 +129,21 @@ export class AccessRulesComponent {
BitTableV2Component,
);
/** Collection name by id, which the search matches a rule's collections against. */
private readonly collectionNamesById = computed(
() => new Map<string, string>(this.collections().map((c) => [c.id, c.name])),
);
/** The table's row test, given the search term and the chips' selections keyed by chip. */
protected readonly rowMatchesFilter = (
rule: AccessRuleView,
values: AccessRuleFilterValues,
): boolean => {
const ruleCollectionIds = rule.collections.map(uuidAsString);
return accessRuleMatchesFilter(
{ name: rule.name, enabled: rule.enabled, collections: ruleCollectionIds },
resolveCollectionNames(ruleCollectionIds, this.collections()),
): boolean =>
accessRuleMatchesFilter(
{ name: rule.name, enabled: rule.enabled, collections: rule.collections.map(uuidAsString) },
this.collectionNamesById(),
toAccessRuleFilter(values),
);
};
/** The rows the filters leave visible, which select-all spans. */
private readonly selectableRows = computed(() => this.rulesTable()?.filtered() ?? []);