mirror of
https://github.com/discourse/discourse.git
synced 2025-09-06 10:50:21 +08:00
FIX: validate automatic membership email domains
This commit is contained in:
parent
f74a6457ee
commit
6354324f2f
3 changed files with 24 additions and 0 deletions
|
@ -21,6 +21,7 @@ class Group < ActiveRecord::Base
|
||||||
|
|
||||||
validate :name_format_validator
|
validate :name_format_validator
|
||||||
validates_uniqueness_of :name, case_sensitive: false
|
validates_uniqueness_of :name, case_sensitive: false
|
||||||
|
validate :automatic_membership_email_domains_format_validator
|
||||||
|
|
||||||
AUTO_GROUPS = {
|
AUTO_GROUPS = {
|
||||||
:everyone => 0,
|
:everyone => 0,
|
||||||
|
@ -290,6 +291,18 @@ class Group < ActiveRecord::Base
|
||||||
UsernameValidator.perform_validation(self, 'name')
|
UsernameValidator.perform_validation(self, 'name')
|
||||||
end
|
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
|
# hack around AR
|
||||||
def destroy_deletions
|
def destroy_deletions
|
||||||
if @deletions
|
if @deletions
|
||||||
|
|
|
@ -196,6 +196,7 @@ en:
|
||||||
errors:
|
errors:
|
||||||
can_not_modify_automatic: "You can not modify an automatic group"
|
can_not_modify_automatic: "You can not modify an automatic group"
|
||||||
member_already_exist: "'%{username}' is already a member of this group."
|
member_already_exist: "'%{username}' is already a member of this group."
|
||||||
|
invalid_domain: "'%{domain}' is not a valid domain."
|
||||||
default_names:
|
default_names:
|
||||||
everyone: "everyone"
|
everyone: "everyone"
|
||||||
admins: "admins"
|
admins: "admins"
|
||||||
|
|
|
@ -33,6 +33,16 @@ describe Group do
|
||||||
group.name = 'This_Is_A_Name'
|
group.name = 'This_Is_A_Name'
|
||||||
expect(group.valid?).to eq false
|
expect(group.valid?).to eq false
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
def real_admins
|
def real_admins
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue