module TenantLevelSecurity

Constants

VERSION

Public Class Methods

current_session_tenant_id() click to toggle source
# File lib/activerecord-tenant-level-security/tenant_level_security.rb, line 38
def current_session_tenant_id
  ActiveRecord::Base.connection.execute('SHOW tenant_level_security.tenant_id').getvalue(0, 0)
rescue ActiveRecord::StatementInvalid => e
  return nil if e.cause.kind_of? PG::UndefinedObject
  raise
end
current_tenant_id(&block) click to toggle source

The current_tenant_id sets the default tenant from the outside. Be sure to register in advance as `TenantLevelSecurity.current_tenant_id { id }` with initializers. This value is mainly used as the current value when reusing a connection. Therefore, keep in mind that you need to manage it differently from the session value in the database.

# File lib/activerecord-tenant-level-security/tenant_level_security.rb, line 7
def current_tenant_id(&block)
  if block_given?
    @@block = block
  else
    @@block.call
  end
end
switch!(tenant_id) click to toggle source
# File lib/activerecord-tenant-level-security/tenant_level_security.rb, line 26
def switch!(tenant_id)
  switch_with_connection!(ActiveRecord::Base.connection, tenant_id)
end
switch_with_connection!(conn, tenant_id) click to toggle source
# File lib/activerecord-tenant-level-security/tenant_level_security.rb, line 30
def switch_with_connection!(conn, tenant_id)
  if tenant_id.present?
    conn.execute("SET tenant_level_security.tenant_id = '#{tenant_id}'")
  else
    conn.execute('SET tenant_level_security.tenant_id TO DEFAULT')
  end
end
with(tenant_id) { || ... } click to toggle source
# File lib/activerecord-tenant-level-security/tenant_level_security.rb, line 15
def with(tenant_id)
  old_tenant_id = current_session_tenant_id
  return yield if old_tenant_id == tenant_id
  begin
    switch! tenant_id
    yield
  ensure
    switch! old_tenant_id
  end
end