Class: PermissionPolicy::Strategies::BaseStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/permission_policy/strategies/base_strategy.rb

Overview

The base strategy defines the object API for all strategies which can be used for permission checks. Each strategy should inherit from it and implement #match? and #allowed?

Direct Known Subclasses

UnknownStrategy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(authorization, action = nil, options = {}) ⇒ BaseStrategy

Returns a new instance of BaseStrategy.



17
18
19
20
21
22
23
24
25
# File 'lib/permission_policy/strategies/base_strategy.rb', line 17

def initialize(authorization, action = nil, options = {})
  authorization.preconditions.each do |attribute|
    self.class.send(:attr_accessor, attribute)
    instance_variable_set(:"@#{attribute}", authorization.public_send(attribute))
  end

  self.action = action
  self.options = options
end

Instance Attribute Details

#actionObject

attributes which are available for #match? and #allowed? are passed from the authorization class.

precondition_attributes

for example [:current_user]

action

This will be :view or :manage

options

A hash having :subject or :feature as keys



15
16
17
# File 'lib/permission_policy/strategies/base_strategy.rb', line 15

def action
  @action
end

#optionsObject

attributes which are available for #match? and #allowed? are passed from the authorization class.

precondition_attributes

for example [:current_user]

action

This will be :view or :manage

options

A hash having :subject or :feature as keys



15
16
17
# File 'lib/permission_policy/strategies/base_strategy.rb', line 15

def options
  @options
end

Instance Method Details

#allowed?Boolean

Check if user has necessary permission Has to return true or false

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


37
38
39
40
41
# File 'lib/permission_policy/strategies/base_strategy.rb', line 37

def allowed?
  raise NotImplementedError, 'please implement #allowed? '\
    "for #{self.class.name} which should decide if the action is allowed, "\
    'based on the given attributes'
end

#match?Boolean

Check if the strategy is responsible for handling the permission check Has to return true or false

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


29
30
31
32
33
# File 'lib/permission_policy/strategies/base_strategy.rb', line 29

def match?
  raise NotImplementedError, 'please implement #match? '\
    "for #{self.class.name} which should return true or false, "\
    'depending on if it can decide #allowed?'
end