Module: Eaco::Adapters::ActiveRecord::PostgresJSONb

Defined in:
lib/eaco/adapters/active_record/postgres_jsonb.rb

Overview

Authorized collection extractor on PostgreSQL >= 9.4 and a jsonb column named acl.

TODO negative authorizations (using a separate column?)

Instance Method Summary collapse

Instance Method Details

#accessible_by(actor) ⇒ ActiveRecord::Relation

Uses the json key existance operator ?| to check whether one of the Actor's Designator instances exist as keys in the ACL objects.

Parameters:

Returns:

  • (ActiveRecord::Relation)

    the authorized collection scope.


25
26
27
28
29
30
31
32
33
# File 'lib/eaco/adapters/active_record/postgres_jsonb.rb', line 25

def accessible_by(actor)
  return scoped if actor.is_admin?

  designators = actor.designators.map {|d| sanitize(d) }

  column = "#{connection.quote_table_name(table_name)}.acl"

  where("#{column} ?| array[#{designators.join(',')}]::varchar[]")
end