Module: Sipity::Policies

Defined in:
app/policies/sipity/policies.rb,
app/policies/sipity/policies/sip_policy.rb,
app/policies/sipity/policies/base_policy.rb,
app/policies/sipity/policies/enrich_sip_by_form_submission_policy.rb

Overview

Contains the various Policies associated with Sipity.

A Policy is an object responsible for answering questions about actions the given user is attempting to take on a given object.

The Policy subsystem is responsible for three major concerns:

  • Given a user, entity, and policy question; Can the user do it? This is answered via a Policy object.

  • Given a user and a policy question, what are all of the entities that can be acted upon? This is answered via a Policy::Scope object.

  • Given a user and an entity, what are all of the actions the user can take on the entity? This is answered via a permitted_actions query. NOTE: This is a concept that is not part of the Pundit ecosystem. It is also a theoretical exercise.

See Also:

  • Pundit gem](http://github.com/elabs/pundit) for further explanation of Policy and Scope objects.

Defined Under Namespace

Classes: BasePolicy, EnrichSipByFormSubmissionPolicy, SipPolicy

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.authorized_for?(user:, policy_question:, entity:) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
# File 'app/policies/sipity/policies.rb', line 28

def authorized_for?(user:, policy_question:, entity:)
  policy_enforcer = find_policy_enforcer_for(entity: entity)
  policy_enforcer.call(user: user, entity: entity, policy_question: policy_question)
end

.find_policy_enforcer_for(entity:) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'app/policies/sipity/policies.rb', line 33

def find_policy_enforcer_for(entity:)
  return entity.policy_enforcer if entity.respond_to?(:policy_enforcer) && entity.policy_enforcer.present?
  policy_name_as_constant = "#{entity.class.to_s.demodulize}Policy"
  if const_defined?(policy_name_as_constant)
    const_get(policy_name_as_constant)
  else
    fail Exceptions::PolicyNotFoundError, name: policy_name_as_constant, container: self
  end
end

Instance Method Details

#authorized_for?(user:, policy_question:, entity:) ⇒ Object (private)



28
29
30
31
# File 'app/policies/sipity/policies.rb', line 28

def authorized_for?(user:, policy_question:, entity:)
  policy_enforcer = find_policy_enforcer_for(entity: entity)
  policy_enforcer.call(user: user, entity: entity, policy_question: policy_question)
end

#find_policy_enforcer_for(entity:) ⇒ Object (private)



33
34
35
36
37
38
39
40
41
# File 'app/policies/sipity/policies.rb', line 33

def find_policy_enforcer_for(entity:)
  return entity.policy_enforcer if entity.respond_to?(:policy_enforcer) && entity.policy_enforcer.present?
  policy_name_as_constant = "#{entity.class.to_s.demodulize}Policy"
  if const_defined?(policy_name_as_constant)
    const_get(policy_name_as_constant)
  else
    fail Exceptions::PolicyNotFoundError, name: policy_name_as_constant, container: self
  end
end