Class: ProxES::RequestPolicy

Inherits:
Object
  • Object
show all
Includes:
Helpers::Indices
Defined in:
lib/proxes/policies/request_policy.rb

Defined Under Namespace

Classes: Scope

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Indices

#filter

Constructor Details

#initialize(user, record) ⇒ RequestPolicy

Returns a new instance of RequestPolicy.



13
14
15
16
# File 'lib/proxes/policies/request_policy.rb', line 13

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/proxes/policies/request_policy.rb', line 18

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

    if record.indices?
      patterns = ProxES::Permission.where(verb: 'INDEX', role: user.roles).map do |permission|
        permission.pattern.gsub(/\{user.(.*)\}/) { |match| user.send(Regexp.last_match[1].to_sym) }
      end
      return filter(record.index, patterns).count.positive?
    else
      # Give me all the user's permissions that match the verb
      ProxES::Permission.where(verb: method_sym[0..-2].upcase, role: user.roles).each do |permission|
        return true if record.path =~ %r{#{permission.pattern}}
      end
    end
    false
  else
    super
  end
end

Instance Attribute Details

#recordObject (readonly)

Returns the value of attribute record.



11
12
13
# File 'lib/proxes/policies/request_policy.rb', line 11

def record
  @record
end

#userObject (readonly)

Returns the value of attribute user.



11
12
13
# File 'lib/proxes/policies/request_policy.rb', line 11

def user
  @user
end

Instance Method Details

#loggerObject



43
44
45
# File 'lib/proxes/policies/request_policy.rb', line 43

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

#respond_to_missing?(name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/proxes/policies/request_policy.rb', line 39

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