orfeas_lyra 0.9.3 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1923 -17
  3. data/README.md +15 -8
  4. data/Rakefile +40 -1
  5. data/app/controllers/lyra/application_controller.rb +108 -0
  6. data/app/controllers/lyra/dashboard_controller.rb +17 -15
  7. data/app/controllers/lyra/flow_controller.rb +30 -33
  8. data/app/controllers/lyra/privacy_controller.rb +57 -18
  9. data/app/views/lyra/dashboard/discrepancies.html.erb +5 -5
  10. data/app/views/lyra/dashboard/index.html.erb +1 -0
  11. data/app/views/lyra/flow/crud_mapping.html.erb +1 -1
  12. data/app/views/lyra/privacy/pii_detection.html.erb +1 -1
  13. data/app/views/lyra/privacy/policy.html.erb +7 -0
  14. data/app/views/lyra/privacy/rights.html.erb +78 -0
  15. data/config/privacy_policies.rb +0 -3
  16. data/config/routes.rb +1 -0
  17. data/lib/lyra/access_log.rb +187 -6
  18. data/lib/lyra/advisory_lock.rb +236 -0
  19. data/lib/lyra/aggregate.rb +1 -1
  20. data/lib/lyra/associations/habtm_links.rb +382 -19
  21. data/lib/lyra/associations/maintenance.rb +229 -0
  22. data/lib/lyra/attribution.rb +48 -0
  23. data/lib/lyra/bypass_events.rb +426 -45
  24. data/lib/lyra/command_handler.rb +19 -14
  25. data/lib/lyra/configuration.rb +237 -10
  26. data/lib/lyra/consistency/read_your_writes.rb +4 -2
  27. data/lib/lyra/correlation.rb +121 -0
  28. data/lib/lyra/domain_events.rb +4 -0
  29. data/lib/lyra/dual_view.rb +53 -16
  30. data/lib/lyra/encrypted_attributes.rb +5 -0
  31. data/lib/lyra/engine.rb +67 -0
  32. data/lib/lyra/erasure.rb +1002 -116
  33. data/lib/lyra/event.rb +35 -1
  34. data/lib/lyra/event_data.rb +38 -0
  35. data/lib/lyra/event_flow.rb +31 -15
  36. data/lib/lyra/event_store_adapter.rb +28 -0
  37. data/lib/lyra/event_window.rb +58 -0
  38. data/lib/lyra/genesis.rb +548 -44
  39. data/lib/lyra/interceptors/association_interceptor.rb +689 -34
  40. data/lib/lyra/interceptors/crud_interceptor.rb +547 -95
  41. data/lib/lyra/internal.rb +26 -0
  42. data/lib/lyra/mode_sync.rb +51 -3
  43. data/lib/lyra/mode_transition.rb +245 -34
  44. data/lib/lyra/model_selection.rb +35 -0
  45. data/lib/lyra/own_connection.rb +17 -2
  46. data/lib/lyra/privacy/adapters/pam.rb +25 -2
  47. data/lib/lyra/privacy/gdpr_compliance.rb +264 -36
  48. data/lib/lyra/privacy/interface.rb +27 -7
  49. data/lib/lyra/privacy/subject_links.rb +143 -0
  50. data/lib/lyra/privacy/subject_records.rb +369 -0
  51. data/lib/lyra/projection.rb +7 -1
  52. data/lib/lyra/projections/async_checkpoint.rb +32 -0
  53. data/lib/lyra/projections/async_projection_job.rb +166 -23
  54. data/lib/lyra/projections/cached_joins.rb +33 -18
  55. data/lib/lyra/projections/cached_projection.rb +74 -33
  56. data/lib/lyra/projections/cached_relation.rb +3175 -321
  57. data/lib/lyra/projections/default_scopes.rb +254 -0
  58. data/lib/lyra/projections/deferred_relation.rb +23 -3
  59. data/lib/lyra/projections/event_store_reader.rb +47 -8
  60. data/lib/lyra/projections/foreign_keys.rb +92 -0
  61. data/lib/lyra/projections/lazy_projection.rb +165 -27
  62. data/lib/lyra/projections/model_projection.rb +165 -14
  63. data/lib/lyra/projections/rebuild.rb +34 -6
  64. data/lib/lyra/projections/reference_links.rb +463 -72
  65. data/lib/lyra/projections/typed_values.rb +629 -0
  66. data/lib/lyra/projections/unique_indexes.rb +595 -0
  67. data/lib/lyra/projections/uniqueness.rb +784 -0
  68. data/lib/lyra/projections/value_digest.rb +93 -0
  69. data/lib/lyra/purpose_bound_reads.rb +912 -77
  70. data/lib/lyra/repair.rb +119 -28
  71. data/lib/lyra/retention.rb +114 -8
  72. data/lib/lyra/rights/consent_store.rb +415 -0
  73. data/lib/lyra/rights/erasure_guard.rb +135 -0
  74. data/lib/lyra/rights/pending.rb +74 -0
  75. data/lib/lyra/rights/queries.rb +269 -0
  76. data/lib/lyra/rights/store.rb +512 -0
  77. data/lib/lyra/rights/subject_keys.rb +224 -0
  78. data/lib/lyra/rights.rb +281 -0
  79. data/lib/lyra/saves_without_effect.rb +69 -0
  80. data/lib/lyra/stream_tail.rb +319 -0
  81. data/lib/lyra/strict_data_access.rb +906 -107
  82. data/lib/lyra/temporal.rb +4 -2
  83. data/lib/lyra/touch_registry.rb +404 -0
  84. data/lib/lyra/verification/bypass_workflow.rb +452 -42
  85. data/lib/lyra/verification/rights_workflows.rb +170 -0
  86. data/lib/lyra/version.rb +1 -1
  87. data/lib/lyra/visualization/event_graph.rb +1 -1
  88. data/lib/lyra/visualization/timeline.rb +2 -2
  89. data/lib/lyra.rb +43 -2
  90. data/lib/tasks/lyra_mode.rake +107 -8
  91. data/lib/tasks/lyra_projections.rake +43 -2
  92. metadata +34 -4
  93. data/lib/lyra/associations/event_aware.rb +0 -224
@@ -0,0 +1,224 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lyra
4
+ module Rights
5
+ # A record whose link to its people cannot be read (its row is gone, or
6
+ # its model cannot be queried for it). Raised rather than let a person's
7
+ # restriction or a missing consent go unseen.
8
+ class UnresolvedSubjectError < PamDsl::Error; end
9
+
10
+ # PamDsl.subject_key_resolver under Lyra: the keys a subject's rights and
11
+ # consent are recorded and looked up under.
12
+ #
13
+ # - A record: its own key first (Lyra.stream_name: Registration$42), then
14
+ # the keys of the people it names, by the selection rule of the
15
+ # subject-access copy (Lyra::Privacy::SubjectLinks), read the other way:
16
+ # - a belongs_to names the record of its class (the owner of the STI
17
+ # hierarchy): belongs_to :payer, class_name: "Person" gives Person$7
18
+ # for payer_id = 7, never Payer$7;
19
+ # - a polymorphic belongs_to names the class its type column holds;
20
+ # - a <type>_id column no association owns names the monitored model
21
+ # of that name (person_id: Person$7), and nothing when there is none;
22
+ # - a record does not name another record of its own model.
23
+ # A record loaded without one of these columns (a select that leaves out
24
+ # payer_id) has them read from its row, in one query Lyra makes as its
25
+ # own; when they cannot be read, UnresolvedSubjectError. PAM DSL asks for
26
+ # the keys only when a right is in force or consent is checked.
27
+ # - Anything else: nil, so PAM DSL's default applies (the subject as a
28
+ # string; none for nil or a model-wide "Model$*").
29
+ module SubjectKeys
30
+ Plan = Struct.new(:associations, :polymorphic, :convention, :columns, keyword_init: true)
31
+
32
+ @plans = {}
33
+ @mutex = Mutex.new
34
+
35
+ class << self
36
+ def call(subject)
37
+ return nil unless defined?(ActiveRecord::Base) && subject.is_a?(ActiveRecord::Base)
38
+ return [] if aggregate_row?(subject)
39
+
40
+ keys_for(subject)
41
+ end
42
+
43
+ # The record's own key and its people's keys, own first. A record
44
+ # narrowed from a whole one (ES-NoProj's select) carries the whole
45
+ # one's keys (remember).
46
+ def keys_for(record)
47
+ remembered = record.instance_variable_get(:@lyra_subject_keys)
48
+ return remembered if remembered
49
+
50
+ [own_key(record), *person_keys(record)].uniq
51
+ end
52
+
53
+ # Give +narrowed+ (a record built with some of +whole+'s columns) the
54
+ # keys of +whole+, so they need not be read again.
55
+ def remember(narrowed, whole)
56
+ narrowed.instance_variable_set(:@lyra_subject_keys, keys_for(whole))
57
+ narrowed
58
+ end
59
+
60
+ # A row of an aggregate (select("status, count(*) AS n").group(:status),
61
+ # find_by_sql with a GROUP BY): no key of its own, and columns the
62
+ # table does not have, or the rows of a grouped load. It is not one
63
+ # person's record, so it is not a subject.
64
+ def aggregate_row?(record)
65
+ return false if record.new_record?
66
+
67
+ key = Lyra.key_column(record.class)
68
+ return false if record.has_attribute?(key) && !record._read_attribute(key).nil?
69
+
70
+ PurposeBoundReads::Context[:lyra_grouped_load] ||
71
+ (record.attribute_names - columns(record.class)).any?
72
+ end
73
+
74
+ def own_key(record)
75
+ Lyra.stream_name(record.class, record.id)
76
+ end
77
+
78
+ # The keys of the people +record+ names, in the order of its links.
79
+ def person_keys(record)
80
+ model = record.class
81
+ own = Lyra.stream_owner_name(model)
82
+ plan = plan_for(model)
83
+ values = link_values(record, plan)
84
+ keys = []
85
+ plan.associations.each do |fk, type|
86
+ keys << "#{type}$#{values[fk]}" if present?(values[fk]) && type != own
87
+ end
88
+ plan.polymorphic.each do |fk, type_column|
89
+ next unless present?(values[fk]) && present?(values[type_column])
90
+
91
+ type = owner_of(values[type_column].to_s)
92
+ keys << "#{type}$#{values[fk]}" unless type == own
93
+ end
94
+ plan.convention.each do |fk, type|
95
+ keys << "#{type}$#{values[fk]}" if present?(values[fk]) && type != own
96
+ end
97
+ keys.uniq
98
+ end
99
+
100
+ # +model+'s record +id+, to read its keys from: its row, else (no row:
101
+ # ES-NoProj, a destroyed record) its last state replayed from its
102
+ # stream, as Lyra's own read; nil when it has neither.
103
+ def find_record(model, id)
104
+ PurposeBoundReads.internal(INTERNAL) do
105
+ row = model.unscoped.find_by(Lyra.key_column(model) => id)
106
+ next row if row
107
+
108
+ state = Lyra::DualView.new(model, id).event_sourced_state[:state]
109
+ next nil if state.nil? || state.empty?
110
+
111
+ attributes = state.transform_keys(&:to_s).slice(*model.column_names)
112
+ attributes[Lyra.key_column(model)] ||= id
113
+ model.instantiate(Lyra::Projections::TypedValues.database_attributes(model, attributes))
114
+ end
115
+ end
116
+
117
+ # The links of +model+'s rows to people: +associations+ and
118
+ # +convention+ as [[column, subject type]], +polymorphic+ as
119
+ # [[id column, type column]] (Rights::Queries reads them).
120
+ def links(model)
121
+ plan_for(model)
122
+ end
123
+
124
+ # Forget the cached plans (after a code reload).
125
+ def reset!
126
+ @mutex.synchronize { @plans = {} }
127
+ end
128
+
129
+ private
130
+
131
+ def present?(value) = !value.nil? && !value.to_s.empty?
132
+
133
+ def plan_for(model)
134
+ key = [model, Lyra.config.monitored_models.map(&:name).hash]
135
+ @plans[key] || @mutex.synchronize { @plans[key] ||= build_plan(model) }
136
+ end
137
+
138
+ def build_plan(model)
139
+ associations = []
140
+ polymorphic = []
141
+ owned = []
142
+ model.reflect_on_all_associations(:belongs_to).each do |reflection|
143
+ owned << reflection.foreign_key.to_s
144
+ if reflection.polymorphic?
145
+ polymorphic << [reflection.foreign_key.to_s, reflection.foreign_type.to_s]
146
+ else
147
+ target = reflection.class_name.to_s.safe_constantize
148
+ associations << [reflection.foreign_key.to_s, Lyra.stream_owner_name(target)] if active_record?(target)
149
+ end
150
+ rescue StandardError
151
+ next
152
+ end
153
+ columns = columns(model)
154
+ convention = columns.filter_map do |column|
155
+ next unless column.end_with?("_id") && column.length > 3 && !owned.include?(column)
156
+
157
+ type = monitored_model_for(column.delete_suffix("_id"))
158
+ [column, type] if type
159
+ end
160
+ link_columns = (associations.map(&:first) + polymorphic.flatten + convention.map(&:first)).uniq
161
+ link_columns &= columns unless columns.empty?
162
+ Plan.new(associations: associations, polymorphic: polymorphic, convention: convention,
163
+ columns: link_columns).freeze
164
+ end
165
+
166
+ # The monitored model whose name, underscored, is +name+ (person for
167
+ # Person, user for Spree::User), by the owner of its table.
168
+ def monitored_model_for(name)
169
+ model = Lyra.config.monitored_models.find { |m| m.name.to_s.demodulize.underscore == name }
170
+ model && Lyra.stream_owner_name(model)
171
+ end
172
+
173
+ def owner_of(type_name)
174
+ klass = Lyra.config.monitored_model_named(type_name) || type_name.safe_constantize
175
+ active_record?(klass) ? Lyra.stream_owner_name(klass) : type_name
176
+ end
177
+
178
+ def active_record?(klass)
179
+ klass.is_a?(Class) && klass < ActiveRecord::Base
180
+ end
181
+
182
+ # The link columns' values: the record's, and those it was loaded
183
+ # without, from its row.
184
+ def link_values(record, plan)
185
+ values = {}
186
+ missing = []
187
+ plan.columns.each do |column|
188
+ if record.has_attribute?(column)
189
+ values[column] = record._read_attribute(column)
190
+ else
191
+ missing << column
192
+ end
193
+ end
194
+ return values if missing.empty? || record.new_record?
195
+
196
+ values.merge(missing.zip(load_columns(record, missing)).to_h)
197
+ end
198
+
199
+ def load_columns(record, columns)
200
+ model = record.class
201
+ row = Lyra::PurposeBoundReads.internal(INTERNAL) do
202
+ model.unscoped.where(Lyra.key_column(model) => record.id).pick(*columns)
203
+ end
204
+ raise UnresolvedSubjectError, "#{own_key(record)} has no row to read #{columns.join(', ')} from" if row.nil?
205
+
206
+ columns.size == 1 ? [row] : row
207
+ rescue UnresolvedSubjectError
208
+ raise
209
+ rescue StandardError => e
210
+ raise UnresolvedSubjectError,
211
+ "#{own_key(record)} was loaded without #{columns.join(', ')}, which name the people it belongs to, " \
212
+ "and they could not be read (#{e.class}: #{e.message}). Select them too, so that their " \
213
+ "restrictions and consent can be checked"
214
+ end
215
+
216
+ def columns(model)
217
+ model.table_exists? ? model.column_names : []
218
+ rescue StandardError
219
+ []
220
+ end
221
+ end
222
+ end
223
+ end
224
+ end
@@ -0,0 +1,281 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lyra
4
+ # Lyra's durable store for the data subject's rights of Arts. 18 and 21
5
+ # (PamDsl.rights, PAM DSL's Rights::Ledger) and for consent (from 1.0.0).
6
+ #
7
+ # PAM DSL records and checks the rights; it keeps them, by default, in
8
+ # memory, per process, which a production application cannot rely on (its
9
+ # in-memory store refuses to record where a durable one is required). When
10
+ # Lyra and PAM DSL are both loaded, Lyra installs:
11
+ #
12
+ # - Rights::Store as PamDsl.rights_store: each request, notice, decision and
13
+ # lift as an event in the subject's stream Lyra::Rights$<subject key>
14
+ # (Lyra::Rights$Payer$7), linked to the index stream Lyra::Rights, with the
15
+ # lyra_rights table as its projection (one row per restriction or
16
+ # objection), written in the same transaction;
17
+ # - Rights::ConsentStore as PamDsl.consent_store: each consent request,
18
+ # grant and withdrawal as an event in Lyra::Consent$<subject key>, linked to
19
+ # Lyra::Consent, with the lyra_consents table as its projection (one row
20
+ # per subject key and purpose). Every policy and every process sees the
21
+ # same consent;
22
+ # - Rights::SubjectKeys as PamDsl.subject_key_resolver: a record maps to its
23
+ # own key (Registration$42) and to the keys of the people it names, by the
24
+ # selection rule of the subject-access copy (Lyra::Privacy::SubjectLinks).
25
+ #
26
+ # Both tables are Lyra's own, created on first use (Lyra.create_own_table).
27
+ # The streams are never erased: not with the access log
28
+ # (Lyra::AccessLog.erase!, which deletes Lyra::DataAccess$<subject> only),
29
+ # nor by Lyra::Erasure. They hold names, grounds and the controller's
30
+ # reasons, never values, and they are the evidence that a right was handled
31
+ # (Art. 5(2)). Keep personal data out of the free-text reasons.
32
+ #
33
+ # Lyra::Rights.rebuild! (bin/rails lyra:rights:rebuild) rebuilds both tables
34
+ # from the index streams.
35
+ module Rights
36
+ STREAM = "Lyra::Rights"
37
+ CONSENT_STREAM = "Lyra::Consent"
38
+ TABLE = "lyra_rights"
39
+ CONSENT_TABLE = "lyra_consents"
40
+
41
+ # The events, one class per PAM rights event type, and three for consent.
42
+ module Events
43
+ class RightsEvent < Lyra::Event; end
44
+ class ConsentEvent < Lyra::Event; end
45
+
46
+ PamDsl::Rights::EVENT_TYPES.each { |type| const_set(type, Class.new(RightsEvent)) }
47
+ CONSENT_TYPES = %w[ConsentRequested ConsentGranted ConsentWithdrawn].freeze
48
+ CONSENT_TYPES.each { |type| const_set(type, Class.new(ConsentEvent)) }
49
+ end
50
+
51
+ # Values in the events: names, symbols, times, arrays of them. Encoded so
52
+ # that they come back as they went in whatever serializer the event store
53
+ # uses (YAML keeps symbols and times; JSON does not).
54
+ module Codec
55
+ module_function
56
+
57
+ def encode(value)
58
+ case value
59
+ when Symbol then { "symbol" => value.to_s }
60
+ when Time, DateTime then { "time" => value.to_time.utc.iso8601(6) }
61
+ when ActiveSupport::TimeWithZone then { "time" => value.utc.iso8601(6) }
62
+ when Date then { "date" => value.iso8601 }
63
+ when Array then value.map { |v| encode(v) }
64
+ when Hash then { "hash" => value.to_h { |k, v| [k.to_s, encode(v)] } }
65
+ when String, Integer, Float, true, false, nil then value
66
+ else value.to_s
67
+ end
68
+ end
69
+
70
+ def decode(value)
71
+ case value
72
+ when Array then value.map { |v| decode(v) }
73
+ when Hash
74
+ tag, inner = value.first
75
+ case tag.to_s
76
+ when "symbol" then inner.to_sym
77
+ when "time" then Time.iso8601(inner)
78
+ when "date" then Date.iso8601(inner)
79
+ when "hash" then inner.to_h { |k, v| [k.to_sym, decode(v)] }
80
+ else value.to_h { |k, v| [k.to_sym, decode(v)] }
81
+ end
82
+ else value
83
+ end
84
+ end
85
+ end
86
+
87
+ class << self
88
+ def store
89
+ @store ||= Store.new
90
+ end
91
+
92
+ def consent_store
93
+ @consent_store ||= ConsentStore.new
94
+ end
95
+
96
+ # Install the stores and the resolver into PAM DSL: when Lyra loads with
97
+ # PAM DSL, and again by the engine at boot with +only_unset+, which
98
+ # leaves in place a store or resolver the application installed in an
99
+ # initializer (PamDsl.rights_store = ..., PamDsl.consent_store = ...,
100
+ # PamDsl.subject_key_resolver = ...). That is how an application opts
101
+ # out: install its own (PamDsl::RightsStore::Memory.new keeps PAM DSL's).
102
+ def install!(only_unset: false)
103
+ PamDsl.rights_store = store unless only_unset && host_rights_store?
104
+ PamDsl.consent_store = consent_store unless only_unset && PamDsl.consent_store
105
+ PamDsl.subject_key_resolver = SubjectKeys unless only_unset && PamDsl.subject_key_resolver
106
+ end
107
+
108
+ # Every write by any store of this process (for tests: whether any
109
+ # rights or consent were recorded).
110
+ def writes
111
+ @writes || 0
112
+ end
113
+
114
+ def note_write!
115
+ WRITES_MUTEX.synchronize { @writes = writes + 1 }
116
+ end
117
+
118
+ # Seconds a process answers from what it last read.
119
+ def refresh_interval
120
+ interval = Lyra.config.respond_to?(:rights_refresh_interval) ? Lyra.config.rights_refresh_interval : nil
121
+ interval.nil? ? 1.0 : interval.to_f
122
+ end
123
+
124
+ # Hold +table+ against other writers until the transaction ends
125
+ # (PostgreSQL; other databases serialise their writers anyway).
126
+ def lock_table(connection, table)
127
+ return unless connection.adapter_name.match?(/postg/i)
128
+
129
+ connection.execute("LOCK TABLE #{connection.quote_table_name(table)} IN EXCLUSIVE MODE")
130
+ end
131
+
132
+ def quote(connection, value)
133
+ case value
134
+ when Time, DateTime, ActiveSupport::TimeWithZone then connection.quote(value.to_time.utc)
135
+ else connection.quote(value)
136
+ end
137
+ end
138
+
139
+ # Drop what this process remembers: the next check reads the tables
140
+ # again. For the start of a checking scope (a request, a job, a batch).
141
+ def refresh!
142
+ store.refresh!
143
+ consent_store.forget!
144
+ end
145
+
146
+ def stream_for(subject_key)
147
+ "#{STREAM}$#{subject_key}"
148
+ end
149
+
150
+ def consent_stream_for(subject_key)
151
+ "#{CONSENT_STREAM}$#{subject_key}"
152
+ end
153
+
154
+ # "Payer$7" => ["Payer", "7"]; a bare id ("user-7") has no type.
155
+ def split_key(subject_key)
156
+ type, id = subject_key.to_s.split("$", 2)
157
+ id.nil? ? [nil, type] : [type, id]
158
+ end
159
+
160
+ # Rebuild lyra_rights and lyra_consents from the index streams. Returns
161
+ # [requests, consents] written.
162
+ def rebuild!
163
+ [store.rebuild!, consent_store.rebuild!]
164
+ end
165
+
166
+ # Run +block+ with an own table ready; false when the database cannot be
167
+ # reached (assets precompilation, a first setup).
168
+ def with_table(name, &definition)
169
+ ActiveRecord::Base.with_connection { |connection| Lyra.create_own_table(connection, name, &definition) }
170
+ true
171
+ rescue ActiveRecord::NoDatabaseError, ActiveRecord::ConnectionNotEstablished
172
+ false
173
+ end
174
+
175
+ def table_exists?(name)
176
+ ActiveRecord::Base.with_connection { |connection| connection.table_exists?(name) }
177
+ rescue ActiveRecord::NoDatabaseError, ActiveRecord::ConnectionNotEstablished
178
+ false
179
+ end
180
+
181
+ def now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
182
+
183
+ # ── Checking scopes ──
184
+ #
185
+ # The stores answer from what the process last read for at most
186
+ # config.rights_refresh_interval seconds, and never from what they read
187
+ # before the current checking scope began: the first check in a scope
188
+ # reads the rights afresh (one indexed query), so a right recorded in
189
+ # another process applies from that process's next scope. A scope is a
190
+ # Lyra.with_purpose block, a web request (Rights::Middleware), an
191
+ # Active Job (Rights::Job), and each batch of in_batches, find_each
192
+ # and find_in_batches. Opening one costs a clock read; nothing is read
193
+ # unless a check is made in it, so code that checks nothing (no
194
+ # purpose, reads_without_purpose :allow) never queries the rights.
195
+
196
+ # Run +block+ as a checking scope.
197
+ def scope(&block)
198
+ PurposeBoundReads::Context.with(:lyra_rights_scope, now, &block)
199
+ end
200
+
201
+ # When the current checking scope began (a monotonic clock reading), or
202
+ # nil outside one.
203
+ def scope_started_at
204
+ PurposeBoundReads::Context[:lyra_rights_scope]
205
+ end
206
+
207
+ # Whether a value read at +read_at+ may still answer: within the
208
+ # refresh interval, and, under config.rights_refresh :scope (the
209
+ # default), not read before the current scope began.
210
+ def current?(read_at)
211
+ return false unless read_at && now - read_at < refresh_interval
212
+ return true unless refresh_per_scope?
213
+
214
+ started = scope_started_at
215
+ started.nil? || read_at >= started
216
+ end
217
+
218
+ # config.rights_refresh: :scope (default) or :interval.
219
+ def refresh_per_scope?
220
+ mode = Lyra.config.respond_to?(:rights_refresh) ? Lyra.config.rights_refresh : nil
221
+ mode.nil? || mode == :scope
222
+ end
223
+
224
+ # A batch loop: the block gets a callable that ends one batch's scope
225
+ # and begins the next one's; the loop's first batch begins a scope too.
226
+ def batches
227
+ scope { yield -> { PurposeBoundReads::Context[:lyra_rights_scope] = now } }
228
+ end
229
+
230
+ # The subject keys with a request in force, from the snapshot (Lyra's
231
+ # store), or from any store's +active+.
232
+ def keys_in_force(store = PamDsl.rights_store)
233
+ return store.keys_in_force if store.respond_to?(:keys_in_force)
234
+
235
+ store.active.map(&:subject_key).uniq
236
+ end
237
+
238
+ private
239
+
240
+ # A rights store other than PAM DSL's own default: Lyra's, or one the
241
+ # application set (PamDsl.rights_store = nil returns to the default).
242
+ def host_rights_store?
243
+ current = PamDsl.instance_variable_get(:@rights_store)
244
+ !current.nil? && !PamDsl.instance_variable_get(:@rights_store_is_default)
245
+ end
246
+ end
247
+
248
+ WRITES_MUTEX = Mutex.new
249
+
250
+ # Rack middleware: each web request is a checking scope.
251
+ class Middleware
252
+ def initialize(app)
253
+ @app = app
254
+ end
255
+
256
+ def call(env)
257
+ Rights.scope { @app.call(env) }
258
+ end
259
+ end
260
+
261
+ # Active Job: each job is a checking scope (the engine includes it).
262
+ module Job
263
+ def self.included(base)
264
+ base.around_perform { |_job, block| Rights.scope(&block) }
265
+ end
266
+ end
267
+ end
268
+ end
269
+
270
+ require "lyra/rights/pending"
271
+ require "lyra/rights/subject_keys"
272
+ require "lyra/rights/store"
273
+ require "lyra/rights/consent_store"
274
+ require "lyra/rights/queries"
275
+ require "lyra/rights/erasure_guard"
276
+
277
+ Lyra::Rights.install! if defined?(PamDsl) && PamDsl.respond_to?(:rights_store=) && PamDsl.respond_to?(:consent_store=)
278
+
279
+ # Each Active Job is a checking scope (the web request's is the engine's
280
+ # middleware, Lyra::Rights::Middleware).
281
+ ActiveSupport.on_load(:active_job) { include Lyra::Rights::Job } if defined?(ActiveSupport)
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lyra
4
+ # The cause named by the writes a save's before_* callbacks made, when the
5
+ # save then ended without an event of its own in the log.
6
+ #
7
+ # A write's id is fixed when its write scope opens (Causation::WriteScope),
8
+ # and the writes its before_* callbacks make (a belongs_to autosave, a
9
+ # dependent: :destroy, the model's own before_save) name it as their cause:
10
+ # they are appended before the write's own event, and before anyone can
11
+ # know whether there will be one. Some writes then leave nothing in their
12
+ # stream: a save that changes nothing, an update or destroy whose row was
13
+ # gone (Monitor's stale copy), a save that was aborted, refused by its
14
+ # command or raised, one whose Monitor append failed. The cause would name
15
+ # no event. In that case only, Lyra appends a SaveWithoutEffect with the
16
+ # write's id, in the write's transaction, so the cause exists.
17
+ #
18
+ # The marker goes to one stream of Lyra's own, STREAM, never to the
19
+ # record's: the record's stream stays as the write left it (nothing may
20
+ # follow a Destroyed there), and nothing that reads a record's history sees
21
+ # it. One stream for every model, as for Lyra::UnrecordedWrites: a marker is
22
+ # looked up by its id, as a cause, never by record, and is rare. The name
23
+ # has no "$", so no reader takes it for a record's stream (Model$id).
24
+ #
25
+ # The marker names the stream owner, the record's key, the operation
26
+ # attempted and why it had no effect, and holds no attribute value. Its
27
+ # data has no model_class or model_id and is marked replay: false, so
28
+ # replay, the projections, ES-Lazy's catch-up, DualView and the subject
29
+ # reports pass it over; Lyra::EventWindow (the dashboard, EventFlow) leaves
30
+ # it out. A causation lookup (event_store.read.event(id),
31
+ # Causation.integrity) finds it.
32
+ module SavesWithoutEffect
33
+ STREAM = "Lyra::SavesWithoutEffect"
34
+ TYPE = "Lyra::Events::SaveWithoutEffect"
35
+ SOURCE = "lyra_save_without_effect"
36
+ # The operation config.metadata_proc is given, as for the write's own event.
37
+ EVENT_OPERATIONS = { create: :created, update: :updated, destroy: :destroyed }.freeze
38
+
39
+ module_function
40
+
41
+ def marker?(event)
42
+ event.respond_to?(:event_type) && event.event_type == TYPE
43
+ end
44
+
45
+ # Append the marker for +record+'s write scope +scope+: +operation+
46
+ # (:create, :update, :destroy), +reason+ ("no change", "no row", "not
47
+ # saved", "append failed"). Metadata as the write's own event would have
48
+ # had (CrudInterceptor#lyra_event_metadata). A failed append fails the
49
+ # write where events are required, and is logged in Monitor.
50
+ def record(scope, record, operation:, reason:)
51
+ event = Lyra::Events::SaveWithoutEffect.new(
52
+ event_id: scope.event_id,
53
+ data: { model: Lyra.stream_owner_name(record.class), record_id: record.id, operation: operation.to_s,
54
+ reason: reason, replay: false },
55
+ metadata: { source: SOURCE }.merge(record.lyra_event_metadata(EVENT_OPERATIONS.fetch(operation)).compact)
56
+ )
57
+ Lyra.append_events(event, stream_name: STREAM)
58
+ rescue => e
59
+ raise if Lyra.config.events_required?
60
+
61
+ Rails.logger.error("Lyra: could not record a save without effect (#{record.class.name} #{record.id}) - #{e.message}")
62
+ end
63
+ end
64
+
65
+ module Events
66
+ # See Lyra::SavesWithoutEffect.
67
+ class SaveWithoutEffect < Lyra::Event; end
68
+ end
69
+ end