FEATURE: support disabling emails for non-staff users

This commit is contained in:
Arpit Jalan 2018-06-07 09:44:35 +05:30
parent d556975cdc
commit f9ab3848ed
15 changed files with 69 additions and 25 deletions

View File

@ -22,7 +22,7 @@ export default Ember.Component.extend(bufferedRender({
notices.push([I18n.t("read_only_mode.enabled"), 'alert-read-only']); notices.push([I18n.t("read_only_mode.enabled"), 'alert-read-only']);
} }
if (this.siteSettings.disable_emails) { if (this.siteSettings.disable_emails === "yes" || this.siteSettings.disable_emails === "non-staff") {
notices.push([I18n.t("emails_are_disabled"), 'alert-emails-disabled']); notices.push([I18n.t("emails_are_disabled"), 'alert-emails-disabled']);
} }

View File

@ -99,7 +99,7 @@ class Admin::BackupsController < Admin::AdminController
client_id: params.fetch(:client_id), client_id: params.fetch(:client_id),
publish_to_message_bus: true, publish_to_message_bus: true,
} }
SiteSetting.set_and_log(:disable_emails, true, current_user) SiteSetting.set_and_log(:disable_emails, 'yes', current_user)
BackupRestore.restore!(current_user.id, opts) BackupRestore.restore!(current_user.id, opts)
rescue BackupRestore::OperationRunningError rescue BackupRestore::OperationRunningError
render json: failed_json.merge(message: I18n.t("backup.operation_already_running")) render json: failed_json.merge(message: I18n.t("backup.operation_already_running"))

View File

@ -1486,7 +1486,7 @@ en:
alternative_reply_by_email_addresses: "List of alternative templates for reply by email incoming email addresses. Example: %{reply_key}@reply.example.com|replies+%{reply_key}@example.com" alternative_reply_by_email_addresses: "List of alternative templates for reply by email incoming email addresses. Example: %{reply_key}@reply.example.com|replies+%{reply_key}@example.com"
incoming_email_prefer_html: "Use HTML instead of text for incoming email." incoming_email_prefer_html: "Use HTML instead of text for incoming email."
disable_emails: "Prevent Discourse from sending any kind of 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."
strip_images_from_short_emails: "Strip images from emails having size less than 2800 Bytes" strip_images_from_short_emails: "Strip images from emails having size less than 2800 Bytes"
short_email_length: "Short email length in Bytes" short_email_length: "Short email length in Bytes"

View File

@ -783,8 +783,13 @@ email:
email_prefix: '' email_prefix: ''
email_site_title: '' email_site_title: ''
disable_emails: disable_emails:
default: false
client: true client: true
type: enum
default: 'no'
choices:
- 'no'
- 'yes'
- 'non-staff'
strip_images_from_short_emails: true strip_images_from_short_emails: true
short_email_length: 2800 short_email_length: 2800
display_name_on_email_from: display_name_on_email_from:

View File

@ -0,0 +1,11 @@
class MigrateDisableEmails < ActiveRecord::Migration[5.1]
def up
execute "UPDATE site_settings SET data_type = 7 WHERE name = 'disable_emails';"
execute "UPDATE site_settings SET value = 'yes' WHERE value = 't' AND name = 'disable_emails';"
execute "UPDATE site_settings SET value = 'no' WHERE value = 'f' AND name = 'disable_emails';"
end
def down
raise ActiveRecord::IrreversibleMigration
end
end

View File

@ -22,7 +22,7 @@ module Email
end end
def send def send
return if SiteSetting.disable_emails && @email_type.to_s != "admin_login" return if SiteSetting.disable_emails == "yes" && @email_type.to_s != "admin_login"
return if ActionMailer::Base::NullMail === @message return if ActionMailer::Base::NullMail === @message
return if ActionMailer::Base::NullMail === (@message.message rescue nil) return if ActionMailer::Base::NullMail === (@message.message rescue nil)
@ -30,6 +30,11 @@ module Email
return skip(I18n.t('email_log.message_blank')) if @message.blank? return skip(I18n.t('email_log.message_blank')) if @message.blank?
return skip(I18n.t('email_log.message_to_blank')) if @message.to.blank? return skip(I18n.t('email_log.message_to_blank')) if @message.to.blank?
if SiteSetting.disable_emails == "non-staff"
user = User.find_by_email(to_address)
return unless user && user.staff?
end
if @message.text_part if @message.text_part
return skip(I18n.t('email_log.text_part_body_blank')) if @message.text_part.body.to_s.blank? return skip(I18n.t('email_log.text_part_body_blank')) if @message.text_part.body.to_s.blank?
else else

View File

@ -55,7 +55,7 @@ class BulkImport::Vanilla < BulkImport::Base
# SiteSetting.port = 3000 # SiteSetting.port = 3000
# SiteSetting.automatic_backups_enabled = false # SiteSetting.automatic_backups_enabled = false
# SiteSetting.disable_emails = true # SiteSetting.disable_emails = "non-staff"
# etc. # etc.
import_users import_users

View File

@ -75,7 +75,7 @@ class ImportScripts::Base
min_personal_message_post_length: 1, min_personal_message_post_length: 1,
min_personal_message_title_length: 1, min_personal_message_title_length: 1,
allow_duplicate_topic_titles: true, allow_duplicate_topic_titles: true,
disable_emails: true, disable_emails: "non-staff",
max_attachment_size_kb: 102400, max_attachment_size_kb: 102400,
max_image_size_kb: 102400, max_image_size_kb: 102400,
authorized_extensions: '*' authorized_extensions: '*'

View File

@ -70,7 +70,7 @@ class ImportScripts::IpboardSQL < ImportScripts::Base
# Site settings # # Site settings #
################# #################
# don't send any emails # don't send any emails
SiteSetting.disable_emails = true SiteSetting.disable_emails = "non-staff"
# don't send digests (so you can enable email without users noticing) # don't send digests (so you can enable email without users noticing)
SiteSetting.disable_digest_emails = true SiteSetting.disable_digest_emails = true
# keep site and users private # keep site and users private

View File

@ -27,7 +27,7 @@ class ImportScripts::Mbox < ImportScripts::Base
BATCH_SIZE = 1000 BATCH_SIZE = 1000
# Site settings # Site settings
SiteSetting.disable_emails = true SiteSetting.disable_emails = "non-staff"
# Comment out if each file contains a single message # Comment out if each file contains a single message
# Use formail to split yourself: http://linuxcommand.org/man_pages/formail1.html # Use formail to split yourself: http://linuxcommand.org/man_pages/formail1.html

View File

@ -24,7 +24,7 @@ class ImportScripts::Modx < ImportScripts::Base
def initialize def initialize
super super
SiteSetting.disable_emails = true SiteSetting.disable_emails = "non-staff"
@old_username_to_new_usernames = {} @old_username_to_new_usernames = {}

View File

@ -37,7 +37,7 @@ class ImportScripts::MyBB < ImportScripts::Base
end end
def execute def execute
SiteSetting.disable_emails = true SiteSetting.disable_emails = "non-staff"
import_users import_users
import_categories import_categories
import_posts import_posts

View File

@ -33,7 +33,7 @@ class ImportScripts::MylittleforumSQL < ImportScripts::Base
QUIET = true QUIET = true
# Site settings # Site settings
SiteSetting.disable_emails = true SiteSetting.disable_emails = "non-staff"
if FORCE_HOSTNAME if FORCE_HOSTNAME
SiteSetting.force_hostname = FORCE_HOSTNAME SiteSetting.force_hostname = FORCE_HOSTNAME
end end

View File

@ -3,19 +3,42 @@ require 'email/sender'
describe Email::Sender do describe Email::Sender do
context "disable_emails is enabled" do
let(:user) { Fabricate(:user) }
let(:moderator) { Fabricate(:moderator) }
context "disable_emails is enabled for everyone" do
before { SiteSetting.disable_emails = "yes" }
it "doesn't deliver mail when mails are disabled" do it "doesn't deliver mail when mails are disabled" do
SiteSetting.disable_emails = true
Mail::Message.any_instance.expects(:deliver_now).never Mail::Message.any_instance.expects(:deliver_now).never
message = Mail::Message.new(to: "hello@world.com" , body: "hello") message = Mail::Message.new(to: moderator.email , body: "hello")
expect(Email::Sender.new(message, :hello).send).to eq(nil) expect(Email::Sender.new(message, :hello).send).to eq(nil)
end end
it "delivers mail when mails are disabled but the email_type is admin_login" do it "delivers mail when mails are disabled but the email_type is admin_login" do
SiteSetting.disable_emails = true
Mail::Message.any_instance.expects(:deliver_now).once Mail::Message.any_instance.expects(:deliver_now).once
message = Mail::Message.new(to: "hello@world.com" , body: "hello") message = Mail::Message.new(to: moderator.email , body: "hello")
Email::Sender.new(message, :admin_login).send Email::Sender.new(message, :admin_login).send
end end
end
context "disable_emails is enabled for non-staff users" do
before { SiteSetting.disable_emails = "non-staff" }
it "doesn't deliver mail to normal user" do
Mail::Message.any_instance.expects(:deliver_now).never
message = Mail::Message.new(to: user.email, body: "hello")
expect(Email::Sender.new(message, :hello).send).to eq(nil)
end
it "delivers mail to staff user" do
Mail::Message.any_instance.expects(:deliver_now).once
message = Mail::Message.new(to: moderator.email, body: "hello")
Email::Sender.new(message, :hello).send
end
end
end
it "doesn't deliver mail when the message is of type NullMail" do it "doesn't deliver mail when the message is of type NullMail" do
Mail::Message.any_instance.expects(:deliver_now).never Mail::Message.any_instance.expects(:deliver_now).never

View File

@ -169,12 +169,12 @@ describe Admin::BackupsController do
describe ".restore" do describe ".restore" do
it "starts a restore" do it "starts a restore" do
expect(SiteSetting.disable_emails).to eq(false) expect(SiteSetting.disable_emails).to eq("no")
BackupRestore.expects(:restore!).with(@admin.id, filename: backup_filename, publish_to_message_bus: true, client_id: "foo") BackupRestore.expects(:restore!).with(@admin.id, filename: backup_filename, publish_to_message_bus: true, client_id: "foo")
post :restore, params: { id: backup_filename, client_id: "foo" }, format: :json post :restore, params: { id: backup_filename, client_id: "foo" }, format: :json
expect(SiteSetting.disable_emails).to eq(true) expect(SiteSetting.disable_emails).to eq("yes")
expect(response.status).to eq(200) expect(response.status).to eq(200)
end end