Sha256: b59d221bc24ca5afd99d99f40dc86e0ae07a91440206cb24edc0f3257602cb52

Contents?: true

Size: 1.05 KB

Versions: 44

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

module Decidim
  # ImpersonationLogs are created whenever an admin impersonate a managed user
  class ImpersonationLog < ApplicationRecord
    SESSION_TIME_IN_MINUTES = 30

    belongs_to :admin, foreign_key: "decidim_admin_id", class_name: "Decidim::User"
    belongs_to :user, foreign_key: "decidim_user_id", class_name: "Decidim::User"

    validate :same_organization

    scope :active, -> { where(ended_at: nil, expired_at: nil) }
    scope :expired, -> { where(ended_at: nil).where.not(expired_at: nil) }

    def ended?
      ended_at.present?
    end

    def expired?
      expired_at.present?
    end

    def self.log_presenter_class_for(_log)
      Decidim::AdminLog::ImpersonationLogPresenter
    end

    def ensure_not_expired!
      expire! if started_at + SESSION_TIME_IN_MINUTES.minutes < Time.current
    end

    def expire!
      update!(expired_at: Time.current)
    end

    private

    def same_organization
      return if admin&.organization == user&.organization

      errors.add(:admin, :invalid)
    end
  end
end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
decidim-core-0.29.1 app/models/decidim/impersonation_log.rb
decidim-core-0.28.4 app/models/decidim/impersonation_log.rb
decidim-core-0.27.9 app/models/decidim/impersonation_log.rb
decidim-core-0.29.0 app/models/decidim/impersonation_log.rb
decidim-core-0.28.3 app/models/decidim/impersonation_log.rb
decidim-core-0.27.8 app/models/decidim/impersonation_log.rb
decidim-core-0.29.0.rc4 app/models/decidim/impersonation_log.rb
decidim-core-0.29.0.rc3 app/models/decidim/impersonation_log.rb
decidim-core-0.29.0.rc2 app/models/decidim/impersonation_log.rb
decidim-core-0.29.0.rc1 app/models/decidim/impersonation_log.rb
decidim-core-0.28.2 app/models/decidim/impersonation_log.rb
decidim-core-0.27.7 app/models/decidim/impersonation_log.rb
decidim-core-0.28.1 app/models/decidim/impersonation_log.rb
decidim-core-0.27.6 app/models/decidim/impersonation_log.rb
decidim-core-0.26.10 app/models/decidim/impersonation_log.rb
decidim-core-0.26.9 app/models/decidim/impersonation_log.rb
decidim-core-0.28.0 app/models/decidim/impersonation_log.rb
decidim-core-0.27.5 app/models/decidim/impersonation_log.rb
decidim-core-0.28.0.rc5 app/models/decidim/impersonation_log.rb
decidim-core-0.28.0.rc4 app/models/decidim/impersonation_log.rb