Skip to content

Commit

Permalink
Save encrypted attributes before initialize and install filtered para…
Browse files Browse the repository at this point in the history
…ms after
  • Loading branch information
duduribeiro committed Jun 19, 2023
1 parent 55442ba commit 95b3e01
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
19 changes: 14 additions & 5 deletions activerecord/lib/active_record/encryption/configurable.rb
Expand Up @@ -9,6 +9,7 @@ module Configurable
included do
mattr_reader :config, default: Config.new
mattr_accessor :encrypted_attribute_declaration_listeners
mattr_reader :encrypted_attributes, default: Concurrent::Map.new
end

class_methods do
Expand Down Expand Up @@ -48,12 +49,20 @@ def encrypted_attribute_was_declared(klass, name) # :nodoc:
end
end

def install_auto_filtered_parameters_hook(app) # :nodoc:
def install_encrypted_attributed_declared_hook()
ActiveRecord::Encryption.on_encrypted_attribute_declared do |klass, encrypted_attribute_name|
filter = [("#{klass.model_name.element}" if klass.name), encrypted_attribute_name.to_s].compact.join(".")
unless excluded_from_filter_parameters?(filter)
app.config.filter_parameters << filter unless app.config.filter_parameters.include?(filter)
klass.filter_attributes += [encrypted_attribute_name]
encrypted_attributes.fetch_or_store(klass, []) << encrypted_attribute_name
end
end

def install_auto_filtered_parameters(app) # :nodoc:
encrypted_attributes.each do |klass, attributes|
attributes.each do |encrypted_attribute_name|
filter = [("#{klass.model_name.element}" if klass.name), encrypted_attribute_name.to_s].compact.join(".")
unless excluded_from_filter_parameters?(filter)
app.config.filter_parameters << filter unless app.config.filter_parameters.include?(filter)
klass.filter_attributes += [encrypted_attribute_name]
end
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion activerecord/lib/active_record/railtie.rb
Expand Up @@ -357,6 +357,8 @@ class Railtie < Rails::Railtie # :nodoc:
end

initializer "active_record_encryption.configuration" do |app|
ActiveRecord::Encryption.install_encrypted_attributed_declared_hook

config.after_initialize do |app|
ActiveRecord::Encryption.configure \
primary_key: app.credentials.dig(:active_record_encryption, :primary_key),
Expand All @@ -382,7 +384,7 @@ class Railtie < Rails::Railtie # :nodoc:

# Filtered params
if ActiveRecord::Encryption.config.add_to_filter_parameters
ActiveRecord::Encryption.install_auto_filtered_parameters_hook(app)
ActiveRecord::Encryption.install_auto_filtered_parameters(app)
end
end
end
Expand Down

0 comments on commit 95b3e01

Please sign in to comment.