Sha256: 638dbc517d5ce4e69f70ade418bd0a4f1b46b6b4eef884060bcddb80e4be5436

Contents?: true

Size: 1.96 KB

Versions: 15

Compression:

Stored size: 1.96 KB

Contents

module Pageflow
  # Query users for their role on accounts based on role
  class AccountRoleQuery < ApplicationQuery
    # Query for accounts where a user has a given role.
    class Scope < Scope
      # Create query scope.
      #
      # @param [User] user
      #   User to query roles for.
      # @param [ActiveRecord::Relation] scope
      #   Scope of all accounts to filter from.
      def initialize(user, scope)
        @user = user
        @scope = scope
      end

      # Find all accounts where user has at least given role.
      #
      # @param [String] role
      #   Name of role.
      # @return [ActiveRecord::Relation]
      def with_role_at_least(role)
        scope.joins(with_membership_for_account(role))
      end

      private

      attr_reader :user, :scope

      def with_membership_for_account(role)
        sanitize_sql(<<-SQL, user_id: user.id, roles: Roles.at_least(role))
          INNER JOIN pageflow_memberships ON
          pageflow_memberships.user_id = :user_id AND
          pageflow_memberships.entity_id = pageflow_accounts.id AND
          pageflow_memberships.entity_type = 'Pageflow::Account' AND
          pageflow_memberships.role IN (:roles)
        SQL
      end
    end

    # Create query that can be used for role comparisons
    #
    # @param [User] user
    #   Required. Membership user to check.
    # @param [Pageflow::Account] account
    #   Required. Membership entity to check.
    def initialize(user, account)
      @user = user
      @account = account
    end

    # Return true if there is a membership with at least role for
    # user/account
    #
    # @param [String] role
    #   Required. Minimum role that we compare against.
    # @return [Boolean]
    def has_at_least_role?(role)
      @user
        .memberships
        .where(role: Roles.at_least(role))
        .where('(entity_id = :account_id AND '\
               "entity_type = 'Pageflow::Account')",
               account_id: @account.id)
        .any?
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

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