Sha256: 6912a759dfa4c3d72a4791666bb3b1068efc56e72a27ac3af6fda18e6cffc067

Contents?: true

Size: 1.8 KB

Versions: 6

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

module Decidim
  # This command destroys the user's account.
  class DestroyAccount < Decidim::Command
    delegate :current_user, to: :form

    # Destroy a user's account.
    #
    # form - The form with the data.
    def initialize(form)
      @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
        destroy_participatory_space_private_user
        delegate_destroy_to_participatory_spaces
      end

      broadcast(:ok)
    end

    private

    attr_reader :form

    def destroy_user_account!
      current_user.invalidate_all_sessions!

      current_user.name = ""
      current_user.nickname = ""
      current_user.email = ""
      current_user.delete_reason = @form.delete_reason
      current_user.admin = false if current_user.admin?
      current_user.deleted_at = Time.current
      current_user.skip_reconfirmation!
      current_user.avatar.purge
      current_user.save!
    end

    def destroy_user_identities
      current_user.identities.destroy_all
    end

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

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

    def destroy_participatory_space_private_user
      Decidim::ParticipatorySpacePrivateUser.where(user: current_user).destroy_all
    end

    def delegate_destroy_to_participatory_spaces
      Decidim.participatory_space_manifests.each do |space_manifest|
        space_manifest.invoke_on_destroy_account(current_user)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-core-0.29.1 app/commands/decidim/destroy_account.rb
decidim-core-0.29.0 app/commands/decidim/destroy_account.rb
decidim-core-0.29.0.rc4 app/commands/decidim/destroy_account.rb
decidim-core-0.29.0.rc3 app/commands/decidim/destroy_account.rb
decidim-core-0.29.0.rc2 app/commands/decidim/destroy_account.rb
decidim-core-0.29.0.rc1 app/commands/decidim/destroy_account.rb