Skip to content

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

Restrict glossary permissions (#76275)

Nicola Mometto0a0589299cfd
diff --git a/frontend/src/metabase/common/components/Glossary/Glossary.module.css b/frontend/src/metabase/common/components/Glossary/Glossary.module.css
index b2c1d27c253..666c5b57af4 100644
--- a/frontend/src/metabase/common/components/Glossary/Glossary.module.css
+++ b/frontend/src/metabase/common/components/Glossary/Glossary.module.css
@@ -23,9 +23,15 @@
}
.row {
- cursor: pointer;
+ &:not(.readOnly) {
+ cursor: pointer;
+ }
&:hover {
+ &.readOnly {
+ background-color: transparent;
+ }
+
.action {
visibility: visible;
}
diff --git a/frontend/src/metabase/common/components/Glossary/GlossaryRowEditor.tsx b/frontend/src/metabase/common/components/Glossary/GlossaryRowEditor.tsx
index d589400182b..918f2961fa8 100644
--- a/frontend/src/metabase/common/components/Glossary/GlossaryRowEditor.tsx
+++ b/frontend/src/metabase/common/components/Glossary/GlossaryRowEditor.tsx
@@ -15,12 +15,13 @@ import {
} from "metabase/ui";
import S from "./Glossary.module.css";
+import type { GlossaryField } from "./types";
type GlossaryRowEditorProps = {
item: Pick<GlossaryItem, "term" | "definition">;
onSave: (term: string, definition: string) => void | Promise<void>;
onCancel: () => void;
- autoFocusField?: "term" | "definition";
+ autoFocusField?: GlossaryField;
existingTerms?: string[];
};
@@ -142,7 +143,7 @@ export function GlossaryRowEditor({
);
}
-function useFocusOnMount(autoFocusField: "term" | "definition") {
+function useFocusOnMount(autoFocusField: GlossaryField) {
const termRef = useRef<HTMLInputElement>(null);
const definitionRef = useRef<HTMLTextAreaElement>(null);
diff --git a/frontend/src/metabase/common/components/Glossary/GlossaryTable.tsx b/frontend/src/metabase/common/components/Glossary/GlossaryTable.tsx
index 1f9d4eafbd4..ce8794e501c 100644
--- a/frontend/src/metabase/common/components/Glossary/GlossaryTable.tsx
+++ b/frontend/src/metabase/common/components/Glossary/GlossaryTable.tsx
@@ -22,22 +22,25 @@ import type { SortDirection } from "metabase-types/api";
import S from "./Glossary.module.css";
import { GlossaryRowEditor } from "./GlossaryRowEditor";
+import type { GlossaryField } from "./types";
export type GlossaryTableProps = {
className?: string;
glossary: GlossaryItem[];
- onCreate: (term: string, definition: string) => Promise<void> | void;
- onEdit: (
+ readOnly?: boolean;
+ onCreate?: (term: string, definition: string) => Promise<void> | void;
+ onEdit?: (
id: number,
term: string,
definition: string,
) => Promise<void> | void;
- onDelete: (id: number) => Promise<void> | void;
+ onDelete?: (id: number) => Promise<void> | void;
};
export function GlossaryTable({
className,
glossary,
+ readOnly = false,
onCreate,
onEdit,
onDelete,
@@ -45,15 +48,24 @@ export function GlossaryTable({
const [isCreating, setIsCreating] = useState(false);
const metabotName = useMetabotName();
const [editingId, setEditingId] = useState<number | null>(null);
- const [editingField, setEditingField] = useState<
- "term" | "definition" | null
- >(null);
+ const [editingField, setEditingField] = useState<GlossaryField | null>(null);
const [deletingItem, setDeletingItem] = useState<GlossaryItem | null>(null);
const [sortColumnName, setSortColumnName] = useState<
keyof GlossaryItem | null
>(null);
const [sortDirection, setSortDirection] = useState<SortDirection>("asc");
+ const startEditing = (id: number, field: GlossaryField) => {
+ if (readOnly) {
+ return;
+ }
+ if (isCreating) {
+ setIsCreating(false);
+ }
+ setEditingId(id);
+ setEditingField(field);
+ };
+
const sortedRows = useMemo(() => {
if (!sortColumnName) {
return [
@@ -79,20 +91,22 @@ export function GlossaryTable({
<Text>
{t`Define terms to help your team and ${metabotName} understand your data.`}
</Text>
- <Button
- variant="default"
- size="sm"
- leftSection={<Icon name="add" />}
- onClick={() => {
- if (editingId) {
- setEditingId(null);
- setEditingField(null);
- }
- setIsCreating(true);
- }}
- >
- {t`New term`}
- </Button>
+ {!readOnly && (
+ <Button
+ variant="default"
+ size="sm"
+ leftSection={<Icon name="add" />}
+ onClick={() => {
+ if (editingId) {
+ setEditingId(null);
+ setEditingField(null);
+ }
+ setIsCreating(true);
+ }}
+ >
+ {t`New term`}
+ </Button>
+ )}
</Group>
<CommonTable
className={cx(S.table, className)}
@@ -134,7 +148,7 @@ export function GlossaryTable({
existingTerms={existingTerms}
onCancel={() => setIsCreating(false)}
onSave={async (term, definition) => {
- await onCreate(term, definition);
+ await onCreate?.(term, definition);
setIsCreating(false);
}}
/>
@@ -146,7 +160,12 @@ export function GlossaryTable({
const isEditing = editingId === item.id;
return (
- <tr className={cx(S.row, { [S.rowEditor]: isEditing })}>
+ <tr
+ className={cx(S.row, {
+ [S.rowEditor]: isEditing,
+ [S.readOnly]: readOnly,
+ })}
+ >
{isEditing ? (
<GlossaryRowEditor
item={item}
@@ -156,7 +175,7 @@ export function GlossaryTable({
.map((g) => g.term)}
onCancel={() => setEditingId(null)}
onSave={async (newTerm, newDefinition) => {
- await onEdit(item.id, newTerm, newDefinition);
+ await onEdit?.(item.id, newTerm, newDefinition);
setEditingId(null);
setEditingField(null);
}}
@@ -166,14 +185,7 @@ export function GlossaryTable({
<Box
component="td"
valign="top"
- onClick={() => {
- if (isCreating) {
- setIsCreating(false);
- }
-
- setEditingId(item.id);
- setEditingField("term");
- }}
+ onClick={() => startEditing(item.id, "term")}
>
<Text lh="1.2" fw="bold" pt="xs">
{item.term}
@@ -183,13 +195,7 @@ export function GlossaryTable({
component="td"
valign="top"
style={{ wordBreak: "break-word", whiteSpace: "pre-wrap" }}
- onClick={() => {
- if (isCreating) {
- setIsCreating(false);
- }
- setEditingId(item.id);
- setEditingField("definition");
- }}
+ onClick={() => startEditing(item.id, "definition")}
>
<Text lh="1.2" pt="xs">
{item.definition}
@@ -203,17 +209,19 @@ export function GlossaryTable({
p="sm"
w="25px"
>
- <Tooltip label={t`Delete`}>
- <ActionIcon
- aria-label={t`Delete`}
- variant="subtle"
- c="text-tertiary"
- className={cx(S.action)}
- onClick={() => setDeletingItem(item)}
- >
- <Icon name="trash" />
- </ActionIcon>
- </Tooltip>
+ {!readOnly && (
+ <Tooltip label={t`Delete`}>
+ <ActionIcon
+ aria-label={t`Delete`}
+ variant="subtle"
+ c="text-tertiary"
+ className={cx(S.action)}
+ onClick={() => setDeletingItem(item)}
+ >
+ <Icon name="trash" />
+ </ActionIcon>
+ </Tooltip>
+ )}
</Box>
</>
)}
@@ -229,7 +237,7 @@ export function GlossaryTable({
onClose={() => setDeletingItem(null)}
onConfirm={async () => {
if (deletingItem) {
- await onDelete(deletingItem.id);
+ await onDelete?.(deletingItem.id);
setDeletingItem(null);
}
}}
diff --git a/frontend/src/metabase/common/components/Glossary/GlossaryTable.unit.spec.tsx b/frontend/src/metabase/common/components/Glossary/GlossaryTable.unit.spec.tsx
index e1e640992c4..286b7fcbacc 100644
--- a/frontend/src/metabase/common/components/Glossary/GlossaryTable.unit.spec.tsx
+++ b/frontend/src/metabase/common/components/Glossary/GlossaryTable.unit.spec.tsx
@@ -370,4 +370,31 @@ describe("GlossaryTable", () => {
screen.queryByRole("button", { name: /save/i }),
).not.toBeInTheDocument();
});
+
+ describe("readOnly", () => {
+ it("hides create, edit, and delete controls", async () => {
+ const user = userEvent.setup();
+
+ renderWithProviders(
+ <GlossaryTable
+ glossary={[makeItem({ id: 1, term: "Alpha", definition: "First" })]}
+ readOnly
+ />,
+ );
+
+ // No "New term" button
+ expect(
+ screen.queryByRole("button", { name: /new term/i }),
+ ).not.toBeInTheDocument();
+
+ // No delete action
+ expect(
+ screen.queryByRole("button", { name: /delete/i }),
+ ).not.toBeInTheDocument();
+
+ // Clicking a term does not open the inline editor
+ await user.click(screen.getByText("Alpha"));
+ expect(screen.queryByPlaceholderText(/boat/i)).not.toBeInTheDocument();
+ });
+ });
});
diff --git a/frontend/src/metabase/common/components/Glossary/types.ts b/frontend/src/metabase/common/components/Glossary/types.ts
new file mode 100644
index 00000000000..9875cf42a36
--- /dev/null
+++ b/frontend/src/metabase/common/components/Glossary/types.ts
@@ -0,0 +1 @@
+export type GlossaryField = "term" | "definition";
diff --git a/frontend/src/metabase/reference/glossary/GlossaryContainer.tsx b/frontend/src/metabase/reference/glossary/GlossaryContainer.tsx
index 7337d4a7cec..5579eac69b5 100644
--- a/frontend/src/metabase/reference/glossary/GlossaryContainer.tsx
+++ b/frontend/src/metabase/reference/glossary/GlossaryContainer.tsx
@@ -11,7 +11,9 @@ import S from "metabase/common/components/Glossary/Glossary.module.css";
import { GlossaryTable } from "metabase/common/components/Glossary/GlossaryTable";
import { SidebarLayout } from "metabase/common/components/SidebarLayout";
import CS from "metabase/css/core/index.css";
+import { useSelector } from "metabase/redux";
import BaseSidebar from "metabase/reference/guide/BaseSidebar";
+import { getUserIsAdmin, getUserIsAnalyst } from "metabase/selectors/user";
import { Card, Group, Stack, Text } from "metabase/ui";
export function GlossaryContainer() {
@@ -19,6 +21,9 @@ export function GlossaryContainer() {
const [createGlossary] = useCreateGlossaryMutation();
const [updateGlossary] = useUpdateGlossaryMutation();
const [deleteGlossary] = useDeleteGlossaryMutation();
+ const isAdmin = useSelector(getUserIsAdmin);
+ const isAnalyst = useSelector(getUserIsAnalyst);
+ const canManage = isAdmin || isAnalyst;
return (
<SidebarLayout
@@ -46,6 +51,7 @@ export function GlossaryContainer() {
<GlossaryTable
className={S.table}
glossary={glossary}
+ readOnly={!canManage}
onCreate={async (term, definition) => {
await createGlossary({ term, definition });
}}
diff --git a/src/metabase/glossary/api.clj b/src/metabase/glossary/api.clj
index 72c9ac50a4c..784ca9fd43e 100644
--- a/src/metabase/glossary/api.clj
+++ b/src/metabase/glossary/api.clj
@@ -34,6 +34,7 @@
{:keys [term definition]} :- [:map
[:term ms/NonBlankString]
[:definition ms/NonBlankString]]]
+ (api/check-data-analyst)
(let [glossary (t2/insert-returning-instance! :model/Glossary
{:term term
:definition definition
@@ -54,6 +55,7 @@
{:keys [term definition]} :- [:map
[:term ms/NonBlankString]
[:definition ms/NonBlankString]]]
+ (api/check-data-analyst)
(let [previous-glossary (api/check-404 (t2/select-one :model/Glossary :id id))]
(t2/update! :model/Glossary id {:term term :definition definition})
(let [glossary (t2/select-one :model/Glossary :id id)]
@@ -70,6 +72,7 @@
(api.macros/defendpoint :delete "/:id"
"Delete a glossary entry."
[{:keys [id]} :- [:map [:id ms/PositiveInt]]]
+ (api/check-data-analyst)
(let [glossary (api/check-404 (t2/select-one :model/Glossary :id id))]
(t2/delete! :model/Glossary :id id)
(events/publish-event! :event/glossary-delete
diff --git a/test/metabase/glossary/api_test.clj b/test/metabase/glossary/api_test.clj
index 66095d4d905..5907cf566f5 100644
--- a/test/metabase/glossary/api_test.clj
+++ b/test/metabase/glossary/api_test.clj
@@ -3,6 +3,7 @@
(:require
[clojure.set :as set]
[clojure.test :refer :all]
+ [metabase.permissions.models.permissions-group :as perms-group]
[metabase.test :as mt]
[toucan2.core :as t2]))
@@ -92,3 +93,39 @@
(testing "returns 404 when deleting non-existent entry"
(is (= "Not found."
(mt/user-http-request :crowberto :delete 404 "glossary/99999")))))))
+
+(deftest glossary-authorization-test
+ (testing "Only data analysts (or superusers) can mutate glossary entries"
+ (mt/with-model-cleanup [:model/Glossary]
+ (mt/with-temp [:model/Glossary {gid :id} {:term "Existing"
+ :definition "An existing entry"
+ :creator_id (mt/user->id :crowberto)}]
+ (testing "regular authenticated users cannot mutate the glossary"
+ (testing "POST is forbidden"
+ (is (= "You don't have permissions to do that."
+ (mt/user-http-request :rasta :post 403 "glossary"
+ {:term "Hacked" :definition "Tampered"}))))
+ (testing "PUT is forbidden"
+ (is (= "You don't have permissions to do that."
+ (mt/user-http-request :rasta :put 403 (str "glossary/" gid)
+ {:term "Hacked" :definition "Tampered"}))))
+ (testing "DELETE is forbidden"
+ (is (= "You don't have permissions to do that."
+ (mt/user-http-request :rasta :delete 403 (str "glossary/" gid)))))
+ (testing "the entry was not modified"
+ (is (=? {:term "Existing" :definition "An existing entry"}
+ (t2/select-one :model/Glossary :id gid)))))
+ (testing "regular users can still read the glossary"
+ (is (mt/user-http-request :rasta :get 200 "glossary")))))
+ (testing "data analysts can mutate the glossary"
+ (mt/with-model-cleanup [:model/Glossary]
+ (mt/with-temp [:model/User {analyst-id :id} {:is_data_analyst true}
+ :model/PermissionsGroupMembership _ {:user_id analyst-id
+ :group_id (:id (perms-group/data-analyst))}]
+ (let [{gid :id} (mt/user-http-request analyst-id :post 200 "glossary"
+ {:term "Analyst" :definition "Created by analyst"})]
+ (is (pos-int? gid))
+ (is (=? {:definition "Edited by analyst"}
+ (mt/user-http-request analyst-id :put 200 (str "glossary/" gid)
+ {:term "Analyst" :definition "Edited by analyst"})))
+ (is (nil? (mt/user-http-request analyst-id :delete 204 (str "glossary/" gid))))))))))