Class: Decidim::PermissionAction

Inherits:
Object
  • Object
show all
Defined in:
app/models/decidim/permission_action.rb

Overview

This class encapsulates an action, which will be used by the permissions system to check if the user is allowed to perform it.

It consists of a ‘scope` (which will typically be either `:public` or `:admin`), the name of the `:action` that is being performed and the `:subject` of the action.

Defined Under Namespace

Classes: PermissionCannotBeDisallowedError, PermissionNotSetError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action:, scope:, subject:) ⇒ PermissionAction

action - a Symbol representing the action being performed scope - a Symbol representing the scope of the action subject - a Symbol representing the subject of the action



14
15
16
17
18
19
20
# File 'app/models/decidim/permission_action.rb', line 14

def initialize(action:, scope:, subject:)
  @action = action
  @scope = scope
  @subject = subject
  @state = nil
  @backtrace = []
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



22
23
24
# File 'app/models/decidim/permission_action.rb', line 22

def action
  @action
end

#backtraceObject (readonly)

Returns the value of attribute backtrace.



22
23
24
# File 'app/models/decidim/permission_action.rb', line 22

def backtrace
  @backtrace
end

#scopeObject (readonly)

Returns the value of attribute scope.



22
23
24
# File 'app/models/decidim/permission_action.rb', line 22

def scope
  @scope
end

#subjectObject (readonly)

Returns the value of attribute subject.



22
23
24
# File 'app/models/decidim/permission_action.rb', line 22

def subject
  @subject
end

Instance Method Details

#allow!Object



24
25
26
27
# File 'app/models/decidim/permission_action.rb', line 24

def allow!
  raise PermissionCannotBeDisallowedError, "Allowing a previously disallowed action is not permitted: #{inspect}" if @state == :disallowed
  @state = :allowed
end

#allowed?Boolean

Returns:

  • (Boolean)

Raises:



33
34
35
36
# File 'app/models/decidim/permission_action.rb', line 33

def allowed?
  raise PermissionNotSetError, "Permission hasn't been allowed or disallowed yet: #{inspect}" if @state.blank?
  @state == :allowed
end

#disallow!Object



29
30
31
# File 'app/models/decidim/permission_action.rb', line 29

def disallow!
  @state = :disallowed
end

#trace(class_name, state) ⇒ Object



38
39
40
# File 'app/models/decidim/permission_action.rb', line 38

def trace(class_name, state)
  @backtrace << [class_name, state]
end