Sha256: af70ad4806a42a2ec0e01c2c8b0763202464f1969e8c139661cc79b17829fa0d

Contents?: true

Size: 1.93 KB

Versions: 16

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

module Decidim
  module Admin
    class BlockUser < Rectify::Command
      # Public: Initializes the command.
      #
      # form - BlockUserForm
      def initialize(form)
        @form = form
      end

      # Executes the command. Broadcasts these events:
      #
      # - :ok when everything is valid, together with the resource.
      # - :invalid if the resource is not reported
      #
      # Returns nothing.
      def call
        return broadcast(:invalid) unless form.valid?

        transaction do
          block!
          register_justification!
          notify_user!
        end

        broadcast(:ok, form.user)
      end

      private

      attr_reader :form

      def register_justification!
        @current_blocking = UserBlock.create!(
          justification: form.justification,
          user: form.user,
          blocking_user: form.current_user
        )
      end

      def notify_user!
        Decidim::BlockUserJob.perform_later(
          @current_blocking.user,
          @current_blocking.justification
        )
      end

      def block!
        Decidim.traceability.perform_action!(
          "block",
          form.user,
          form.current_user,
          extra: {
            reportable_type: form.user.class.name,
            current_justification: form.justification
          },
          resource: {
            # Make sure the action log entry gets the original user name instead
            # of "Blocked user". Otherwise the log entries would show funny
            # messages such as "Mr. Admin blocked user Blocked user"-
            title: form.user.name
          }
        ) do
          form.user.blocked = true
          form.user.blocked_at = Time.current
          form.user.blocking = @current_blocking
          form.user.extended_data["user_name"] = form.user.name
          form.user.name = "Blocked user"
          form.user.save!
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
decidim-admin-0.26.7 app/commands/decidim/admin/block_user.rb
decidim-admin-0.26.5 app/commands/decidim/admin/block_user.rb
decidim-admin-0.26.4 app/commands/decidim/admin/block_user.rb
decidim-admin-0.26.3 app/commands/decidim/admin/block_user.rb
decidim-admin-0.26.2 app/commands/decidim/admin/block_user.rb
decidim-admin-0.26.1 app/commands/decidim/admin/block_user.rb
decidim-admin-0.26.0 app/commands/decidim/admin/block_user.rb
decidim-admin-0.26.0.rc2 app/commands/decidim/admin/block_user.rb
decidim-admin-0.26.0.rc1 app/commands/decidim/admin/block_user.rb
decidim-admin-0.25.2 app/commands/decidim/admin/block_user.rb
decidim-admin-0.25.1 app/commands/decidim/admin/block_user.rb
decidim-admin-0.25.0 app/commands/decidim/admin/block_user.rb
decidim-admin-0.25.0.rc4 app/commands/decidim/admin/block_user.rb
decidim-admin-0.25.0.rc3 app/commands/decidim/admin/block_user.rb
decidim-admin-0.25.0.rc2 app/commands/decidim/admin/block_user.rb
decidim-admin-0.25.0.rc1 app/commands/decidim/admin/block_user.rb