FIX: validate automatic membership email domains

This commit is contained in:
Arpit Jalan 2015-11-27 11:05:16 +05:30
parent f74a6457ee
commit 6354324f2f
3 changed files with 24 additions and 0 deletions

View File

@ -21,6 +21,7 @@ class Group < ActiveRecord::Base
validate :name_format_validator
validates_uniqueness_of :name, case_sensitive: false
validate :automatic_membership_email_domains_format_validator
AUTO_GROUPS = {
:everyone => 0,
@ -290,6 +291,18 @@ class Group < ActiveRecord::Base
UsernameValidator.perform_validation(self, 'name')
end
def automatic_membership_email_domains_format_validator
return if self.automatic_membership_email_domains.blank?
domains = self.automatic_membership_email_domains.split("|")
domains.each do |domain|
domain.sub!(/^https?:\/\//, '')
domain.sub!(/\/.*$/, '')
self.errors.add :base, (I18n.t('groups.errors.invalid_domain', domain: domain)) unless domain =~ /\A[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?\Z/i
end
self.automatic_membership_email_domains = domains.join("|")
end
# hack around AR
def destroy_deletions
if @deletions

View File

@ -196,6 +196,7 @@ en:
errors:
can_not_modify_automatic: "You can not modify an automatic group"
member_already_exist: "'%{username}' is already a member of this group."
invalid_domain: "'%{domain}' is not a valid domain."
default_names:
everyone: "everyone"
admins: "admins"

View File

@ -33,6 +33,16 @@ describe Group do
group.name = 'This_Is_A_Name'
expect(group.valid?).to eq false
end
it "is invalid for poorly formatted domains" do
group.automatic_membership_email_domains = "wikipedia.org|*@example.com"
expect(group.valid?).to eq false
end
it "is valid for proper domains" do
group.automatic_membership_email_domains = "discourse.org|wikipedia.org"
expect(group.valid?).to eq true
end
end
def real_admins