Setting 'min username length' greater than 5 breaks granting and revoking Admin/Moderator permissions

We ran into an issue while setting up our Discourse forum. We were trying to grant Moderator permission to an account we created for our Sales team, but kept receiving a 422 error.

I went through our error logs and this was showing up:

ActiveRecord::RecordInvalid (Validation failed: Name must be at least 6 characters)
/var/www/discourse/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.7.1/lib/active_record/validations.rb:79:in `raise_record_invalid'

We had modified the min username length to 6 instead of the default 3 when first going through the setup since that is the length we want to require. Once I reset the field to 3 I was able to add/remove the Moderator permissions. I did some testing and found that I could change min username length anywhere from 1-5 and it did not cause an issue, but as soon as it was set to 6 or greater it broke granting or removing Admin/Moderator permissions.

This didn’t make sense to me since we only had 6 Users and all of their usernames were 6 characters or more. I looked through the rest of our forum to see if there was anything that would be related specifically to Moderators/Admins and had a name that was less than 6 characters. The staff Group is the only thing that I’ve been able to find that meets those criteria. And since it is 5 characters that may be the reason that I can set min username length up to 5 and not cause any issues. This definitely seems like something is not connected correctly within the system.

I found a couple of Topics here with similar issues:

https://meta.discourse.org/t/unable-to-grant-admin-because-username-must-be-at-least-6-characters/25271

The ‘solution’ to both of those issues was to just reset min username length to the default value, which is just a workaround not an actual fix for the issue.

Other than this issue the rest of the setup has been a breeze. Thanks for the great product.

3 Likes

For the users it breaks on, is there username less than 6? If so, that is why. You’d have to convert them to users names greater than 6 before changing that minimum username setting.

5 Likes

Actually, maybe we should just make the minimum be 5 since staff is in the same namespace. It does seem a bit odd that group names are affecting this @sam, recategorizing this back to bug


I went ahead and added this softer warning instead, to both min and max:

min_username_length: "Minimum username length in characters. WARNING: if any existing users or groups have names shorter than this, your site will break!"
max_username_length: "Maximum username length in characters. WARNING: if any existing users or groups have names longer than this, your site will break!"

3 Likes

How hard would it be to build a “Check this” button into the admin interface?

i.e. when you click on “Check this” it goes and scans for entries which would cause a problem, preferably returning a list of what would cause problems.

There is a similar issue with max post length. Even just a query with data explorer would be helpful (I’m still getting to grips with data explorer).

1 Like

Also thanks for pointing this out @mark as this is definitely a weird gotcha. It took me a bit to grok that the group name was causing the problem


1 Like

@cpradio Good idea, that was the first thing I thought too when I saw the error in the logs. I triple checked all of the names to make sure that they were more than 6 characters.

@codinghorror Sorry it wasn’t more obvious in my post that it was the Group name that appeared to be causing the issue. After looking at it again, I could have done a better job of highlighting that point since it was the main reason I wanted to point out the bug. Thanks for updating the help text as well. That should make it clearer that the field may have unwanted side affects if it is changed.

4 Likes

Long term I would prefer if we had a “validation” routine that runs every time you try to increase length for this key:

if min_user_name_length.increased? 
   if User.where('length(username) < :size', size: min_user_name_length).exists? 
     raise "sorry, but this is not gonna happen"
   end
    if Group.where('length(name) < :size', size: min_user_name_length).exists? 
     raise "sorry, but this is also not gonna happen"
   end
end
4 Likes

This is still an issue. Since ‘staff’ is an existing group name, setting the min username length to anything other than 3, 4 or 5 will break granting and revoking admin if you do not rename the staff group first.

Worse, the error message is just “The change you wanted was rejected. Maybe you tried to change something you didn’t have access to.” and the logs show “Name must be at least 6 characters”. Took me while to find out that this was about the group name (no actually, I never figured it out, I found this topic :wink: ).

2 Likes

@nbianca can you add a custom validation for that site setting similar to what @sam proposed?

5 Likes

This has been fixed a little while ago

https://github.com/discourse/discourse/commit/e0d7cdac12a180697b3939967c8315184899bfa4

3 Likes