Skip to content

A platform for community discussion. Free, open, simple.

DEV: Add site setting to allow/disallow email invites (#37215)

Penar Musaraj3f17a7ee90c6
diff --git a/app/controllers/invites_controller.rb b/app/controllers/invites_controller.rb
index 41e9d79267d..02178ceae72 100644
--- a/app/controllers/invites_controller.rb
+++ b/app/controllers/invites_controller.rb
@@ -93,7 +93,7 @@ class InvitesController < ApplicationController
email: email,
description: params[:description],
domain: params[:domain],
- skip_email: params[:skip_email],
+ skip_email: skip_email_param,
invited_by: current_user,
custom_message: params["custom_message"],
max_redemptions_allowed: params[:max_redemptions_allowed],
@@ -149,7 +149,7 @@ class InvitesController < ApplicationController
email: params[:email],
description: params[:description],
domain: params[:domain],
- skip_email: params[:skip_email],
+ skip_email: skip_email_param,
invited_by: current_user,
custom_message: params[:custom_message],
max_redemptions_allowed: params[:max_redemptions_allowed],
@@ -275,6 +275,10 @@ class InvitesController < ApplicationController
end
if params[:send_email]
+ if !SiteSetting.allow_email_invites
+ return render_json_error(I18n.t("invite.email_invites_disabled"))
+ end
+
if invite.emailed_status != Invite.emailed_status_types[:pending]
begin
RateLimiter.new(current_user, "resend-invite-per-hour", 10, 1.hour).performed!
@@ -436,6 +440,10 @@ class InvitesController < ApplicationController
end
def resend_invite
+ if !SiteSetting.allow_email_invites
+ return render_json_error(I18n.t("invite.email_invites_disabled"))
+ end
+
params.require(:email)
RateLimiter.new(current_user, "resend-invite-per-hour", 10, 1.hour).performed!
@@ -448,6 +456,10 @@ class InvitesController < ApplicationController
end
def resend_all_invites
+ if !SiteSetting.allow_email_invites
+ return render_json_error(I18n.t("invite.email_invites_disabled"))
+ end
+
guardian.ensure_can_resend_all_invites!
begin
@@ -529,6 +541,10 @@ class InvitesController < ApplicationController
private
+ def skip_email_param
+ !SiteSetting.allow_email_invites || params[:skip_email]
+ end
+
def show_invite(invite)
email = Email.obfuscate(invite.email)
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index dc34f52177c..16298f7096a 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -2278,6 +2278,7 @@ en:
other {# days}
}.
edit_link_options: "Edit link options or send by email."
+ edit_link_options_only: "Edit link options"
show_advanced: "Show Advanced Options"
hide_advanced: "Hide Advanced Options"
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 998f427e874..3b489c91abb 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -431,6 +431,7 @@ en:
redemption_count_less_than_max: "should be less than %{max_redemptions_allowed}."
email_xor_domain: "Email and domain fields are not allowed at the same time"
existing_user_success: "Invite redeemed successfully"
+ email_invites_disabled: "Sending invite emails is disabled"
bulk_invite:
file_should_be_csv: "The uploaded file should be of csv format."
@@ -2318,7 +2319,7 @@ en:
strip_incoming_email_lines: "Remove leading and trailing whitespaces from each line of incoming emails."
disable_emails: "Prevent Discourse from sending any kind of emails. Select 'yes' to disable emails for all users. Select 'non-staff' to disable emails for non-staff users only."
-
+ allow_email_invites: "When disabled, users won't be able to send invite emails via Discourse"
strip_images_from_short_emails: "Strip images from emails having size less than 2800 Bytes"
short_email_length: "Define the maximum length, in bytes, for an email to be classified as 'short' for image suppression. If an email's size does not exceed this setting, any images (such as avatars and emojis) within the email will be stripped out."
display_name_on_email_from: "Display full names on email from fields"
diff --git a/config/site_settings.yml b/config/site_settings.yml
index e3dac13b98f..1ac607e0456 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -2078,6 +2078,10 @@ email:
require_change_email_confirmation:
default: false
area: "email"
+ allow_email_invites:
+ default: true
+ client: true
+ area: "email"
view_raw_email_allowed_groups:
type: group_list
list_type: compact
diff --git a/frontend/discourse/app/components/modal/create-invite.gjs b/frontend/discourse/app/components/modal/create-invite.gjs
index e3d6ba81ea3..c810bc25727 100644
--- a/frontend/discourse/app/components/modal/create-invite.gjs
+++ b/frontend/discourse/app/components/modal/create-invite.gjs
@@ -184,6 +184,16 @@ export default class CreateInvite extends Component {
return this.currentUser.staff && !this.siteSettings.must_approve_users;
}
+ get canSendEmailInvite() {
+ return this.isEmailInvite && this.siteSettings.allow_email_invites;
+ }
+
+ get linkOptionsLabel() {
+ return this.siteSettings.allow_email_invites
+ ? i18n("user.invited.invite.edit_link_options")
+ : i18n("user.invited.invite.edit_link_options_only");
+ }
+
get simpleMode() {
return !this.args.model.editing && !this.displayAdvancedOptions;
}
@@ -328,7 +338,7 @@ export default class CreateInvite extends Component {
tabindex="0"
{{on "click" this.showAdvancedMode}}
{{on "keydown" this.showAdvancedMode}}
- >{{i18n "user.invited.invite.edit_link_options"}}</a>
+ >{{this.linkOptionsLabel}}</a>
</p>
{{else}}
<Form
@@ -448,7 +458,7 @@ export default class CreateInvite extends Component {
</form.Field>
{{/if}}
- {{#if this.isEmailInvite}}
+ {{#if this.canSendEmailInvite}}
<form.Field
@name="customMessage"
@title={{i18n "user.invited.invite.custom_message"}}
@@ -486,7 +496,7 @@ export default class CreateInvite extends Component {
@disabled={{this.saving}}
class="btn-primary save-invite"
/>
- {{#if this.isEmailInvite}}
+ {{#if this.canSendEmailInvite}}
<DButton
@label={{if
this.inviteCreated
diff --git a/frontend/discourse/tests/integration/components/create-invite-test.gjs b/frontend/discourse/tests/integration/components/create-invite-test.gjs
index d4dbae46407..a077b7d70db 100644
--- a/frontend/discourse/tests/integration/components/create-invite-test.gjs
+++ b/frontend/discourse/tests/integration/components/create-invite-test.gjs
@@ -57,6 +57,28 @@ module("Integration | Component | CreateInvite", function (hooks) {
);
});
+ test("when allow_email_invites is disabled", async function (assert) {
+ const model = {};
+ this.siteSettings.allow_email_invites = false;
+
+ await render(
+ <template><CreateInvite @inline={{true}} @model={{model}} /></template>
+ );
+
+ await click(".edit-link-options");
+ await formKit().field("restrictTo").fillIn("[email protected]");
+
+ assert.false(
+ formKit().hasField("customMessage"),
+ "customMessage field is not shown when allow_email_invites is disabled"
+ );
+ assert
+ .dom(".save-invite-and-send-email")
+ .doesNotExist(
+ "'Create invite and send email' button is not shown when allow_email_invites is disabled"
+ );
+ });
+
test("the inviteToTopic field", async function (assert) {
const model = {};
this.currentUser.admin = true;
diff --git a/spec/requests/invites_controller_spec.rb b/spec/requests/invites_controller_spec.rb
index d0ca1f49eb0..c83590c33eb 100644
--- a/spec/requests/invites_controller_spec.rb
+++ b/spec/requests/invites_controller_spec.rb
@@ -483,6 +483,16 @@ RSpec.describe InvitesController do
end
end
+ context "when allow_email_invites is disabled" do
+ before { SiteSetting.allow_email_invites = false }
+
+ it "does not send invite email even without skip_email param" do
+ create_invite
+ expect(response).to have_http_status :ok
+ expect(Jobs::InviteEmail.jobs.size).to eq(0)
+ end
+ end
+
context "when validations fail" do
let(:email) { "[email protected]" }
@@ -743,6 +753,16 @@ RSpec.describe InvitesController do
expect(Jobs::InviteEmail.jobs.size).to eq(0)
end
end
+
+ context "when allow_email_invites is disabled" do
+ before { SiteSetting.allow_email_invites = false }
+
+ it "does not send invite emails even without skip_email param" do
+ create_multiple_invites
+ expect(response).to have_http_status :ok
+ expect(Jobs::InviteEmail.jobs.size).to eq(0)
+ end
+ end
end
it "fails if asked to generate too many invites at once" do
@@ -851,6 +871,16 @@ RSpec.describe InvitesController do
end
end
+ context "when allow_email_invites is disabled" do
+ before { SiteSetting.allow_email_invites = false }
+
+ it "returns an error when trying to send email" do
+ put "/invites/#{invite.id}", params: { send_email: true }
+ expect(response.status).to eq(422)
+ expect(response.parsed_body["errors"]).to include(I18n.t("invite.email_invites_disabled"))
+ end
+ end
+
context "when providing an email belonging to an existing user" do
subject(:update_invite) { put "/invites/#{invite.id}.json", params: { email: admin.email } }
@@ -1683,6 +1713,13 @@ RSpec.describe InvitesController do
expect(response.status).to eq(200)
expect(Jobs::InviteEmail.jobs.size).to eq(1)
end
+
+ it "returns an error when allow_email_invites is disabled" do
+ SiteSetting.allow_email_invites = false
+ post "/invites/reinvite.json", params: { email: invite.email }
+ expect(response.status).to eq(422)
+ expect(response.parsed_body["errors"]).to include(I18n.t("invite.email_invites_disabled"))
+ end
end
end
@@ -1725,6 +1762,14 @@ RSpec.describe InvitesController do
post "/invites/reinvite-all"
expect(response.parsed_body["errors"][0]).to eq(I18n.t("rate_limiter.slow_down"))
end
+
+ it "returns an error when allow_email_invites is disabled" do
+ SiteSetting.allow_email_invites = false
+ sign_in(admin)
+ post "/invites/reinvite-all"
+ expect(response.status).to eq(422)
+ expect(response.parsed_body["errors"]).to include(I18n.t("invite.email_invites_disabled"))
+ end
end
describe "#upload_csv" do