Sha256: 68ac0b4a0e37f7491127640f51c2df374c8aa534274f1da81b2b26101cd62e6a

Contents?: true

Size: 1.1 KB

Versions: 13

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

module Decidim
  module Admin
    # A command with all the business logic to close a current impersonation session.
    class CloseSessionManagedUser < Decidim::Command
      # Public: Initializes the command.
      #
      # user         - The user impersonated.
      # current_user - The current user doing the impersonation.
      def initialize(user, current_user)
        @user = user
        @current_user = current_user
      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) if impersonation_log.blank?

        close_session

        broadcast(:ok)
      end

      attr_reader :current_user, :user

      private

      def impersonation_log
        @impersonation_log ||= Decidim::ImpersonationLog.where(admin: current_user, user:).active.first
      end

      def close_session
        impersonation_log.ended_at = Time.current
        impersonation_log.save!
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
decidim-admin-0.29.1 app/commands/decidim/admin/close_session_managed_user.rb
decidim-admin-0.28.4 app/commands/decidim/admin/close_session_managed_user.rb
decidim-admin-0.29.0 app/commands/decidim/admin/close_session_managed_user.rb
decidim-admin-0.28.3 app/commands/decidim/admin/close_session_managed_user.rb
decidim-admin-0.29.0.rc4 app/commands/decidim/admin/close_session_managed_user.rb
decidim-admin-0.29.0.rc3 app/commands/decidim/admin/close_session_managed_user.rb
decidim-admin-0.29.0.rc2 app/commands/decidim/admin/close_session_managed_user.rb
decidim-admin-0.29.0.rc1 app/commands/decidim/admin/close_session_managed_user.rb
decidim-admin-0.28.2 app/commands/decidim/admin/close_session_managed_user.rb
decidim-admin-0.28.1 app/commands/decidim/admin/close_session_managed_user.rb
decidim-admin-0.28.0 app/commands/decidim/admin/close_session_managed_user.rb
decidim-admin-0.28.0.rc5 app/commands/decidim/admin/close_session_managed_user.rb
decidim-admin-0.28.0.rc4 app/commands/decidim/admin/close_session_managed_user.rb