Class: ActiveElement::PermissionsCheck

Inherits:
Object
  • Object
show all
Includes:
Paintbrush
Defined in:
lib/active_element/permissions_check.rb

Overview

Verifies provided permissions against required permissions.

Instance Method Summary collapse

Constructor Details

#initialize(required:, actual:, controller_path:, action_name:, rails_component:) ⇒ PermissionsCheck

Returns a new instance of PermissionsCheck.



8
9
10
11
12
13
14
15
# File 'lib/active_element/permissions_check.rb', line 8

def initialize(required:, actual:, controller_path:, action_name:, rails_component:)
  @required = required.presence || []
  @actual = normalized(actual)
  @controller_name = controller_path.to_s.gsub('/', '_')
  @action_name = action_name.to_s
  @rails_component = rails_component
  raise_unprotected_route_error if applicable.empty?
end

Instance Method Details

#applicableObject



36
37
38
# File 'lib/active_element/permissions_check.rb', line 36

def applicable
  @applicable ||= default_permissions + required_permissions
end

#applicable_permissionsObject



40
41
42
# File 'lib/active_element/permissions_check.rb', line 40

def applicable_permissions
  applicable.map { |permission| permission.fetch(:with) }
end

#messageObject



21
22
23
24
25
26
# File 'lib/active_element/permissions_check.rb', line 21

def message
  return development_environment_message if rails_component.environment == 'development'
  return permitted_message if permitted?

  forbidden_message
end

#missingObject



28
29
30
31
32
33
34
# File 'lib/active_element/permissions_check.rb', line 28

def missing
  @missing ||= applicable.reject do |permission|
    next true if permission.fetch(:always, false)

    actual.include?(permission.fetch(:with).to_s)
  end
end

#missing_permissionsObject



44
45
46
# File 'lib/active_element/permissions_check.rb', line 44

def missing_permissions
  missing.map { |permission| permission.fetch(:with) }
end

#permitted?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/active_element/permissions_check.rb', line 17

def permitted?
  rails_component.environment == 'development' || missing.blank?
end