Sha256: ccfd0d646c8c3a235d5a620a0441312b2fc49e7b4fe7b89717e04627df4a9298

Contents?: true

Size: 1.48 KB

Versions: 38

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module Decidim
  module Admin
    # A command with all the business logic to impersonate a managed user.
    class ImpersonateUser < Rectify::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

          create_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
    end
  end
end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
decidim-admin-0.23.6 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.23.5 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.23.4 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.23.3 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.23.2 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.23.1 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.23.1.rc1 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.23.0 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.22.0 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.21.0 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.20.1 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.20.0 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.19.1 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.18.1 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.19.0 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.17.2 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.18.0 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.17.1 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.16.1 app/commands/decidim/admin/impersonate_user.rb
decidim-admin-0.17.0 app/commands/decidim/admin/impersonate_user.rb