Sha256: 5225a624177ba0b2c5059ace764f95a5f5dacc000d9c06d6853273c7b9229f26

Contents?: true

Size: 1.19 KB

Versions: 15

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

module Decidim
  # This command destroys the user's account.
  class DestroyAccount < Rectify::Command
    # Destroy a user's account.
    #
    # user - The user to be updated.
    # form - The form with the data.
    def initialize(user, form)
      @user = user
      @form = form
    end

    def call
      return broadcast(:invalid) unless @form.valid?

      Decidim::User.transaction do
        destroy_user_account!
        destroy_user_identities
        destroy_user_group_memberships
        destroy_follows
      end

      broadcast(:ok)
    end

    private

    def destroy_user_account!
      @user.name = ""
      @user.nickname = ""
      @user.email = ""
      @user.delete_reason = @form.delete_reason
      @user.deleted_at = Time.current
      @user.skip_reconfirmation!
      @user.remove_avatar!
      @user.save!
    end

    def destroy_user_identities
      @user.identities.destroy_all
    end

    def destroy_user_group_memberships
      Decidim::UserGroupMembership.where(user: @user).destroy_all
    end

    def destroy_follows
      Decidim::Follow.where(followable: @user).destroy_all
      Decidim::Follow.where(user: @user).destroy_all
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
decidim-core-0.21.0 app/commands/decidim/destroy_account.rb
decidim-core-0.20.1 app/commands/decidim/destroy_account.rb
decidim-core-0.20.0 app/commands/decidim/destroy_account.rb
decidim-core-0.19.1 app/commands/decidim/destroy_account.rb
decidim-core-0.18.1 app/commands/decidim/destroy_account.rb
decidim-core-0.19.0 app/commands/decidim/destroy_account.rb
decidim-core-0.17.2 app/commands/decidim/destroy_account.rb
decidim-core-0.18.0 app/commands/decidim/destroy_account.rb
decidim-core-0.17.1 app/commands/decidim/destroy_account.rb
decidim-core-0.16.1 app/commands/decidim/destroy_account.rb
decidim-core-0.17.0 app/commands/decidim/destroy_account.rb
decidim-core-0.16.0 app/commands/decidim/destroy_account.rb
decidim-core-0.15.2 app/commands/decidim/destroy_account.rb
decidim-core-0.15.1 app/commands/decidim/destroy_account.rb
decidim-core-0.15.0 app/commands/decidim/destroy_account.rb