Skip to content

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

Analysts can do crud on segments and measures (#68501)

John Swanson590c8945fcaf
diff --git a/src/metabase/measures/models/measure.clj b/src/metabase/measures/models/measure.clj
index 7c544d9c531..7d86ee7dea0 100644
--- a/src/metabase/measures/models/measure.clj
+++ b/src/metabase/measures/models/measure.clj
@@ -61,13 +61,20 @@
([model pk]
(mi/can-read? (t2/select-one model pk))))
-;; Measures can be written by superusers, but only if the parent table is editable
-;; (not in a remote-synced collection in read-only mode).
+;; Measures can be written by superusers or data analysts with unrestricted view data permissions,
+;; but only if the parent table is editable (not in a remote-synced collection in read-only mode).
(defmethod mi/can-write? :model/Measure
([instance]
(let [table (or (:table instance)
(t2/select-one :model/Table :id (:table_id instance)))]
- (and (mi/superuser?)
+ (and (or api/*is-superuser?*
+ (and api/*is-data-analyst?*
+ (perms/user-has-permission-for-table?
+ api/*current-user-id*
+ :perms/view-data
+ :unrestricted
+ (:db_id table)
+ (u/the-id table))))
(remote-sync/table-editable? table))))
([model pk]
(mi/can-write? (t2/select-one model pk))))
@@ -78,7 +85,14 @@
[_model instance]
(let [table (or (:table instance)
(t2/select-one :model/Table :id (:table_id instance)))]
- (and (mi/superuser?)
+ (and (or api/*is-superuser?*
+ (and api/*is-data-analyst?*
+ (perms/user-has-permission-for-table?
+ api/*current-user-id*
+ :perms/view-data
+ :unrestricted
+ (:db_id table)
+ (u/the-id table))))
(remote-sync/table-editable? table))))
(methodical/defmethod t2/batched-hydrate [:model/Measure :can_write]
diff --git a/src/metabase/segments/models/segment.clj b/src/metabase/segments/models/segment.clj
index a66ca0d4ad4..089f4579b87 100644
--- a/src/metabase/segments/models/segment.clj
+++ b/src/metabase/segments/models/segment.clj
@@ -85,24 +85,42 @@
([model pk]
(mi/can-read? (t2/select-one model pk))))
-;; Segments can be written by superusers, but only if the parent table is editable
-;; (not in a remote-synced collection in read-only mode).
+;; Segments can be created by
+;; a) superusers
+;; b) OR data analysts with unrestricted view-data permissions
+;; But ONLY if the parent table is editable (not in a remote-synced collection in read-only mode).
(defmethod mi/can-write? :model/Segment
([instance]
(let [table (or (:table instance)
(t2/select-one :model/Table :id (:table_id instance)))]
- (and (mi/superuser?)
+ (and (or (mi/superuser?)
+ (and api/*is-data-analyst?*
+ (perms/user-has-permission-for-table?
+ api/*current-user-id*
+ :perms/view-data
+ :unrestricted
+ (:db_id table)
+ (u/the-id table))))
(remote-sync/table-editable? table))))
([model pk]
(mi/can-write? (t2/select-one model pk))))
-;; Segments can be created by superusers, but only if the parent table is editable
-;; (not in a remote-synced collection in read-only mode).
+;; Segments can be created by
+;; a) superusers
+;; b) OR data analysts with unrestricted view-data permissions
+;; But ONLY if the parent table is editable (not in a remote-synced collection in read-only mode).
(defmethod mi/can-create? :model/Segment
[_model instance]
(let [table (or (:table instance)
(t2/select-one :model/Table :id (:table_id instance)))]
- (and (mi/superuser?)
+ (and (or (mi/superuser?)
+ (and api/*is-data-analyst?*
+ (perms/user-has-permission-for-table?
+ api/*current-user-id*
+ :perms/view-data
+ :unrestricted
+ (:db_id table)
+ (u/the-id table))))
(remote-sync/table-editable? table))))
(methodical/defmethod t2/batched-hydrate [:model/Segment :can_write]
diff --git a/test/metabase/measures/models/measure_test.clj b/test/metabase/measures/models/measure_test.clj
index 6db880b4c26..7a9ab539603 100644
--- a/test/metabase/measures/models/measure_test.clj
+++ b/test/metabase/measures/models/measure_test.clj
@@ -3,7 +3,11 @@
[clojure.test :refer :all]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
+ [metabase.models.interface :as mi]
[metabase.models.serialization :as serdes]
+ [metabase.permissions.core :as perms]
+ [metabase.permissions.models.data-permissions :as data-perms]
+ [metabase.request.session :as session]
[metabase.test :as mt]
[toucan2.core :as t2]))
@@ -192,3 +196,83 @@
:type :query
:query {:source-table (mt/id :venues)
:aggregation [[:count]]}}})))))))
+
+;;; ------------------------------------------------ Permission Tests ------------------------------------------------
+
+(deftest can-write?-superuser-test
+ (testing "Superusers can write measures"
+ (mt/with-temp [:model/Measure measure {:name "Test Measure"
+ :table_id (mt/id :venues)
+ :creator_id (mt/user->id :rasta)
+ :definition (measure-definition (lib/count))}]
+ (mt/with-test-user :crowberto
+ (is (true? (mi/can-write? measure)))))))
+
+(deftest can-write?-analyst-unrestricted-test
+ (testing "Data analysts with unrestricted view-data can write measures"
+ (mt/with-no-data-perms-for-all-users!
+ (mt/with-temp [:model/PermissionsGroup {group-id :id} {}
+ :model/User {analyst-id :id} {:is_data_analyst true}
+ :model/Measure measure {:name "Test Measure"
+ :table_id (mt/id :venues)
+ :creator_id (mt/user->id :rasta)
+ :definition (measure-definition (lib/count))}]
+ (perms/add-user-to-group! analyst-id group-id)
+ (data-perms/set-table-permission! group-id (mt/id :venues) :perms/view-data :unrestricted)
+ (session/with-current-user analyst-id
+ (is (mi/can-write? measure)))))))
+
+(deftest can-write?-analyst-restricted-test
+ (testing "Data analysts without unrestricted view-data cannot write measures"
+ (mt/with-no-data-perms-for-all-users!
+ (mt/with-temp [:model/User {analyst-id :id} {:is_data_analyst true}
+ :model/Measure measure {:name "Test Measure"
+ :table_id (mt/id :venues)
+ :creator_id (mt/user->id :rasta)
+ :definition (measure-definition (lib/count))}]
+ (session/with-current-user analyst-id
+ (is (false? (mi/can-write? measure))))))))
+
+(deftest can-write?-non-analyst-test
+ (testing "Non-data-analysts cannot write measures"
+ (mt/with-temp [:model/Measure measure {:name "Test Measure"
+ :table_id (mt/id :venues)
+ :creator_id (mt/user->id :rasta)
+ :definition (measure-definition (lib/count))}]
+ (mt/with-test-user :rasta
+ (is (false? (mi/can-write? measure)))))))
+
+(deftest can-create?-superuser-test
+ (testing "Superusers can create measures"
+ (mt/with-test-user :crowberto
+ (is (true? (mi/can-create? :model/Measure {:name "Test Measure"
+ :table_id (mt/id :venues)
+ :definition (measure-definition (lib/count))}))))))
+
+(deftest can-create?-analyst-unrestricted-test
+ (testing "Data analysts with unrestricted view-data can create measures"
+ (mt/with-no-data-perms-for-all-users!
+ (mt/with-temp [:model/PermissionsGroup {group-id :id} {}
+ :model/User {analyst-id :id} {:is_data_analyst true}]
+ (perms/add-user-to-group! analyst-id group-id)
+ (data-perms/set-table-permission! group-id (mt/id :venues) :perms/view-data :unrestricted)
+ (session/with-current-user analyst-id
+ (is (true? (mi/can-create? :model/Measure {:name "Test Measure"
+ :table_id (mt/id :venues)
+ :definition (measure-definition (lib/count))}))))))))
+
+(deftest can-create?-analyst-restricted-test
+ (testing "Data analysts without unrestricted view-data cannot create measures"
+ (mt/with-no-data-perms-for-all-users!
+ (mt/with-temp [:model/User {analyst-id :id} {:is_data_analyst true}]
+ (session/with-current-user analyst-id
+ (is (false? (mi/can-create? :model/Measure {:name "Test Measure"
+ :table_id (mt/id :venues)
+ :definition (measure-definition (lib/count))}))))))))
+
+(deftest can-create?-non-analyst-test
+ (testing "Non-data-analysts cannot create measures"
+ (mt/with-test-user :rasta
+ (is (false? (mi/can-create? :model/Measure {:name "Test Measure"
+ :table_id (mt/id :venues)
+ :definition (measure-definition (lib/count))}))))))
diff --git a/test/metabase/segments/models/segment_test.clj b/test/metabase/segments/models/segment_test.clj
index a283ec98d7d..ad40fd08853 100644
--- a/test/metabase/segments/models/segment_test.clj
+++ b/test/metabase/segments/models/segment_test.clj
@@ -1,7 +1,11 @@
(ns metabase.segments.models.segment-test
(:require
[clojure.test :refer :all]
+ [metabase.models.interface :as mi]
[metabase.models.serialization :as serdes]
+ [metabase.permissions.core :as perms]
+ [metabase.permissions.models.data-permissions :as data-perms]
+ [metabase.request.session :as session]
[metabase.test :as mt]
[metabase.util.json :as json]
[toucan2.core :as t2]))
@@ -183,3 +187,83 @@
Exception
#"[Cc]ycle"
(t2/update! :model/Segment segment-1-id {:definition {:filter [:segment segment-2-id]}}))))))
+
+;;; ------------------------------------------------ Permission Tests ------------------------------------------------
+
+(deftest can-write?-superuser-test
+ (testing "Superusers can write segments"
+ (mt/with-temp [:model/Segment segment {:name "Test Segment"
+ :table_id (mt/id :venues)
+ :creator_id (mt/user->id :rasta)
+ :definition {:filter [:> [:field (mt/id :venues :price) nil] 2]}}]
+ (mt/with-test-user :crowberto
+ (is (true? (mi/can-write? segment)))))))
+
+(deftest can-write?-analyst-unrestricted-test
+ (testing "Data analysts with unrestricted view-data can write segments"
+ (mt/with-no-data-perms-for-all-users!
+ (mt/with-temp [:model/PermissionsGroup {group-id :id} {}
+ :model/User {analyst-id :id} {:is_data_analyst true}
+ :model/Segment segment {:name "Test Segment"
+ :table_id (mt/id :venues)
+ :creator_id (mt/user->id :rasta)
+ :definition {:filter [:> [:field (mt/id :venues :price) nil] 2]}}]
+ (perms/add-user-to-group! analyst-id group-id)
+ (data-perms/set-table-permission! group-id (mt/id :venues) :perms/view-data :unrestricted)
+ (session/with-current-user analyst-id
+ (is (mi/can-write? segment)))))))
+
+(deftest can-write?-analyst-restricted-test
+ (testing "Data analysts without unrestricted view-data cannot write segments"
+ (mt/with-no-data-perms-for-all-users!
+ (mt/with-temp [:model/User {analyst-id :id} {:is_data_analyst true}
+ :model/Segment segment {:name "Test Segment"
+ :table_id (mt/id :venues)
+ :creator_id (mt/user->id :rasta)
+ :definition {:filter [:> [:field (mt/id :venues :price) nil] 2]}}]
+ (session/with-current-user analyst-id
+ (is (false? (mi/can-write? segment))))))))
+
+(deftest can-write?-non-analyst-test
+ (testing "Non-data-analysts cannot write segments"
+ (mt/with-temp [:model/Segment segment {:name "Test Segment"
+ :table_id (mt/id :venues)
+ :creator_id (mt/user->id :rasta)
+ :definition {:filter [:> [:field (mt/id :venues :price) nil] 2]}}]
+ (mt/with-test-user :rasta
+ (is (false? (mi/can-write? segment)))))))
+
+(deftest can-create?-superuser-test
+ (testing "Superusers can create segments"
+ (mt/with-test-user :crowberto
+ (is (true? (mi/can-create? :model/Segment {:name "Test Segment"
+ :table_id (mt/id :venues)
+ :definition {:filter [:> [:field (mt/id :venues :price) nil] 2]}}))))))
+
+(deftest can-create?-analyst-unrestricted-test
+ (testing "Data analysts with unrestricted view-data can create segments"
+ (mt/with-no-data-perms-for-all-users!
+ (mt/with-temp [:model/PermissionsGroup {group-id :id} {}
+ :model/User {analyst-id :id} {:is_data_analyst true}]
+ (perms/add-user-to-group! analyst-id group-id)
+ (data-perms/set-table-permission! group-id (mt/id :venues) :perms/view-data :unrestricted)
+ (session/with-current-user analyst-id
+ (is (true? (mi/can-create? :model/Segment {:name "Test Segment"
+ :table_id (mt/id :venues)
+ :definition {:filter [:> [:field (mt/id :venues :price) nil] 2]}}))))))))
+
+(deftest can-create?-analyst-restricted-test
+ (testing "Data analysts without unrestricted view-data cannot create segments"
+ (mt/with-no-data-perms-for-all-users!
+ (mt/with-temp [:model/User {analyst-id :id} {:is_data_analyst true}]
+ (session/with-current-user analyst-id
+ (is (false? (mi/can-create? :model/Segment {:name "Test Segment"
+ :table_id (mt/id :venues)
+ :definition {:filter [:> [:field (mt/id :venues :price) nil] 2]}}))))))))
+
+(deftest can-create?-non-analyst-test
+ (testing "Non-data-analysts cannot create segments"
+ (mt/with-test-user :rasta
+ (is (false? (mi/can-create? :model/Segment {:name "Test Segment"
+ :table_id (mt/id :venues)
+ :definition {:filter [:> [:field (mt/id :venues :price) nil] 2]}}))))))