FIX: Users should be able to remove their primary group

This fix allows a user to remove their currently assigned primary group
if the Site Setting `user selected primary groups` is enabled.

Before this fix, if a user selected "none" for their primary group it
would silently fail and never be updated.
This commit is contained in:
Blake Erickson 2020-01-17 15:43:54 -07:00
parent 3fc589f875
commit 75069ff179
2 changed files with 24 additions and 0 deletions

View File

@ -89,6 +89,10 @@ class UserUpdater
guardian.can_use_primary_group?(user, attributes[:primary_group_id])
user.primary_group_id = attributes[:primary_group_id]
elsif SiteSetting.user_selected_primary_groups &&
!attributes[:primary_group_id].present?
user.primary_group_id = nil
end
CATEGORY_IDS.each do |attribute, level|

View File

@ -272,6 +272,26 @@ describe UserUpdater do
user.reload
expect(user.primary_group_id).to eq nil
end
it 'can be removed by the user when setting is enabled' do
SiteSetting.user_selected_primary_groups = true
user.groups << new_group
user.update(primary_group_id: new_group.id)
UserUpdater.new(acting_user, user).update(primary_group_id: '')
user.reload
expect(user.primary_group_id).to eq nil
end
it 'cannot be removed by the user when setting is disabled' do
SiteSetting.user_selected_primary_groups = false
user.groups << new_group
user.update(primary_group_id: new_group.id)
UserUpdater.new(acting_user, user).update(primary_group_id: '')
user.reload
expect(user.primary_group_id).to eq new_group.id
end
end
context 'when update fails' do