The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
add events when making cards/dash public or retracting (#64582)
dpsutton0aa02bdc3659
diff --git a/src/metabase/audit_app/events/audit_log.clj b/src/metabase/audit_app/events/audit_log.cljindex cdd765357e4..3a64b167a4b 100644--- a/src/metabase/audit_app/events/audit_log.clj+++ b/src/metabase/audit_app/events/audit_log.clj@@ -49,6 +49,23 @@:model :model/Dashboard:model-id (u/id object)})))+(derive ::publicize ::event)+(derive ::publicize-card ::publicize)+(derive ::publicize-dashboard ::publicize)+(derive :event/card-public-link-created ::publicize-card)+(derive :event/card-public-link-deleted ::publicize-card)+(derive :event/dashboard-public-link-created ::publicize-dashboard)+(derive :event/dashboard-public-link-deleted ::publicize-dashboard)++(methodical/defmethod events/publish-event! ::publicize+ [topic {:keys [user-id object-id] :as _event}]+ (audit-log/record-event! topic+ {:user-id user-id+ :model (if (isa? topic ::publicize-dashboard)+ :model/Dashboard+ :model/Card)+ :model-id object-id}))+(derive ::table-event ::event)(derive :event/table-manual-scan ::table-event)(derive :event/table-manual-sync ::table-event)diff --git a/src/metabase/dashboards_rest/api.clj b/src/metabase/dashboards_rest/api.cljindex 491d03972fb..816c8a90fd0 100644--- a/src/metabase/dashboards_rest/api.clj+++ b/src/metabase/dashboards_rest/api.clj@@ -1129,11 +1129,16 @@(api/check-superuser)(public-sharing.validation/check-public-sharing-enabled)(api/check-not-archived (api/read-check :model/Dashboard dashboard-id))- {:uuid (or (t2/select-one-fn :public_uuid :model/Dashboard :id dashboard-id)- (u/prog1 (str (random-uuid))- (t2/update! :model/Dashboard dashboard-id- {:public_uuid <>- :made_public_by_id api/*current-user-id*})))})+ (let [existing-public-uuid (t2/select-one-fn :public_uuid :model/Dashboard :id dashboard-id)+ uuid (or existing-public-uuid+ (u/prog1 (str (random-uuid))+ (events/publish-event! :event/dashboard-public-link-created+ {:object-id dashboard-id+ :user-id api/*current-user-id*})+ (t2/update! :model/Dashboard dashboard-id+ {:public_uuid <>+ :made_public_by_id api/*current-user-id*})))]+ {:uuid uuid}));; TODO (Cam 10/28/25) -- fix this endpoint route to use kebab-case for consistency with the rest of our REST API;;@@ -1152,6 +1157,9 @@(t2/update! :model/Dashboard dashboard-id{:public_uuid nil:made_public_by_id nil})+ (events/publish-event! :event/dashboard-public-link-deleted+ {:object-id dashboard-id+ :user-id api/*current-user-id*}){:status 204, :body nil});; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers todiff --git a/src/metabase/events/schema.clj b/src/metabase/events/schema.cljindex a1aca130a70..c78e1325647 100644--- a/src/metabase/events/schema.clj+++ b/src/metabase/events/schema.clj@@ -77,6 +77,16 @@[:user-id [:maybe pos-int?]][:object-id [:maybe pos-int?]]])+(mr/def ::publicize+ [:map {:closed true}+ [:user-id pos-int?]+ [:object-id pos-int?]])++(mr/def :event/dashboard-public-link-created ::publicize)+(mr/def :event/dashboard-public-link-deleted ::publicize)+(mr/def :event/card-public-link-created ::publicize)+(mr/def :event/card-public-link-deleted ::publicize)+;; user events(mr/def ::userdiff --git a/src/metabase/queries/api/card.clj b/src/metabase/queries/api/card.cljindex 38c6f99fa1f..e82b1c3f19c 100644--- a/src/metabase/queries/api/card.clj+++ b/src/metabase/queries/api/card.clj@@ -953,12 +953,16 @@(perms/check-has-application-permission :setting)(public-sharing.validation/check-public-sharing-enabled)(api/check-not-archived (api/read-check :model/Card card-id))- (let [{existing-public-uuid :public_uuid} (t2/select-one [:model/Card :public_uuid :card_schema] :id card-id)]- {:uuid (or existing-public-uuid- (u/prog1 (str (random-uuid))- (t2/update! :model/Card card-id- {:public_uuid <>- :made_public_by_id api/*current-user-id*})))}))+ (let [{existing-public-uuid :public_uuid} (t2/select-one [:model/Card :public_uuid :card_schema] :id card-id)+ uuid (or existing-public-uuid+ (u/prog1 (str (random-uuid))+ (events/publish-event! :event/card-public-link-created+ {:object-id card-id+ :user-id api/*current-user-id*})+ (t2/update! :model/Card card-id+ {:public_uuid <>+ :made_public_by_id api/*current-user-id*})))]+ {:uuid uuid}));; TODO (Cam 10/28/25) -- fix this endpoint route to use kebab-case for consistency with the rest of our REST API;;@@ -977,6 +981,9 @@(t2/update! :model/Card card-id{:public_uuid nil:made_public_by_id nil})+ (events/publish-event! :event/card-public-link-deleted+ {:object-id card-id+ :user-id api/*current-user-id*}){:status 204, :body nil});; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers todiff --git a/src/metabase/queries/events/schema.clj b/src/metabase/queries/events/schema.cljindex d7c11d5d8fa..ba6146185ec 100644--- a/src/metabase/queries/events/schema.clj+++ b/src/metabase/queries/events/schema.clj@@ -25,3 +25,13 @@[:card-id pos-int?][:user-id [:maybe pos-int?]][:context {:optional true} :any]])++(mr/def :event/card-public-link-created+ [:map {:closed true}+ [:user-id pos-int?]+ [:object-id pos-int?]])++(mr/def :event/card-public-link-deleted+ [:map {:closed true}+ [:user-id pos-int?]+ [:object-id pos-int?]])diff --git a/test/metabase/queries/api/card_test.clj b/test/metabase/queries/api/card_test.cljindex ee3c8aca64a..4f8ec78c56f 100644--- a/test/metabase/queries/api/card_test.clj+++ b/test/metabase/queries/api/card_test.clj@@ -2975,6 +2975,48 @@(is (= "Not found."(mt/user-http-request :crowberto :delete 404 (format "card/%d/public_link" Integer/MAX_VALUE))))))))+(deftest share-card-audit-log-test+ (testing "POST /api/card/:id/public_link creates audit log entry"+ (mt/with-premium-features #{:audit-app}+ (mt/with-temporary-setting-values [enable-public-sharing true]+ (mt/with-temp [:model/Card {card-id :id :as _card}]+ (mt/user-http-request :crowberto :post 200 (format "card/%d/public_link" card-id))+ (is (partial=+ {:topic :card-public-link-created+ :user_id (mt/user->id :crowberto)+ :model "Card"+ :model_id card-id}+ (mt/latest-audit-log-entry :card-public-link-created card-id)))++ (testing "Does not create duplicate audit log entry when returning existing public link"+ (let [audit-log-count-before (count (mt/all-entries-for :card-public-link-created+ :model/Card+ card-id))]+ (mt/user-http-request :crowberto :post 200 (format "card/%d/public_link" card-id))+ (is (= audit-log-count-before+ (count (mt/all-entries-for :card-public-link-created+ :model/Card+ card-id)))+ "Should not create additional audit log entry for existing public link"))))))))++(deftest unshare-card-audit-log-test+ (testing "DELETE /api/card/:id/public_link creates audit log entry"+ (mt/with-premium-features #{:audit-app}+ (mt/with-temporary-setting-values [enable-public-sharing true]+ (mt/with-temp [:model/Card {card-id :id :as _card} (shared-card)]+ (mt/user-http-request :crowberto :delete 204 (format "card/%d/public_link" card-id))+ (is (partial=+ {:topic :card-public-link-deleted+ :user_id (mt/user->id :crowberto)+ :model "Card"+ :model_id card-id}+ (mt/latest-audit-log-entry :card-public-link-deleted card-id)))+ (testing "Does not create duplicate audit entries if not public"+ (let [all-events-before (mt/all-entries-for nil :model/Card card-id)]+ (mt/user-http-request :crowberto :delete 404 (format "card/%d/public_link" card-id))+ (is (= all-events-before+ (mt/all-entries-for nil :model/Card card-id))))))))))+(deftest test-that-we-can-fetch-a-list-of-publicly-accessible-cards(testing "GET /api/card/public"(mt/with-temporary-setting-values [enable-public-sharing true]diff --git a/test/metabase/test.clj b/test/metabase/test.cljindex 1bf82d7b794..f941fd7e11c 100644--- a/test/metabase/test.clj+++ b/test/metabase/test.clj@@ -267,6 +267,7 @@format-env-keypriv-key->base64-urilatest-audit-log-entry+ all-entries-forlet-urlmetric-valueobj->json->objdiff --git a/test/metabase/test/util.clj b/test/metabase/test/util.cljindex 00f7bc2028f..79416ddec18 100644--- a/test/metabase/test/util.clj+++ b/test/metabase/test/util.clj@@ -1586,6 +1586,19 @@:where [:and (when topic [:= :topic (name topic)])(when model-id [:= :model_id model-id])]})))+(defn all-entries-for+ "Return all audit log entries for a particular object. If you omit the topic, will get all audit logs. You must+ provide a model so we can disambiguate dash 4 from card 4."+ [topic model model-id]+ (assert (int? model-id) "Must provide an integer id for the model")+ (assert (isa? model :metabase/model))+ (t2/select-one [:model/AuditLog :topic :user_id :model :model_id :details]+ {:order-by [[:id :desc]]+ :where [:and+ [:= :model (name model)]+ [:= :model_id model-id]+ (when topic [:= :topic (name topic)])]}))+(defn repeat-concurrently"Run `f` `n` times concurrently. Returns a vector of the results of each invocation of `f`."[n f]