diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 36dab7149f3..4d0d9cc433b 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -135,18 +135,29 @@ class Admin::UsersController < Admin::AdminController def remove_group group = Group.find(params[:group_id].to_i) return render_json_error group unless group && !group.automatic + group.remove(@user) GroupActionLogger.new(current_user, group).log_remove_user_from_group(@user) + render nothing: true end def primary_group - group = Group.find(params[:primary_group_id].to_i) guardian.ensure_can_change_primary_group!(@user) - if group.users.include?(@user) - @user.primary_group_id = params[:primary_group_id] - @user.save! + + if params[:primary_group_id].present? + primary_group_id = params[:primary_group_id].to_i + if group = Group.find(primary_group_id) + if group.user_ids.include?(@user.id) + @user.primary_group_id = primary_group_id + end + end + else + @user.primary_group_id = nil end + + @user.save! + render nothing: true end diff --git a/spec/controllers/admin/users_controller_spec.rb b/spec/controllers/admin/users_controller_spec.rb index f5dae281c96..0a199043988 100644 --- a/spec/controllers/admin/users_controller_spec.rb +++ b/spec/controllers/admin/users_controller_spec.rb @@ -228,6 +228,13 @@ describe Admin::UsersController do @another_user.reload expect(@another_user.primary_group_id).to be_nil end + + it "remove user's primary group" do + group.add(@another_user) + xhr :put, :primary_group, user_id: @another_user.id, primary_group_id: "" + @another_user.reload + expect(@another_user.primary_group_id).to be(nil) + end end context '.trust_level' do