Module: PgRls

Extended by:
Forwardable
Defined in:
lib/pg_rls.rb,
lib/pg_rls/error.rb,
lib/pg_rls/engine.rb,
lib/pg_rls/railtie.rb,
lib/pg_rls/version.rb,
lib/pg_rls/deprecation.rb,
app/models/pg_rls/admin.rb,
app/models/pg_rls/record.rb,
app/models/pg_rls/tenant.rb,
lib/pg_rls/active_record.rb,
app/models/pg_rls/current.rb,
lib/pg_rls/active_support.rb,
lib/pg_rls/connection_config.rb,
app/models/pg_rls/tenant/securable.rb,
lib/pg_rls/active_record/migration.rb,
app/models/pg_rls/tenant/searchable.rb,
app/models/pg_rls/tenant/switchable.rb,
lib/pg_rls/active_support/string_ext.rb,
lib/generators/pg_rls/pg_rls_generator.rb,
lib/pg_rls/active_record/test_databases.rb,
lib/pg_rls/active_record/database_shards.rb,
lib/pg_rls/active_record/connection_adapters.rb,
lib/generators/pg_rls/install/install_generator.rb,
lib/pg_rls/active_record/migration/command_recorder.rb,
lib/pg_rls/active_record/connection_adapters/postgre_sql.rb,
lib/generators/pg_rls/active_record/active_record_generator.rb,
lib/pg_rls/active_record/connection_adapters/connection_pool.rb,
lib/pg_rls/active_record/connection_adapters/postgre_sql/errors.rb,
lib/pg_rls/active_record/connection_adapters/postgre_sql/rls_policies.rb,
lib/pg_rls/active_record/connection_adapters/postgre_sql/rls_triggers.rb,
lib/pg_rls/active_record/connection_adapters/postgre_sql/rls_functions.rb,
lib/pg_rls/active_record/connection_adapters/postgre_sql/schema_dumper.rb,
lib/pg_rls/active_record/connection_adapters/postgre_sql/schema_statements.rb,
lib/pg_rls/active_record/connection_adapters/postgre_sql/sql_helper_method.rb,
lib/pg_rls/active_record/connection_adapters/postgre_sql/rls_user_statements.rb,
lib/pg_rls/active_record/connection_adapters/postgre_sql/check_rls_user_privileges.rb,
lib/pg_rls/active_record/connection_adapters/postgre_sql/grant_rls_user_privileges.rb

Overview

Row Level Security for PostgreSQL

Defined Under Namespace

Modules: ActiveRecord, ActiveSupport, Deprecation, Generators Classes: Admin, ConnectionConfig, Current, Engine, Error, InstallGenerator, Railtie, Record, Tenant

Constant Summary collapse

DEFAULT_CONFIG_MAP =
{
  "@@search_methods": %i[subdomain tenant_id id],
  "@@table_name": :organizations,
  "@@class_name": :Organization,
  "@@username": :app_user,
  "@@password": :password,
  "@@schema": :public,
  "@@rls_role_group": :rls_group,
  "@@current_attributes": Array(nil),
  "@@abstract_base_record_class": "ActiveRecord::Base"
}.freeze
VERSION =
"1.0.1"
@@search_methods =
%i[subdomain tenant_id id]
@@table_name =
:organizations
@@class_name =
:Organization
@@username =
ENV.fetch("POSTGRES_RLS_USERNAME", "app_user")
@@password =
ENV.fetch("POSTGRES_RLS_PASSWORD", "password")
@@schema =
:public
@@rls_role_group =
:rls_group
@@connects_to =
nil
@@current_attributes =
Array(nil)
@@abstract_base_record_class =
"ActiveRecord::Base"

Class Method Summary collapse

Class Method Details

.admin_execute(sql = nil, &block) ⇒ Object

Alias for the Admin.execute method



17
18
19
20
21
22
23
# File 'app/models/pg_rls/admin.rb', line 17

def self.admin_execute(sql = nil, &block)
  Deprecation.warn(
    "This method is deprecated and will be removed in future versions. " \
    "please use PgRls::Admin.execute instead."
  )
  Admin.execute(sql, &block)
end

.freeze_config!Object

:nocov:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/pg_rls.rb', line 56

def freeze_config! # rubocop:disable Metrics/MethodLength
  PgRls.singleton_class.class_eval do
    PgRls.class_variables.each do |var_name|
      method_name = var_name.to_s.delete_prefix("@@").to_sym
      setter_method_name = :"#{method_name}="
      const_name = "#{method_name.to_s.upcase}_FROZEN_CONFIGURATION"

      remove_method setter_method_name if method_defined?(setter_method_name)

      PgRls.const_set(const_name, PgRls.class_variable_get(var_name))

      define_method(method_name) do
        PgRls.const_get(const_name)
      end

      PgRls.remove_class_variable(var_name)
    end
  end
end

.main_modelObject



45
46
47
# File 'lib/pg_rls.rb', line 45

def main_model
  class_name.to_s.camelize.constantize
end

.reset_config!Object



49
50
51
52
53
# File 'lib/pg_rls.rb', line 49

def reset_config!
  PgRls.class_variables.each do |var|
    PgRls.class_variable_set(var, DEFAULT_CONFIG_MAP[var])
  end
end

.setup {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (PgRls)

    the object that the method was called on



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pg_rls.rb', line 32

def setup
  PgRls.reset_config!
  Rails.application.config.to_prepare do
    PgRls::Record.ignored_columns += %w[tenant_id]
  end

  yield self

  connection_config = PgRls::ConnectionConfig.new
  connection_config.look_up_connection_config unless connection_config.connection_config?
  freeze_config! unless Rails.env.test?
end