Module: PgSqlTriggers::KillSwitchProtection

Extended by:
ActiveSupport::Concern
Included in:
ApplicationController
Defined in:
app/controllers/concerns/pg_sql_triggers/kill_switch_protection.rb

Instance Method Summary collapse

Instance Method Details

#check_kill_switch(operation:, confirmation: nil) ⇒ true

Checks kill switch before executing a dangerous operation. Raises KillSwitchError if the operation is blocked.

Parameters:

  • operation (Symbol)

    The operation being performed

  • confirmation (String, nil) (defaults to: nil)

    Optional confirmation text from params

Returns:

  • (true)

    If the operation is allowed

Raises:



26
27
28
29
30
31
32
33
# File 'app/controllers/concerns/pg_sql_triggers/kill_switch_protection.rb', line 26

def check_kill_switch(operation:, confirmation: nil)
  PgSqlTriggers::SQL::KillSwitch.check!(
    operation: operation,
    environment: current_environment,
    confirmation: confirmation,
    actor: current_actor
  )
end

#current_environmentString

Returns the current environment.

Returns:

  • (String)

    The current Rails environment



62
63
64
# File 'app/controllers/concerns/pg_sql_triggers/kill_switch_protection.rb', line 62

def current_environment
  Rails.env
end

#expected_confirmation_text(operation) ⇒ String

Returns the expected confirmation text for an operation (for use in views).

Parameters:

  • operation (Symbol)

    The operation name

Returns:

  • (String)

    The expected confirmation text



50
51
52
53
54
55
56
57
# File 'app/controllers/concerns/pg_sql_triggers/kill_switch_protection.rb', line 50

def expected_confirmation_text(operation)
  if PgSqlTriggers.respond_to?(:kill_switch_confirmation_pattern) &&
     PgSqlTriggers.kill_switch_confirmation_pattern.respond_to?(:call)
    PgSqlTriggers.kill_switch_confirmation_pattern.call(operation)
  else
    "EXECUTE #{operation.to_s.upcase}"
  end
end

#kill_switch_active?Boolean

Checks if kill switch is active for the current environment.

Returns:

  • (Boolean)

    true if kill switch is active, false otherwise



15
16
17
# File 'app/controllers/concerns/pg_sql_triggers/kill_switch_protection.rb', line 15

def kill_switch_active?
  PgSqlTriggers::SQL::KillSwitch.active?(environment: current_environment)
end

#require_kill_switch_override(operation, confirmation: nil) ⇒ Object

Before action to require kill switch override for an action. Add to specific controller actions that need protection:

before_action -> { require_kill_switch_override(:operation_name) }, only: [:dangerous_action]

Parameters:

  • operation (Symbol)

    The operation name

  • confirmation (String, nil) (defaults to: nil)

    Optional confirmation text

Raises:



42
43
44
# File 'app/controllers/concerns/pg_sql_triggers/kill_switch_protection.rb', line 42

def require_kill_switch_override(operation, confirmation: nil)
  check_kill_switch(operation: operation, confirmation: confirmation)
end