Project

General

Profile

Defect #11870 » fix-11870-v3.patch

vzvu 3k6k, 2020-07-14 17:57

View differences:

app/controllers/users_controller.rb
184 184
  end
185 185

  
186 186
  def destroy
187
    if @user == User.current && !@user.own_account_deletable?
188
      error_messages = @user.own_account_deletion_errors.full_messsages
189
      respond_to do |format|
190
        format.html { return render_error message: error_messages.join(', '), status: 422 }
191
        format.api  { return render_api_errors(error_messages) }
192
      end
193
    end
194

  
187 195
    @user.destroy
188 196
    respond_to do |format|
189 197
      format.html { redirect_back_or_default(users_path) }
app/models/user.rb
754 754

  
755 755
  # Returns true if the user is allowed to delete the user's own account
756 756
  def own_account_deletable?
757
    Setting.unsubscribe? &&
758
      (!admin? || User.active.admin.where("id <> ?", id).exists?)
757
    own_account_deletion_errors.present?
758
  end
759

  
760
  def own_account_deletion_errors
761
    errors = ActiveModel::Errors.new(self)
762

  
763
    unless Setting.unsubscribe?
764
      errors.add(:base, :error_unable_delete_own_account)
765
      return errors
766
    end
767

  
768
    if admin? && !User.active.admin.where("id <> ?", id).exists?
769
      errors.add(:base, :error_unable_delete_last_active_admin)
770
    end
771

  
772
    errors
759 773
  end
760 774

  
761 775
  safe_attributes(
(3-3/3)