Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Fix #61219: Add ability to include personal collection items in search results for admins (#67396)

Brad Andersond2da7314281e
diff --git a/e2e/test/scenarios/onboarding/command-palette.cy.spec.js b/e2e/test/scenarios/onboarding/command-palette.cy.spec.js
index b5a28d0b5af..8ed4d7c4935 100644
--- a/e2e/test/scenarios/onboarding/command-palette.cy.spec.js
+++ b/e2e/test/scenarios/onboarding/command-palette.cy.spec.js
@@ -435,6 +435,15 @@ describe("command palette", () => {
});
});
+ it("should allow searching personal collections if no results and user is admin", () => {
+ cy.visit("/");
+ cy.findByRole("button", { name: /search/i }).click();
+ cy.realType("asdf");
+ H.commandPalette()
+ .get("#search-results-metadata")
+ .should("contain", "Search everything");
+ });
+
describe("ee", () => {
beforeEach(() => {
H.activateToken("bleeding-edge");
diff --git a/e2e/test/scenarios/search/search-filters.cy.spec.js b/e2e/test/scenarios/search/search-filters.cy.spec.js
index 836fa5c1f30..d4327d5931a 100644
--- a/e2e/test/scenarios/search/search-filters.cy.spec.js
+++ b/e2e/test/scenarios/search/search-filters.cy.spec.js
@@ -2,8 +2,10 @@ const { H } = cy;
import { SAMPLE_DB_ID } from "e2e/support/cypress_data";
import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
import {
+ ADMIN_PERSONAL_COLLECTION_ID,
ADMIN_USER_ID,
FIRST_COLLECTION_ID,
+ NORMAL_PERSONAL_COLLECTION_ID,
NORMAL_USER_ID,
ORDERS_COUNT_QUESTION_ID,
} from "e2e/support/cypress_sample_instance_data";
@@ -1003,6 +1005,83 @@ describe("scenarios > search", () => {
});
});
+ describe("personal collection filter", () => {
+ beforeEach(() => {
+ cy.log("Create a question in admin's personal collection");
+ H.createQuestion({
+ name: "Admin Personal Question [keyword]",
+ query: { "source-table": ORDERS_ID, limit: 1 },
+ collection_id: ADMIN_PERSONAL_COLLECTION_ID,
+ });
+
+ cy.log("Create a question in normal user's personal collection");
+ cy.signInAsNormalUser();
+ H.createQuestion({
+ name: "Normal User Personal Question [keyword]",
+ query: { "source-table": ORDERS_ID, limit: 1 },
+ collection_id: NORMAL_PERSONAL_COLLECTION_ID,
+ });
+ cy.signInAsAdmin();
+ });
+
+ it("should hydrate personal collection filter", () => {
+ cy.visit("/search?q=keyword&filter_items_in_personal_collection=all");
+ cy.wait("@search");
+
+ cy.findByTestId("filter_items_in_personal_collection-search-filter")
+ .findByRole("switch")
+ .should("be.checked");
+
+ expectSearchResultItemNameContent({
+ itemNames: [
+ "Admin Personal Question [keyword]",
+ "Normal User Personal Question [keyword]",
+ ],
+ });
+ });
+
+ it("should not be exposed to non-admins", () => {
+ cy.signInAsNormalUser();
+ cy.visit("/search?q=keyword&filter_items_in_personal_collection=all");
+ cy.wait("@search");
+ expectSearchResultItemNameContent({
+ itemNames: ["Normal User Personal Question [keyword]"],
+ });
+ cy.findByTestId(
+ "filter_items_in_personal_collection-search-filter",
+ ).should("not.exist");
+ });
+
+ it("should include other users' personal collection items when filter is turned on", () => {
+ cy.visit("/search?q=keyword");
+ cy.wait("@search");
+
+ cy.log("Should only see own personal items when filter is off");
+ expectSearchResultItemNameContent({
+ itemNames: ["Admin Personal Question [keyword]"],
+ });
+
+ cy.log("Turn on personal collection filter");
+ cy.findByTestId("filter_items_in_personal_collection-search-filter")
+ .findByRole("switch")
+ .should("not.be.checked");
+ cy.findByTestId("filter_items_in_personal_collection-search-filter")
+ .click()
+ .findByRole("switch")
+ .should("be.checked");
+
+ cy.url().should("contain", "filter_items_in_personal_collection=all");
+
+ cy.log("Should see own and other users' items when filter is on");
+ expectSearchResultItemNameContent({
+ itemNames: [
+ "Admin Personal Question [keyword]",
+ "Normal User Personal Question [keyword]",
+ ],
+ });
+ });
+ });
+
describe("trashed items filter", () => {
it("should only show items in the trash", () => {
cy.visit("/search?q=First");
diff --git a/frontend/src/metabase/common/hooks/use-show-other-users-collections.ts b/frontend/src/metabase/common/hooks/use-show-other-users-collections.ts
new file mode 100644
index 00000000000..8adb4d3a52d
--- /dev/null
+++ b/frontend/src/metabase/common/hooks/use-show-other-users-collections.ts
@@ -0,0 +1,16 @@
+import { useSelector } from "metabase/lib/redux";
+import { getUserIsAdmin } from "metabase/selectors/user";
+
+import { useSetting } from "./use-setting";
+
+/**
+ * Hook to determine if other users' collections are shown in the UI.
+ * Returns true if the current user is an admin AND there are other users in the instance.
+ */
+export const useShowOtherUsersCollections = (): boolean => {
+ const isAdmin = useSelector(getUserIsAdmin);
+ const activeUsersCount = useSetting("active-users-count");
+ const areThereOtherUsers = (activeUsersCount ?? 0) > 1;
+
+ return isAdmin && areThereOtherUsers;
+};
diff --git a/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/MainNavbarContainer.tsx b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/MainNavbarContainer.tsx
index 0a9af625362..3ef869f5093 100644
--- a/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/MainNavbarContainer.tsx
+++ b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/MainNavbarContainer.tsx
@@ -29,7 +29,6 @@ import {
getIsTenantUser,
getUser,
getUserCanWriteToCollections,
- getUserIsAdmin,
} from "metabase/selectors/user";
import type Database from "metabase-lib/v1/metadata/Database";
import type { Bookmark, Collection, User } from "metabase-types/api";
@@ -46,7 +45,6 @@ type NavbarModal = "MODAL_NEW_COLLECTION" | null;
function mapStateToProps(state: State, { databases = [] }: DatabaseProps) {
return {
currentUser: getUser(state),
- isAdmin: getUserIsAdmin(state),
hasDataAccess: databases.length > 0,
bookmarks: getOrderedBookmarks(state),
};
@@ -58,7 +56,6 @@ const mapDispatchToProps = {
};
interface Props extends MainNavbarProps {
- isAdmin: boolean;
currentUser: User;
databases: Database[];
selectedItems: SelectedItem[];
@@ -78,7 +75,6 @@ interface DatabaseProps {
function MainNavbarContainer({
bookmarks,
- isAdmin,
selectedItems,
isOpen,
currentUser,
@@ -205,7 +201,6 @@ function MainNavbarContainer({
<MainNavbarView
{...props}
bookmarks={bookmarks}
- isAdmin={isAdmin}
isOpen={isOpen}
collections={collectionTree}
selectedItems={selectedItems}
diff --git a/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/MainNavbarView.tsx b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/MainNavbarView.tsx
index 557290ceb3b..e94c13a2cf1 100644
--- a/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/MainNavbarView.tsx
+++ b/frontend/src/metabase/nav/containers/MainNavbar/MainNavbarContainer/MainNavbarView.tsx
@@ -14,6 +14,7 @@ import CollapseSection from "metabase/common/components/CollapseSection";
import { Tree } from "metabase/common/components/tree";
import { useSetting, useUserSetting } from "metabase/common/hooks";
import { useIsAtHomepageDashboard } from "metabase/common/hooks/use-is-at-homepage-dashboard";
+import { useShowOtherUsersCollections } from "metabase/common/hooks/use-show-other-users-collections";
import type { CollectionTreeItem } from "metabase/entities/collections";
import {
getCanAccessOnboardingPage,
@@ -56,7 +57,6 @@ import { BrowseNavSection } from "./BrowseNavSection";
import { GettingStartedSection } from "./GettingStartedSection";
type Props = {
- isAdmin: boolean;
isOpen: boolean;
bookmarks: Bookmark[];
hasDataAccess: boolean;
@@ -79,7 +79,6 @@ type Props = {
const OTHER_USERS_COLLECTIONS_URL = Urls.otherUsersPersonalCollections();
export function MainNavbarView({
- isAdmin,
bookmarks,
collections,
selectedItems,
@@ -173,9 +172,7 @@ export function MainNavbarView({
const canAccessOnboarding = useSelector(getCanAccessOnboardingPage);
const shouldDisplayGettingStarted = isNewInstance && canAccessOnboarding;
- const activeUsersCount = useSetting("active-users-count");
- const areThereOtherUsers = (activeUsersCount ?? 0) > 1;
- const showOtherUsersCollections = isAdmin && areThereOtherUsers;
+ const showOtherUsersCollections = useShowOtherUsersCollections();
const collectionsHeading = showExternalCollectionsSection
? t`Internal Collections`
diff --git a/frontend/src/metabase/palette/components/PaletteResults.tsx b/frontend/src/metabase/palette/components/PaletteResults.tsx
index f77e7d1e4f4..013488bdcb1 100644
--- a/frontend/src/metabase/palette/components/PaletteResults.tsx
+++ b/frontend/src/metabase/palette/components/PaletteResults.tsx
@@ -6,6 +6,7 @@ import { useKeyPressEvent } from "react-use";
import { t } from "ttag";
import NoResults from "assets/img/no_results.svg";
+import { useShowOtherUsersCollections } from "metabase/common/hooks/use-show-other-users-collections";
import { trackSearchClick } from "metabase/search/analytics";
import {
Flex,
@@ -29,6 +30,53 @@ import { PaletteResultList } from "./PaletteResultsList";
const PAGE_SIZE = 4;
+const FullSearchCTA = ({
+ locationQuery,
+ searchResults,
+ searchTerm,
+ onClick,
+}: {
+ locationQuery: Query;
+ searchResults: SearchResponse;
+ searchTerm: string;
+ onClick: () => void;
+}) => {
+ const showOtherUsersCollections = useShowOtherUsersCollections();
+ if (!searchResults.total && !showOtherUsersCollections) {
+ return null;
+ }
+
+ const promptSearchEverything =
+ !searchResults.total && showOtherUsersCollections;
+
+ return (
+ <Text
+ c="brand"
+ component={Link}
+ fw={700}
+ id="search-results-metadata"
+ to={{
+ pathname: "search",
+ query: {
+ ...locationQuery,
+ q: searchTerm,
+ },
+ }}
+ className={S.viewAndFilterResults}
+ onClick={onClick}
+ >
+ <Group align="center" gap={rem(4)}>
+ <span>
+ {promptSearchEverything
+ ? t`Search everything`
+ : t`View and filter all ${searchResults.total} results`}
+ </span>
+ <Icon name="chevronright" size={12} />
+ </Group>
+ </Text>
+ );
+};
+
type Props = Omit<StackProps, "children"> & {
locationQuery: Query;
searchRequestId?: string;
@@ -112,20 +160,11 @@ export const PaletteResults = ({
>
{item}
- {item === t`Results` && searchResults?.data.length && (
- <Text
- c="brand"
- component={Link}
- fw={700}
- id="search-results-metadata"
- to={{
- pathname: "search",
- query: {
- ...locationQuery,
- q: searchTerm,
- },
- }}
- className={S.viewAndFilterResults}
+ {item === t`Results` && searchResults && (
+ <FullSearchCTA
+ locationQuery={locationQuery}
+ searchResults={searchResults}
+ searchTerm={searchTerm}
onClick={() => {
query.setVisualState(VisualState.hidden);
@@ -140,13 +179,7 @@ export const PaletteResults = ({
searchTerm,
});
}}
- >
- <Group align="center" gap={rem(4)}>
- <span>{t`View and filter all ${searchResults?.total} results`}</span>
-
- <Icon name="chevronright" size={12} />
- </Group>
- </Text>
+ />
)}
</Group>
) : (
diff --git a/frontend/src/metabase/search/components/SearchSidebar/SearchSidebar.tsx b/frontend/src/metabase/search/components/SearchSidebar/SearchSidebar.tsx
index d2e234e6f04..976331f0678 100644
--- a/frontend/src/metabase/search/components/SearchSidebar/SearchSidebar.tsx
+++ b/frontend/src/metabase/search/components/SearchSidebar/SearchSidebar.tsx
@@ -1,5 +1,6 @@
import _ from "underscore";
+import { useShowOtherUsersCollections } from "metabase/common/hooks/use-show-other-users-collections";
import { PLUGIN_CONTENT_VERIFICATION } from "metabase/plugins";
import { DropdownSidebarFilter } from "metabase/search/components/DropdownSidebarFilter";
import { ToggleSidebarFilter } from "metabase/search/components/ToggleSidebarFilter";
@@ -8,6 +9,7 @@ import { CreatedByFilter } from "metabase/search/components/filters/CreatedByFil
import { LastEditedAtFilter } from "metabase/search/components/filters/LastEditedAtFilter";
import { LastEditedByFilter } from "metabase/search/components/filters/LastEditedByFilter";
import { NativeQueryFilter } from "metabase/search/components/filters/NativeQueryFilter";
+import { PersonalCollectionsFilter } from "metabase/search/components/filters/PersonalCollectionsFilter";
import { SearchTrashedItemsFilter } from "metabase/search/components/filters/SearchTrashedItemsFilter";
import { TypeFilter } from "metabase/search/components/filters/TypeFilter";
import { SearchFilterKeys } from "metabase/search/constants";
@@ -34,6 +36,7 @@ export const SearchSidebar = ({ value, onChange }: SearchSidebarProps) => {
[SearchFilterKeys.Verified]: PLUGIN_CONTENT_VERIFICATION.VerifiedFilter,
[SearchFilterKeys.NativeQuery]: NativeQueryFilter,
[SearchFilterKeys.SearchTrashedItems]: SearchTrashedItemsFilter,
+ [SearchFilterKeys.PersonalCollections]: PersonalCollectionsFilter,
};
const onOutputChange = (key: FilterTypeKeys, val?: SearchQueryParamValue) => {
@@ -78,6 +81,8 @@ export const SearchSidebar = ({ value, onChange }: SearchSidebarProps) => {
return null;
};
+ const showOtherUsersCollections = useShowOtherUsersCollections();
+
return (
<Stack gap="lg">
{getFilter(SearchFilterKeys.Type)}
@@ -92,6 +97,8 @@ export const SearchSidebar = ({ value, onChange }: SearchSidebarProps) => {
{getFilter(SearchFilterKeys.Verified)}
{getFilter(SearchFilterKeys.NativeQuery)}
{getFilter(SearchFilterKeys.SearchTrashedItems)}
+ {showOtherUsersCollections &&
+ getFilter(SearchFilterKeys.PersonalCollections)}
</Stack>
);
};
diff --git a/frontend/src/metabase/search/components/filters/PersonalCollectionsFilter.tsx b/frontend/src/metabase/search/components/filters/PersonalCollectionsFilter.tsx
new file mode 100644
index 00000000000..e7823f84e59
--- /dev/null
+++ b/frontend/src/metabase/search/components/filters/PersonalCollectionsFilter.tsx
@@ -0,0 +1,10 @@
+import { t } from "ttag";
+
+import type { SearchFilterToggle } from "metabase/search/types";
+
+export const PersonalCollectionsFilter: SearchFilterToggle = {
+ label: () => t`Search all personal collections`,
+ type: "toggle",
+ fromUrl: (value) => value === "all",
+ toUrl: (value: boolean) => (value ? "all" : null),
+};
diff --git a/frontend/src/metabase/search/constants.ts b/frontend/src/metabase/search/constants.ts
index e0e8207aad0..fff55c8f5af 100644
--- a/frontend/src/metabase/search/constants.ts
+++ b/frontend/src/metabase/search/constants.ts
@@ -9,6 +9,7 @@ export const SearchFilterKeys = {
LastEditedAt: "last_edited_at",
NativeQuery: "search_native_query",
SearchTrashedItems: "archived",
+ PersonalCollections: "filter_items_in_personal_collection",
} as const;
export const enabledSearchTypes: EnabledSearchModel[] = [
diff --git a/frontend/src/metabase/search/types.ts b/frontend/src/metabase/search/types.ts
index ffcceda943f..318c6d8c23d 100644
--- a/frontend/src/metabase/search/types.ts
+++ b/frontend/src/metabase/search/types.ts
@@ -21,6 +21,7 @@ export type LastEditedAtFilterProps = string | null;
export type VerifiedFilterProps = true | null;
export type NativeQueryFilterProps = true | null;
export type SearchTrashedItemsFilterProps = true | undefined;
+export type PersonalCollectionFilterProps = "only" | undefined;
export type SearchFilterPropTypes = {
[SearchFilterKeys.Type]: TypeFilterProps;
@@ -31,6 +32,7 @@ export type SearchFilterPropTypes = {
[SearchFilterKeys.LastEditedAt]: LastEditedAtFilterProps;
[SearchFilterKeys.NativeQuery]: NativeQueryFilterProps;
[SearchFilterKeys.SearchTrashedItems]: SearchTrashedItemsFilterProps;
+ [SearchFilterKeys.PersonalCollections]: PersonalCollectionFilterProps;
};
export type FilterTypeKeys = keyof SearchFilterPropTypes;