Sha256: 9b5e73fc5820b369310948b46460b5f6924d2f5d49119617623fbf0656d7c373

Contents?: true

Size: 1.25 KB

Versions: 15

Compression:

Stored size: 1.25 KB

Contents

module Pageflow
  # @api private
  class ManagedUserQuery < ApplicationQuery
    class Scope < Scope
      def initialize(current_user, scope)
        @current_user = current_user
        @scope = scope
      end

      def resolve
        if current_user.admin?
          scope.all
        else
          scope
            .distinct
            .joins(account_memberships)
            .joins(account_manager_memberships_of_current_user)
        end
      end

      private

      attr_reader :current_user, :scope

      def account_memberships
        <<-SQL
          INNER JOIN pageflow_memberships account_memberships ON
          account_memberships.user_id = users.id AND
          account_memberships.entity_type = 'Pageflow::Account'
        SQL
      end

      def account_manager_memberships_of_current_user
        sanitize_sql(<<-SQL, user_id: current_user.id)
          INNER JOIN pageflow_memberships account_manager_memberships ON
          account_manager_memberships.entity_type = account_memberships.entity_type AND
          account_manager_memberships.entity_id = account_memberships.entity_id AND
          account_manager_memberships.user_id = :user_id AND
          account_manager_memberships.role = "manager"
        SQL
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
pageflow-17.0.4 app/models/pageflow/managed_user_query.rb
pageflow-17.0.3 app/models/pageflow/managed_user_query.rb
pageflow-17.0.2 app/models/pageflow/managed_user_query.rb
pageflow-17.0.1 app/models/pageflow/managed_user_query.rb
pageflow-17.0.0 app/models/pageflow/managed_user_query.rb
pageflow-16.2.0 app/models/pageflow/managed_user_query.rb
pageflow-16.1.0 app/models/pageflow/managed_user_query.rb
pageflow-16.0.0 app/models/pageflow/managed_user_query.rb
pageflow-15.8.0 app/models/pageflow/managed_user_query.rb
pageflow-15.7.1 app/models/pageflow/managed_user_query.rb
pageflow-15.7.0 app/models/pageflow/managed_user_query.rb
pageflow-15.6.1 app/models/pageflow/managed_user_query.rb
pageflow-15.6.0 app/models/pageflow/managed_user_query.rb
pageflow-15.5.0 app/models/pageflow/managed_user_query.rb
pageflow-15.4.0 app/models/pageflow/managed_user_query.rb