Skip to content

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

Don't allow attachments if the creator doesn't have permission to download (#66827)

Nathan Voxland0c01ed9259d4
diff --git a/enterprise/backend/src/metabase_enterprise/advanced_permissions/models/permissions/data_permissions.clj b/enterprise/backend/src/metabase_enterprise/advanced_permissions/models/permissions/data_permissions.clj
new file mode 100644
index 00000000000..78432854194
--- /dev/null
+++ b/enterprise/backend/src/metabase_enterprise/advanced_permissions/models/permissions/data_permissions.clj
@@ -0,0 +1,39 @@
+(ns metabase-enterprise.advanced-permissions.models.permissions.data-permissions
+ (:require
+ [metabase.lib.core :as lib]
+ [metabase.lib.schema :as lib.schema]
+ [metabase.permissions.core :as perms]
+ [metabase.premium-features.core :refer [defenterprise]]
+ [metabase.query-permissions.core :as query-perms]
+ [metabase.util.malli :as mu]))
+
+(defmulti download-perms-level*
+ "Returns user's download permission level for the given query."
+ {:arglists '([mbql5-query user-id])}
+ (mu/fn [query :- ::lib.schema/query _user-id]
+ (:lib/type (lib/query-stage query -1))))
+
+(defmethod download-perms-level* :mbql.stage/native
+ [{database-id :database, :as _query} user-id]
+ (perms/native-download-permission-for-user user-id database-id))
+
+(mu/defmethod download-perms-level* :mbql.stage/mbql
+ [{db-id :database, :as query} :- ::lib.schema/query user-id]
+ (let [{:keys [table-ids native?]} (query-perms/query->source-ids query)
+ perms (if (or native? (lib/any-native-stage-not-introduced-by-sandbox? query))
+ ;; If we detect any native subqueries/joins, even with source-card IDs, require full native
+ ;; download perms
+ #{(perms/native-download-permission-for-user user-id db-id)}
+ (set (map (fn table-perms-lookup [table-id]
+ (perms/table-permission-for-user user-id :perms/download-results db-id table-id))
+ table-ids)))]
+ ;; The download perm level for a query should be equal to the lowest perm level of any table referenced by the query.
+ (or (perms :no)
+ (perms :ten-thousand-rows)
+ :one-million-rows)))
+
+(defenterprise download-perms-level
+ "The user's permission level for the given query"
+ :feature :advanced-permissions
+ [query user-id]
+ (download-perms-level* query user-id))
diff --git a/enterprise/backend/src/metabase_enterprise/advanced_permissions/query_processor/middleware/permissions.clj b/enterprise/backend/src/metabase_enterprise/advanced_permissions/query_processor/middleware/permissions.clj
index 8aead4b98fc..9dc12650b3b 100644
--- a/enterprise/backend/src/metabase_enterprise/advanced_permissions/query_processor/middleware/permissions.clj
+++ b/enterprise/backend/src/metabase_enterprise/advanced_permissions/query_processor/middleware/permissions.clj
@@ -6,7 +6,6 @@
[metabase.lib.schema :as lib.schema]
[metabase.permissions.core :as perms]
[metabase.premium-features.core :refer [defenterprise-schema]]
- [metabase.query-permissions.core :as query-perms]
[metabase.query-processor.error-type :as qp.error-type]
[metabase.query-processor.schema :as qp.schema]
[metabase.util.i18n :refer [tru]]
@@ -19,30 +18,6 @@
[query :- ::qp.schema/any-query]
(some-> query :info :context name (str/includes? "download")))
-(defmulti ^:private current-user-download-perms-level
- {:arglists '([mbql5-query])}
- (mu/fn [query :- ::lib.schema/query]
- (:lib/type (lib/query-stage query -1))))
-
-(defmethod current-user-download-perms-level :mbql.stage/native
- [{database-id :database, :as _query}]
- (perms/native-download-permission-for-user api/*current-user-id* database-id))
-
-(mu/defmethod current-user-download-perms-level :mbql.stage/mbql
- [{db-id :database, :as query} :- ::lib.schema/query]
- (let [{:keys [table-ids native?]} (query-perms/query->source-ids query)
- perms (if (or native? (lib/any-native-stage-not-introduced-by-sandbox? query))
- ;; If we detect any native subqueries/joins, even with source-card IDs, require full native
- ;; download perms
- #{(perms/native-download-permission-for-user api/*current-user-id* db-id)}
- (set (map (fn table-perms-lookup [table-id]
- (perms/table-permission-for-user api/*current-user-id* :perms/download-results db-id table-id))
- table-ids)))]
- ;; The download perm level for a query should be equal to the lowest perm level of any table referenced by the query.
- (or (perms :no)
- (perms :ten-thousand-rows)
- :one-million-rows)))
-
(defenterprise-schema apply-download-limit :- ::lib.schema/query
"Pre-processing middleware to apply row limits to MBQL export queries if the user has `ten-thousand-rows` download
perms. This does not apply to native queries, which are instead limited by the [[limit-download-result-rows]]
@@ -52,7 +27,7 @@
(cond-> query
(and (is-download? query)
(= (:lib/type (lib/query-stage query -1)) :mbql.stage/mbql)
- (= (current-user-download-perms-level query) :ten-thousand-rows))
+ (= (perms/download-perms-level query api/*current-user-id*) :ten-thousand-rows))
(lib/limit ((fnil min Integer/MAX_VALUE) (lib/current-limit query -1) max-rows-in-limited-downloads))))
(defenterprise-schema limit-download-result-rows :- ::qp.schema/rff
@@ -63,7 +38,7 @@
[query :- ::lib.schema/query
rff :- ::qp.schema/rff]
(if (and (is-download? query)
- (= (current-user-download-perms-level query) :ten-thousand-rows))
+ (= (perms/download-perms-level query api/*current-user-id*) :ten-thousand-rows))
(fn limit-download-result-rows* [metadata]
((take max-rows-in-limited-downloads) (rff metadata)))
rff))
@@ -79,7 +54,7 @@
(mu/fn [query :- ::lib.schema/query
rff :- ::qp.schema/rff]
(let [download-perms-level (if api/*current-user-id*
- (current-user-download-perms-level query)
+ (perms/download-perms-level query api/*current-user-id*)
;; If no user is bound, assume full download permissions (e.g. for public questions)
:one-million-rows)]
(when (and (is-download? query)
diff --git a/enterprise/backend/test/metabase_enterprise/advanced_permissions/api/subscription_test.clj b/enterprise/backend/test/metabase_enterprise/advanced_permissions/api/subscription_test.clj
index ee20892d8ea..c5447a30451 100644
--- a/enterprise/backend/test/metabase_enterprise/advanced_permissions/api/subscription_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/advanced_permissions/api/subscription_test.clj
@@ -20,8 +20,12 @@
(testing "/api/pulse/*"
(with-subscription-disabled-for-all-users!
(mt/with-user-in-groups [group {:name "New Group"}
- user [group]]
- (mt/with-temp [:model/Card card {}
+ user [group]]
+ (mt/with-temp [:model/Card card {:dataset_query (mt/mbql-query products
+ {:aggregation [[:sum $price]
+ [:avg $price]]
+ :breakout [$category
+ !year.created_at]})}
:model/Pulse pulse {:creator_id (u/the-id user)}]
(let [pulse-default {:name "A Pulse"
:cards [{:id (:id card)
diff --git a/frontend/src/metabase-types/api/card.ts b/frontend/src/metabase-types/api/card.ts
index a087752aab2..a00549b6c39 100644
--- a/frontend/src/metabase-types/api/card.ts
+++ b/frontend/src/metabase-types/api/card.ts
@@ -24,14 +24,15 @@ import type {
ParameterId,
ParameterValueOrArray,
} from "./parameters";
+import type { DownloadPermission } from "./permissions";
import type { DatasetQuery, FieldReference, PublicDatasetQuery } from "./query";
import type { CollectionEssentials } from "./search";
import type { Table, TableId } from "./table";
import type { UserInfo } from "./user";
import type { CardDisplayType, VisualizationDisplay } from "./visualization";
import type { SmartScalarComparison } from "./visualization-settings";
-export type CardType = "model" | "question" | "metric";
+export type CardType = "model" | "question" | "metric";
export type CardDashboardInfo = Pick<Dashboard, "id" | "name">;
export type CardDocumentInfo = Pick<Document, "id" | "name">;
@@ -83,6 +84,8 @@ export interface Card<Q extends DatasetQuery = DatasetQuery>
creator?: UserInfo;
"last-edit-info"?: LastEditInfo;
table_id?: TableId;
+
+ download_perms?: DownloadPermission;
}
export interface PublicCard {
diff --git a/frontend/src/metabase/notifications/AddEditSidebar/AddEditEmailSidebar.tsx b/frontend/src/metabase/notifications/AddEditSidebar/AddEditEmailSidebar.tsx
index 385fe31bf98..115efa429b6 100644
--- a/frontend/src/metabase/notifications/AddEditSidebar/AddEditEmailSidebar.tsx
+++ b/frontend/src/metabase/notifications/AddEditSidebar/AddEditEmailSidebar.tsx
@@ -4,6 +4,7 @@ import { t } from "ttag";
import _ from "underscore";
import { getCurrentUser } from "metabase/admin/datamodel/selectors";
+import { DataPermissionValue } from "metabase/admin/permissions/types";
import SchedulePicker, {
type ScheduleChangeProp,
} from "metabase/common/components/SchedulePicker";
@@ -85,6 +86,11 @@ export const AddEditEmailSidebar = ({
const userCanAccessSettings = useSelector(canAccessSettings);
const currentUser = useSelector(getCurrentUser);
+ // Return true if the results of all cards can be downloaded
+ const allowDownload = pulse.cards?.every(
+ (card) => card.download_perms !== DataPermissionValue.NONE,
+ );
+
useEffect(() => {
if (isEmbeddingSdk()) {
onChannelPropertyChange("recipients", [currentUser]);
@@ -187,6 +193,7 @@ export const AddEditEmailSidebar = ({
cards={pulse.cards}
pulse={pulse}
setPulse={setPulse}
+ allowDownload={allowDownload}
/>
{pulse.id != null && (
<DeleteSubscriptionAction
diff --git a/frontend/src/metabase/notifications/DashboardSubscriptionsSidebar/DashboardSubscriptionsSidebar.jsx b/frontend/src/metabase/notifications/DashboardSubscriptionsSidebar/DashboardSubscriptionsSidebar.jsx
index e41b244c893..67782f1ad7b 100644
--- a/frontend/src/metabase/notifications/DashboardSubscriptionsSidebar/DashboardSubscriptionsSidebar.jsx
+++ b/frontend/src/metabase/notifications/DashboardSubscriptionsSidebar/DashboardSubscriptionsSidebar.jsx
@@ -67,6 +67,7 @@ const cardsToPulseCards = (cards, pulseCards) => {
pivot_results: pulseCard.pivot_results,
include_csv: pulseCard.include_csv,
include_xls: pulseCard.include_xls,
+ download_perms: pulseCard.download_perms,
};
});
};
diff --git a/frontend/src/metabase/notifications/DashboardSubscriptionsSidebar/get-supported-cards-for-subscriptions.tsx b/frontend/src/metabase/notifications/DashboardSubscriptionsSidebar/get-supported-cards-for-subscriptions.tsx
index 17245e2a20c..d47d52a494f 100644
--- a/frontend/src/metabase/notifications/DashboardSubscriptionsSidebar/get-supported-cards-for-subscriptions.tsx
+++ b/frontend/src/metabase/notifications/DashboardSubscriptionsSidebar/get-supported-cards-for-subscriptions.tsx
@@ -23,6 +23,7 @@ const cardsFromDashboard = (dashboard?: Dashboard) => {
dashboard_card_id: card.id,
dashboard_id: dashboard.id,
parameter_mappings: card.parameter_mappings,
+ download_perms: card.card.download_perms,
}));
};
diff --git a/frontend/src/metabase/notifications/EmailAttachmentPicker.jsx b/frontend/src/metabase/notifications/EmailAttachmentPicker.jsx
index 2c5f78a1e91..d3d12aac4c0 100644
--- a/frontend/src/metabase/notifications/EmailAttachmentPicker.jsx
+++ b/frontend/src/metabase/notifications/EmailAttachmentPicker.jsx
@@ -7,7 +7,7 @@ import _ from "underscore";
import { ExportSettingsWidget } from "metabase/common/components/ExportSettingsWidget";
import Toggle from "metabase/common/components/Toggle";
import CS from "metabase/css/core/index.css";
-import { Box, Checkbox, Group, Icon, Text } from "metabase/ui";
+import { Box, Checkbox, Group, Icon, Text, Tooltip } from "metabase/ui";
function getCardIdPair(card) {
return card.id + "|" + card.dashboard_card_id;
@@ -29,6 +29,7 @@ export default class EmailAttachmentPicker extends Component {
pulse: PropTypes.object.isRequired,
setPulse: PropTypes.func.isRequired,
cards: PropTypes.array.isRequired,
+ allowDownload: PropTypes.bool.isRequired,
};
componentDidMount() {
@@ -75,6 +76,18 @@ export default class EmailAttachmentPicker extends Component {
return this.props.cards.some((card) => card.display === "pivot");
}
+ canAttachFiles() {
+ const { allowDownload } = this.props;
+ return allowDownload;
+ }
+
+ getAttachmentDisabledReason() {
+ if (!this.canAttachFiles()) {
+ return t`You don't have permission to download results and therefore cannot attach files to subscriptions.`;
+ }
+ return null;
+ }
+
shouldUpdateState(newState, currentState) {
return (
(currentState.isEnabled || !newState.isEnabled) &&
@@ -267,6 +280,9 @@ export default class EmailAttachmentPicker extends Component {
selectedCardIds,
} = this.state;
+ const canAttachFiles = this.canAttachFiles();
+ const disabledReason = this.getAttachmentDisabledReason();
+
return (
<div>
<Group
@@ -274,6 +290,7 @@ export default class EmailAttachmentPicker extends Component {
justify="space-between"
pt="1.5rem"
pb="1.5rem"
+ opacity={canAttachFiles ? 1 : 0.6}
>
<Group position="left" gap="0">
<Text fw="bold">{t`Attach results as files`}</Text>
@@ -282,16 +299,22 @@ export default class EmailAttachmentPicker extends Component {
c="gray.6"
ml="0.5rem"
size={12}
- tooltip={t`Attachments can contain up to 2,000 rows of data.`}
+ tooltip={
+ disabledReason ||
+ t`Attachments can contain up to 2,000 rows of data.`
+ }
/>
</Group>
- <Toggle
- aria-label={t`Attach results`}
- value={isEnabled}
- onChange={this.toggleAttach}
- />
+ <Tooltip label={disabledReason} disabled={!disabledReason}>
+ <Toggle
+ aria-label={t`Attach results`}
+ value={isEnabled && canAttachFiles}
+ onChange={this.toggleAttach}
+ disabled={!canAttachFiles}
+ />
+ </Tooltip>
</Group>
- {isEnabled && (
+ {isEnabled && canAttachFiles && (
<div>
<Box py="1rem">
<ExportSettingsWidget
diff --git a/frontend/src/metabase/notifications/EmailAttachmentPicker.unit.spec.js b/frontend/src/metabase/notifications/EmailAttachmentPicker.unit.spec.js
index 00b630dce92..7dafc99cd1a 100644
--- a/frontend/src/metabase/notifications/EmailAttachmentPicker.unit.spec.js
+++ b/frontend/src/metabase/notifications/EmailAttachmentPicker.unit.spec.js
@@ -4,7 +4,11 @@ import { createMockState } from "metabase-types/store/mocks";
import EmailAttachmentPicker from "./EmailAttachmentPicker";
-function setup({ pulse = createPulse(), hasAttachments = false } = {}) {
+function setup({
+ pulse = createPulse(),
+ allowDownload = true,
+ hasAttachments = false,
+} = {}) {
const setPulse = jest.fn();
if (hasAttachments) {
@@ -22,6 +26,7 @@ function setup({ pulse = createPulse(), hasAttachments = false } = {}) {
cards={pulse.cards}
pulse={pulse}
setPulse={setPulse}
+ allowDownload={allowDownload}
/>,
{ storeInitialState: state },
);
@@ -38,6 +43,13 @@ describe("EmailAttachmentPicker", () => {
expect(toggle).not.toBeChecked();
});
+ it("should render disabled toggle if no access", () => {
+ setup({ allowDownload: false });
+ const toggle = screen.getByLabelText("Attach results");
+
+ expect(toggle).toBeDisabled();
+ });
+
it("should have a clickable toggle that reveals attachment type and a checkbox per question", () => {
setup();
diff --git a/src/metabase/channel/email/result_attachment.clj b/src/metabase/channel/email/result_attachment.clj
index 58d515be2e6..a161e079aed 100644
--- a/src/metabase/channel/email/result_attachment.clj
+++ b/src/metabase/channel/email/result_attachment.clj
@@ -4,8 +4,10 @@
[metabase.driver :as driver]
[metabase.driver.util :as driver.u]
[metabase.lib.schema.id :as lib.schema.id]
+ [metabase.permissions.core :as perms]
;; legacy usage -- don't do things like this going forward
- ^{:clj-kondo/ignore [:deprecated-namespace :discouraged-namespace]} [metabase.query-processor.store :as qp.store]
+ ^{:clj-kondo/ignore [:deprecated-namespace :discouraged-namespace]}
+ [metabase.query-processor.store :as qp.store]
[metabase.query-processor.streaming :as qp.streaming]
[metabase.query-processor.streaming.common :as streaming.common]
[metabase.query-processor.streaming.interface :as qp.si]
@@ -90,7 +92,8 @@
result :result
:as part}]
(when (and (or (:include_csv card) (:include_xls card))
- (pos-int? (:row_count result)))
+ (pos-int? (:row_count result))
+ (not= (perms/download-perms-level (:dataset_query card) (:creator_id card)) :no))
(let [maybe-realize-data-rows (requiring-resolve 'metabase.channel.shared/maybe-realize-data-rows)
result (:result (maybe-realize-data-rows part))
visualizer-title (when (and dashcard (get-in dashcard [:visualization_settings :visualization]))
diff --git a/src/metabase/dashboards_rest/api.clj b/src/metabase/dashboards_rest/api.clj
index 6ccd599b0e7..caaf183e728 100644
--- a/src/metabase/dashboards_rest/api.clj
+++ b/src/metabase/dashboards_rest/api.clj
@@ -298,6 +298,26 @@
;; If *dashboard-load-id* is set, the outer layer returns a forever-memoized wrapper around get-dashboard*.
;; If *dashboard-load-id* is nil, it returns the unwrapped get-dashboard*.
+(defn- set-download-perms-on-dashcards
+ "Set each dashcard's nested :card map :download_perms based on the current user's actual permissions."
+ [dashboard]
+ (update dashboard :dashcards
+ (fn [dashcards]
+ (vec (for [dashcard dashcards]
+ (update dashcard :card
+ (fn [card]
+ (when card
+ (let [dataset-query (or (:dataset_query card)
+ (t2/select-one-fn :dataset_query :model/Card :id (:id card)))
+ download-level (when dataset-query
+ (perms/download-perms-level dataset-query api/*current-user-id*))]
+ (assoc card :download_perms (case download-level
+ :no :none
+ :ten-thousand-rows :limited
+ :one-million-rows :full
+ :full :full
+ :none)))))))))))
+
;; TODO: This indirect memoization by *dashboard-load-id* could probably be turned into a macro for reuse elsewhere.
(defn- get-dashboard*
"Get Dashboard with ID."
@@ -311,7 +331,8 @@
collection.root/hydrate-root-collection
hide-unreadable-cards
add-query-average-durations
- (api/present-in-trash-if-archived-directly (collection/trash-collection-id)))))
+ (api/present-in-trash-if-archived-directly (collection/trash-collection-id))
+ set-download-perms-on-dashcards)))
(def ^:private get-dashboard-fn
(memoize/ttl (fn [dashboard-load-id]
diff --git a/src/metabase/permissions/core.clj b/src/metabase/permissions/core.clj
index 574e84ab63c..3027d7a5b14 100644
--- a/src/metabase/permissions/core.clj
+++ b/src/metabase/permissions/core.clj
@@ -39,6 +39,7 @@
[metabase.permissions.models.data-permissions
at-least-as-permissive?
disable-perms-cache
+ download-perms-level
full-db-permission-for-user
full-schema-permission-for-user
groups-have-permission-for-table?
diff --git a/src/metabase/permissions/models/data_permissions.clj b/src/metabase/permissions/models/data_permissions.clj
index 11f90216319..fb800393ba7 100644
--- a/src/metabase/permissions/models/data_permissions.clj
+++ b/src/metabase/permissions/models/data_permissions.clj
@@ -979,3 +979,9 @@
(set-table-permission! group_id table perm_type perm_value))
;; These perms can be inserted raw, and don't require changes to existing perms in the DB
(t2/insert! :model/DataPermissions simple-perms)))))
+
+(defenterprise download-perms-level
+ "Return the download permission for the query that the given user has. OSS returns :full"
+ metabase-enterprise.advanced-permissions.models.permissions.data-permissions
+ [_query _user-id]
+ :full)
diff --git a/src/metabase/pulse/api/pulse.clj b/src/metabase/pulse/api/pulse.clj
index 5e57caf9597..190d8cdf81b 100644
--- a/src/metabase/pulse/api/pulse.clj
+++ b/src/metabase/pulse/api/pulse.clj
@@ -125,7 +125,18 @@
pulses (if creator-or-recipient
(map maybe-strip-sensitive-metadata pulses)
pulses)]
- (t2/hydrate pulses :can_write)))
+ (mapv
+ (fn [pulse]
+ (update pulse :cards
+ (fn [cards]
+ (mapv (fn [card] (assoc card :download_perms (case (perms/download-perms-level
+ (or (:dataset_query card) (t2/select-one-fn :dataset_query [:model/Card :dataset_query] (:id card)))
+ api/*current-user-id*)
+ :no :none
+ :ten-thousand-rows :limited
+ :one-million-rows :full
+ :full :full))) cards))))
+ (t2/hydrate pulses :can_write))))
;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to
;; use our API + we will need it when we make auto-TypeScript-signature generation happen
diff --git a/src/metabase/pulse/models/pulse.clj b/src/metabase/pulse/models/pulse.clj
index 20c34c89e32..d2862b1aabf 100644
--- a/src/metabase/pulse/models/pulse.clj
+++ b/src/metabase/pulse/models/pulse.clj
@@ -168,11 +168,21 @@
(and (mi/current-user-has-full-permissions? :read notification)
(current-user-is-creator? notification)))))
+(defn- can-read-attachments?
+ "Return true if the card has attachments and those can be read. If it has no attachments, return true"
+ [card]
+ (if (or (:include_csv card) (:include_xls card))
+ (let [level (perms/download-perms-level (or (:dataset_query card) (t2/select-one-fn :dataset_query :model/Card :id (:id card))) api/*current-user-id*)]
+ (and level (not= level :no)))
+ true))
+
(defmethod mi/can-create? :model/Pulse
[_m {cards :cards dashboard-id :dashboard_id collection-id :collection_id}]
(and
;; make sure we are allowed to *read* all the Cards we want to put in this Pulse
- (every? #(mi/can-read? :model/Card (u/the-id %)) cards)
+ (every? #(and
+ (mi/can-read? :model/Card (u/the-id %))
+ (can-read-attachments? %)) cards)
;; if we're trying to create this Pulse inside a Collection, and it is not a dashboard subscription,
;; make sure we have write permissions for that collection
(or (nil? collection-id) (mi/can-write? :model/Collection collection-id))
diff --git a/test/metabase/pulse/models/pulse_test.clj b/test/metabase/pulse/models/pulse_test.clj
index ac4662a9985..35978c80e01 100644
--- a/test/metabase/pulse/models/pulse_test.clj
+++ b/test/metabase/pulse/models/pulse_test.clj
@@ -3,6 +3,7 @@
[clojure.test :refer :all]
[medley.core :as m]
[metabase.api.common :as api]
+ [metabase.config.core :as config]
[metabase.models.interface :as mi]
[metabase.permissions.core :as perms]
[metabase.pulse.models.pulse :as models.pulse]
@@ -501,3 +502,74 @@
:creator_id (mt/user->id :crowberto)}]
(is (not (mi/can-read? subscription)))
(is (not (mi/can-write? subscription)))))))))
+
+(deftest create-permissions-test
+ (when config/ee-available?
+ (testing "A user without download permission should not be able to create a pulse with attachments"
+ (mt/with-temp [:model/User {user-id :id} {}
+ :model/Database {db-id :id} {:engine :h2}
+ :model/Table {table-id :id} {:db_id db-id}
+ :model/Collection {collection-id :id} {:name "Test Collection"}
+ :model/Card card {:dataset_query {:database db-id
+ :type :query
+ :collection_id collection-id
+ :query {:source-table table-id}}}
+ :model/PermissionsGroup {group-id :id} {}
+ :model/PermissionsGroupMembership _ {:user_id user-id :group_id group-id}]
+ ;; Grant read access on the card but no download permission
+ (mt/with-premium-features #{:advanced-permissions}
+ (perms/grant-permissions! group-id (perms/collection-read-path collection-id))
+ (perms/set-database-permission! group-id db-id :perms/view-data :unrestricted)
+ (perms/set-table-permission! group-id table-id :perms/create-queries :query-builder)
+ (perms/set-table-permission! group-id table-id :perms/download-results :no)
+ (perms/set-table-permission! (perms/all-users-group) table-id :perms/download-results :no)
+
+ (mt/with-current-user user-id
+ (testing "should not be able to create a pulse with CSV attachment"
+ (is (false? (mi/can-create? :model/Pulse
+ {:cards [(assoc card :include_csv true)]
+ :dashboard_id nil
+ :collection_id nil}))))
+
+ (testing "should not be able to create a pulse with XLS attachment"
+ (is (false? (mi/can-create? :model/Pulse
+ {:cards [(assoc card :include_xls true)]
+ :dashboard_id nil
+ :collection_id nil}))))
+
+ (testing "should be able to create a pulse without attachments"
+ (is (true? (mi/can-create? :model/Pulse
+ {:cards [card]
+ :dashboard_id nil
+ :collection_id nil}))))))))
+
+ (testing "A user with download permission should be able to create a pulse with attachments"
+ (mt/with-temp [:model/User {user-id :id} {}
+ :model/Database {db-id :id} {:engine :h2}
+ :model/Table {table-id :id} {:db_id db-id}
+ :model/Collection {collection-id :id} {:name "Test Collection"}
+ :model/Card card {:dataset_query {:database db-id
+ :collection_id collection-id
+ :type :query
+ :query {:source-table table-id}}}
+ :model/PermissionsGroup {group-id :id} {}
+ :model/PermissionsGroupMembership _ {:user_id user-id :group_id group-id}]
+ ;; Grant read and download permissions
+ (mt/with-premium-features #{:advanced-permissions}
+ (perms/grant-permissions! group-id (perms/collection-read-path collection-id))
+ (perms/set-database-permission! group-id db-id :perms/view-data :unrestricted)
+ (perms/set-table-permission! group-id table-id :perms/create-queries :query-builder)
+ (perms/set-table-permission! group-id table-id :perms/download-results :one-million-rows)
+
+ (mt/with-current-user user-id
+ (testing "should be able to create a pulse with CSV attachment"
+ (is (true? (mi/can-create? :model/Pulse
+ {:cards [(assoc card :include_csv true)]
+ :dashboard_id nil
+ :collection_id nil}))))
+
+ (testing "should be able to create a pulse with XLS attachment"
+ (is (true? (mi/can-create? :model/Pulse
+ {:cards [(assoc card :include_xls true)]
+ :dashboard_id nil
+ :collection_id nil}))))))))))