Sha256: eb5e7d623ce370a5ce292429bd36912b73c0e556f252da1a0619a212743c5682

Contents?: true

Size: 1.73 KB

Versions: 6

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

require 'proxes/db'
require 'proxes/models/permission'
require 'proxes/services/logger'
require 'proxes/helpers/indices'

module ProxES
  class RequestPolicy
    include Helpers::Indices

    attr_reader :user, :record

    def initialize(user, record)
      @user = user
      @record = record
    end

    def method_missing(method_sym, *arguments, &block)
      return super if method_sym.to_s[-1] != '?'

      return false if user.nil?
      return true if record.indices? && index_allowed?
      action_allowed? method_sym[0..-2].upcase
    end

    def index_allowed?
      patterns = Permission.for_user(user, 'INDEX').map do |permission|
        permission.pattern.gsub(/\{user.(.*)\}/) { |_match| user.send(Regexp.last_match[1].to_sym) }
      end
      filter(record.index, patterns).count > 0
    end

    def action_allowed?(action)
      # Give me all the user's permissions that match the verb
      Permission.for_user(user, action).each do |permission|
        return true if record.path =~ /#{permission.pattern}/
      end
      false
    end

    def respond_to_missing?(name, _include_private = false)
      name[-1] == '?'
    end

    def logger
      @logger ||= ProxES::Services::Logger.instance
    end

    class Scope
      include Helpers::Indices

      attr_reader :user, :scope

      def initialize(user, scope)
        @user = user
        @scope = scope
      end

      def logger
        @logger ||= ProxES::Services::Logger.instance
      end
    end
  end
end

require 'proxes/policies/request/cat_policy'
require 'proxes/policies/request/root_policy'
require 'proxes/policies/request/stats_policy'
require 'proxes/policies/request/search_policy'
require 'proxes/policies/request/snapshot_policy'

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
proxes-0.6.1 lib/proxes/policies/request_policy.rb
proxes-0.6.0 lib/proxes/policies/request_policy.rb
proxes-0.5.2 lib/proxes/policies/request_policy.rb
proxes-0.5.1 lib/proxes/policies/request_policy.rb
proxes-0.5.0 lib/proxes/policies/request_policy.rb
proxes-0.4.4 lib/proxes/policies/request_policy.rb