Commit 969de4c1 authored by Douwe Maan's avatar Douwe Maan

Fix EmailsOnPush to allow sending from @company.com for GitLab at gitlab.corp.company.com.

parent 5d863321
......@@ -34,6 +34,20 @@ class Notify < ActionMailer::Base
)
end
# Splits "gitlab.corp.company.com" up into "gitlab.corp.company.com",
# "corp.company.com" and "company.com".
# Respects set tld length so "company.co.uk" won't match "somethingelse.uk"
def self.allowed_email_domains
domain_parts = Gitlab.config.gitlab.host.split(".")
allowed_domains = []
begin
allowed_domains << domain_parts.join(".")
domain_parts.shift
end while domain_parts.length > ActionDispatch::Http::URL.tld_length
allowed_domains
end
private
# The default email address to send emails from
......@@ -50,7 +64,8 @@ class Notify < ActionMailer::Base
address = default_sender_address
address.display_name = sender.name
if send_from_user_email && sender.email.end_with?("@#{Gitlab.config.gitlab.host}")
sender_domain = sender.email.split("@").last
if send_from_user_email && self.class.allowed_email_domains.include?(sender_domain)
address.address = sender.email
end
......
......@@ -607,11 +607,14 @@ describe Notify do
let(:send_from_committer_email) { true }
context "when the committer email domain matches" do
before do
allow(Gitlab.config.gitlab).to receive(:host).and_return("gitlab.corp.company.com")
end
context "when the committer email domain is within the GitLab domain" do
before do
allow(Gitlab.config.gitlab).to receive(:host).and_return("gitlab.dev")
user.update_attribute(:email, "user@#{Gitlab.config.gitlab.host}")
user.update_attribute(:email, "user@company.com")
user.confirm!
end
......@@ -621,7 +624,25 @@ describe Notify do
end
end
context "when the committer email doesn't match" do
context "when the committer email domain is not completely within the GitLab domain" do
before do
user.update_attribute(:email, "user@something.company.com")
user.confirm!
end
it "is sent from the default email" do
sender = subject.header[:from].addrs[0]
expect(sender.address).to eq(gitlab_sender)
end
end
context "when the committer email domain is outside the GitLab domain" do
before do
user.update_attribute(:email, "user@mpany.com")
user.confirm!
end
it "is sent from the default email" do
sender = subject.header[:from].addrs[0]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment