Sha256: fb1a7fdf7a4936d72752c7f4200eeb2dacc0231894e59ca2ca2b5b93e8dfeb10

Contents?: true

Size: 1.92 KB

Versions: 12

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

module Decidim
  module Admin
    # A command with all the business logic to impersonate a managed user.
    class ImpersonateUser < Decidim::Command
      # Public: Initializes the command.
      #
      # form         - The form with the authorization info
      # user         - The user to impersonate
      def initialize(form)
        @form = form
      end

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

        transaction do
          user.save! unless user.persisted?

          create_authorization

          impersonation_log = create_impersonation_log
          create_action_log(impersonation_log)
        end

        enqueue_expire_job

        broadcast(:ok)
      end

      private

      attr_reader :form

      def user
        form.user
      end

      def create_authorization
        Authorization.create_or_update_from(form.authorization)
      end

      def create_impersonation_log
        Decidim::ImpersonationLog.create!(
          admin: form.current_user,
          user: user,
          reason: form.reason,
          started_at: Time.current
        )
      end

      def enqueue_expire_job
        Decidim::Admin::ExpireImpersonationJob
          .set(wait: Decidim::ImpersonationLog::SESSION_TIME_IN_MINUTES.minutes)
          .perform_later(user, form.current_user)
      end

      def create_action_log(impersonation_log)
        Decidim.traceability.perform_action!(
          "manage",
          impersonation_log,
          form.current_user,
          resource: {
            name: user.name,
            id: user.id,
            nickname: user.nickname
          },
          visibility: "admin-only",
          reason: form.reason
        )
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
decidim-admin-0.27.9 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.27.8 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.27.7 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.27.6 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.27.5 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.27.4 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.27.3 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.27.2 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.27.1 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.27.0 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.27.0.rc2 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.27.0.rc1 app/commands/decidim/admin/impersonate_user.rb