Class: Yes::Core::Authorization::ReadModelsAuthorizer

Inherits:
Object
  • Object
show all
Defined in:
lib/yes/core/authorization/read_models_authorizer.rb

Overview

Authorizes a collection of read model records by delegating to per-record authorizers.

Constant Summary collapse

NotAuthorized =
Class.new(Yes::Core::Error)

Class Method Summary collapse

Class Method Details

.call(read_model_name, records, auth_data) ⇒ Object

Parameters:

  • read_model_name (String)

    name of the read model

  • records (Array<ApplicationRecord>)

    records to authorize

  • auth_data (Hash)

    authorization data

Raises:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/yes/core/authorization/read_models_authorizer.rb', line 15

def call(read_model_name, records, auth_data)
  authorizer = authorizer_for(read_model_name)

  return unless authorizer

  unauthorized = []
  records.each do |record|
    authorizer.call(record, auth_data)
  rescue ReadModelAuthorizer::NotAuthorized => e
    unauthorized << {
      message: e.message,
      model_type: record.class.to_s,
      model_id: record.id
    }
  end

  raise NotAuthorized.new(extra: unauthorized) if unauthorized.any?
end