Sha256: 04d1076b432751b1f9dee37aa736f5764625e2fc23ed18a6b732840e6e0fc906

Contents?: true

Size: 863 Bytes

Versions: 2

Compression:

Stored size: 863 Bytes

Contents

module TenantLevelSecurity
  module SchemaStatements
    def create_policy(table_name)
      execute <<~SQL
        ALTER TABLE #{table_name} ENABLE ROW LEVEL SECURITY;
        ALTER TABLE #{table_name} FORCE ROW LEVEL SECURITY;
      SQL
      execute <<~SQL
        CREATE POLICY tenant_policy ON #{table_name}
          AS PERMISSIVE
          FOR ALL
          TO PUBLIC
          USING (tenant_id::text = current_setting('tenant_level_security.tenant_id'))
          WITH CHECK (tenant_id::text = current_setting('tenant_level_security.tenant_id'))
      SQL
    end

    def remove_policy(table_name)
      execute <<~SQL
        ALTER TABLE #{table_name} NO FORCE ROW LEVEL SECURITY;
        ALTER TABLE #{table_name} DISABLE ROW LEVEL SECURITY;
      SQL
      execute <<~SQL
        DROP POLICY tenant_policy ON #{table_name}
      SQL
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activerecord-tenant-level-security-0.1.0 lib/activerecord-tenant-level-security/schema_statements.rb
activerecord-tenant-level-security-0.0.1 lib/activerecord-tenant-level-security/schema_statements.rb