Skip to content

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

FIX: refreshing auto groups when min_username_length is long

Sam243fc9d0f9b9
diff --git a/app/models/group.rb b/app/models/group.rb
index 60cb1c0282a..1e38e15ce99 100644
--- a/app/models/group.rb
+++ b/app/models/group.rb
@@ -599,7 +599,15 @@ class Group < ActiveRecord::Base
protected
def name_format_validator
- self.name.strip!
+
+ return if !name_changed?
+
+ # avoid strip! here, it works now
+ # but may not continue to work long term, especially
+ # once we start returning frozen strings
+ if self.name != (stripped = self.name.strip)
+ self.name = stripped
+ end
UsernameValidator.perform_validation(self, 'name') || begin
name_lower = self.name.downcase
diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rb
index 30a3ba3f42e..73befad9548 100644
--- a/spec/models/group_spec.rb
+++ b/spec/models/group_spec.rb
@@ -315,7 +315,7 @@ describe Group do
default_locale = SiteSetting.default_locale
I18n.locale = SiteSetting.default_locale = 'de'
- another_group = Fabricate(:group,
+ _another_group = Fabricate(:group,
name: I18n.t('groups.default_names.staff').upcase
)
@@ -380,6 +380,17 @@ describe Group do
admin.revoke_moderation!
expect(real_admins).to be_empty
expect(real_staff).to eq []
+
+ # we need some ninja work to set min username to 6
+
+ User.where('length(username) < 6').each do |u|
+ u.username = u.username + "ZZZZZZ"
+ u.save!
+ end
+
+ SiteSetting.min_username_length = 6
+ Group.refresh_automatic_groups!(:staff)
+ # should not explode here
end
it "Correctly updates automatic trust level groups" do