Sha256: b61e8c23a87208272de1cf3c860298d2945bfbdfa62241004a4124b4995b0ee8

Contents?: true

Size: 1.46 KB

Versions: 35

Compression:

Stored size: 1.46 KB

Contents

module Ddr::Auth
  module Roles
    #
    # RoleSet query interface
    #
    # @api private
    #
    class RoleSetQuery
      include Enumerable

      attr_reader :criteria, :role_set

      delegate :each, :agents, :permissions, :empty?, to: :result

      def initialize(role_set)
        @role_set = role_set
        @criteria = {}
      end

      def where(conditions={})
        criteria.merge!(conditions)
        self
      end

      def scope(s)
        where(scope: s)
      end

      def in_policy_scope
        scope(Roles::POLICY_SCOPE)
      end

      def in_resource_scope
        scope(Roles::RESOURCE_SCOPE)
      end

      def agent(a)
        where(agent: a)
      end
      alias_method :group, :agent

      def role_type(r)
        where(role_type: r)
      end
      alias_method :type, :role_type

      def merge(other_query)
        where(other_query.criteria)
      end

      def result
        matching_roles = role_set.select { |role| matches_all?(role) }
        RoleSet.new(roles: matching_roles)
      end

      private

      # Return a list of the permissions granted to any of the agents in the given scope
      def permissions_for_agents_in_scope(agents, scope)
        agent(agents).scope(scope).permissions
      end

      def matches_all?(role)
        criteria.all? { |key, value| matches_one?(role, key, value) }
      end

      def matches_one?(role, key, value)
        Array(value).include? role.send(key)
      end

    end
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
ddr-models-3.0.0.beta.16 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.0.beta.15 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.0.beta.14 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.0.beta.13 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.0.beta.12 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.0.beta.11 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.0.beta.10 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.0.beta.9 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.0.beta.8 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.0.beta.7 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.0.beta.6 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.0.beta.4 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.0.beta.3 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.0.beta.2 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.0.beta.1 lib/ddr/auth/roles/role_set_query.rb