Sha256: 5394297cd996115d5a8736587e785cd23a33103302bfac6b7d087b532112e40f

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

require 'active_support'
require 'active_support/core_ext/object/blank'
require 'ditty/services/logger'
require 'proxes/models/permission'
require 'proxes/helpers/indices'

module ProxES
  class RequestPolicy
    include Helpers::Indices

    attr_reader :user, :record
    alias request 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 request.indices? && !index_allowed?
      action_allowed? method_sym[0..-2].upcase
    end

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

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

    def action_allowed?(action)
      # Give me all the user's permissions that match the verb
      patterns_for(action).each do |permission|
        return true unless (request.path =~ /#{permission.pattern}/).nil?
      end
      false
    end

    class Scope
      include Helpers::Indices

      attr_reader :user, :scope
      alias request scope

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

      def resolve
        return [] if user.nil?
        filter request.index, patterns
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
proxes-0.9.7 lib/proxes/policies/request_policy.rb