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.8 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.7 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.6 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.5 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.4 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.3 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.2 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.1 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.0 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.0.rc6 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.0.rc5 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.0.rc4 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.0.rc3 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.0.rc2 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.0.rc1 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.0.beta.22 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.0.beta.21 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.0.beta.20 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.0.beta.18 lib/ddr/auth/roles/role_set_query.rb
ddr-models-3.0.0.beta.17 lib/ddr/auth/roles/role_set_query.rb