Class: Decidim::PermissionAction

Inherits:
Object
  • Object
show all
Defined in:
decidim-core/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 'decidim-core/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 'decidim-core/app/models/decidim/permission_action.rb', line 22

def action
  @action
end

#backtraceObject (readonly)

Returns the value of attribute backtrace


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

def backtrace
  @backtrace
end

#scopeObject (readonly)

Returns the value of attribute scope


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

def scope
  @scope
end

#subjectObject (readonly)

Returns the value of attribute subject


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

def subject
  @subject
end

Instance Method Details

#allow!Object


24
25
26
27
28
# File 'decidim-core/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:


34
35
36
37
38
# File 'decidim-core/app/models/decidim/permission_action.rb', line 34

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

  @state == :allowed
end

#disallow!Object


30
31
32
# File 'decidim-core/app/models/decidim/permission_action.rb', line 30

def disallow!
  @state = :disallowed
end

#matches?(scope, action, subject) ⇒ Boolean

Checks if this PermissionAction specifies the same scope, action and subject thant the ones provided as arguments.

Returns:

  • (Boolean)

46
47
48
49
50
51
# File 'decidim-core/app/models/decidim/permission_action.rb', line 46

def matches?(scope, action, subject)
  same = (self.action == action)
  same &&= (self.scope == scope)
  same &&= (self.subject == subject)
  same
end

#to_sObject


53
54
55
# File 'decidim-core/app/models/decidim/permission_action.rb', line 53

def to_s
  "!#{self.class.name}<action: #{action}, scope: #{scope}, subject: #{subject}, state: #{@state}>"
end

#trace(class_name, state) ⇒ Object


40
41
42
# File 'decidim-core/app/models/decidim/permission_action.rb', line 40

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