Class: OpenStax::Utilities::AccessPolicy

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/openstax/utilities/access_policy.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAccessPolicy

Returns a new instance of AccessPolicy.



10
11
12
# File 'lib/openstax/utilities/access_policy.rb', line 10

def initialize()
  @resource_policy_map = {}
end

Instance Attribute Details

#resource_policy_mapObject (readonly)

Returns the value of attribute resource_policy_map.



8
9
10
# File 'lib/openstax/utilities/access_policy.rb', line 8

def resource_policy_map
  @resource_policy_map
end

Class Method Details

.action_allowed?(action, requestor, resource) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/openstax/utilities/access_policy.rb', line 31

def self.action_allowed?(action, requestor, resource)

  # If the incoming requestor is an ApiUser, choose to use either its
  # human_user or its application.  If there is a human user involved, it
  # should always take precedence when testing for access.
  if defined?(OpenStax::Api::ApiUser) &&
     requestor.is_a?(OpenStax::Api::ApiUser)
    requestor = requestor.human_user ? requestor.human_user : requestor.application
  end

  resource_class = resource.is_a?(Class) ? resource : resource.class
  policy_class = instance.resource_policy_map[resource_class.to_s].try(:constantize)

  # If there is no policy registered, we by default deny access
  return false if policy_class.nil?

  policy_class.action_allowed?(action, requestor, resource)
end

.method_missing(method_name, *arguments, &block) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/openstax/utilities/access_policy.rb', line 14

def self.method_missing(method_name, *arguments, &block)
  if method_name.to_s =~ /(.*)_allowed?/
    action_allowed?(*arguments.unshift($1.to_sym), &block)
  else
    super
  end
end

.register(resource_class, policy_class) ⇒ Object



50
51
52
# File 'lib/openstax/utilities/access_policy.rb', line 50

def self.register(resource_class, policy_class)
  self.instance.resource_policy_map[resource_class.to_s] = policy_class.to_s
end

.require_action_allowed!(action, requestor, resource) ⇒ Object



26
27
28
29
# File 'lib/openstax/utilities/access_policy.rb', line 26

def self.require_action_allowed!(action, requestor, resource)
  msg = "\"#{requestor.inspect}\" is not allowed to perform \"#{action}\" on \"#{resource.inspect}\""
  raise(SecurityTransgression, msg) unless action_allowed?(action, requestor, resource)
end

.respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/openstax/utilities/access_policy.rb', line 22

def self.respond_to_missing?(method_name, include_private = false)
  method_name.to_s.end_with?('_allowed?') || super
end