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

**URL:** https://meta.discourse.org/t/setting-min-username-length-greater-than-5-breaks-granting-and-revoking-admin-moderator-permissions/57079
**Category:** Bug
**Created:** [7 فبراير 2017، 4:50م UTC](https://meta.discourse.org/t/setting-min-username-length-greater-than-5-breaks-granting-and-revoking-admin-moderator-permissions/57079 "2017-02-07T16:50:10Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![MarkT](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/markt/32/64156_2.png) [@MarkT](https://meta.discourse.org/u/MarkT)
#### Post date: [7 فبراير 2017، 4:50م UTC](https://meta.discourse.org/t/setting-min-username-length-greater-than-5-breaks-granting-and-revoking-admin-moderator-permissions/57079/1 "2017-02-07T16:50:10Z")

</div>

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](https://meta.discourse.org/t/unable-to-grant-admin-because-username-must-be-at-least-6-characters/25271)

> [@Can't revoke moderator status](https://meta.discourse.org/t/cant-revoke-moderator-status/51799):
>
> I need to change up my admins/moderators. But I am getting an error when I hit “revoke moderator” Any ideas? Thanks Alex

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.

---

<div class="post-metadata">

### Author: ![cpradio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cpradio/32/4970_2.png) [@cpradio](https://meta.discourse.org/u/cpradio)
#### Post date: [7 فبراير 2017، 5:04م UTC](https://meta.discourse.org/t/setting-min-username-length-greater-than-5-breaks-granting-and-revoking-admin-moderator-permissions/57079/2 "2017-02-07T17:04:24Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [7 فبراير 2017، 6:05م UTC](https://meta.discourse.org/t/setting-min-username-length-greater-than-5-breaks-granting-and-revoking-admin-moderator-permissions/57079/5 "2017-02-07T18:05:29Z")

</div>

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!"`

---

<div class="post-metadata">

### Author: ![JagWaugh](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jagwaugh/32/69335_2.png) [@JagWaugh](https://meta.discourse.org/u/JagWaugh)
#### Post date: [7 فبراير 2017، 6:25م UTC](https://meta.discourse.org/t/setting-min-username-length-greater-than-5-breaks-granting-and-revoking-admin-moderator-permissions/57079/6 "2017-02-07T18:25:58Z")

</div>

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](https://meta.discourse.org/t/32566?silent=true) would be helpful (I’m still getting to grips with [data explorer](https://meta.discourse.org/t/32566?silent=true)).

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [7 فبراير 2017، 6:45م UTC](https://meta.discourse.org/t/setting-min-username-length-greater-than-5-breaks-granting-and-revoking-admin-moderator-permissions/57079/7 "2017-02-07T18:45:04Z")

</div>

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..

---

<div class="post-metadata">

### Author: ![MarkT](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/markt/32/64156_2.png) [@MarkT](https://meta.discourse.org/u/MarkT)
#### Post date: [7 فبراير 2017، 7:07م UTC](https://meta.discourse.org/t/setting-min-username-length-greater-than-5-breaks-granting-and-revoking-admin-moderator-permissions/57079/8 "2017-02-07T19:07:19Z")

</div>

@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.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [7 فبراير 2017، 7:13م UTC](https://meta.discourse.org/t/setting-min-username-length-greater-than-5-breaks-granting-and-revoking-admin-moderator-permissions/57079/9 "2017-02-07T19:13:06Z")

</div>

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

```plaintext
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

```

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [17 أغسطس 2018، 10:17ص UTC](https://meta.discourse.org/t/setting-min-username-length-greater-than-5-breaks-granting-and-revoking-admin-moderator-permissions/57079/10 "2018-08-17T10:17:47Z")

</div>

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 😉 ).

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [17 أغسطس 2018، 10:25ص UTC](https://meta.discourse.org/t/setting-min-username-length-greater-than-5-breaks-granting-and-revoking-admin-moderator-permissions/57079/11 "2018-08-17T10:25:59Z")

</div>

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

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [8 أكتوبر 2018، 5:20م UTC](https://meta.discourse.org/t/setting-min-username-length-greater-than-5-breaks-granting-and-revoking-admin-moderator-permissions/57079/13 "2018-10-08T17:20:57Z")

</div>

This has been fixed a little while ago

[https://github.com/discourse/discourse/commit/e0d7cdac12a180697b3939967c8315184899bfa4](https://github.com/discourse/discourse/commit/e0d7cdac12a180697b3939967c8315184899bfa4)

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [8 أكتوبر 2018، 5:20م UTC](https://meta.discourse.org/t/setting-min-username-length-greater-than-5-breaks-granting-and-revoking-admin-moderator-permissions/57079/14 "2018-10-08T17:20:59Z")

</div>


