Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 26 additions & 22 deletions lib/phi_attrs/phi_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -633,23 +633,25 @@ def phi_wrap_method(method_name)
wrapped_method = :"__#{method_name}_phi_wrapped"
unwrapped_method = :"__#{method_name}_phi_unwrapped"

self.class.send(:define_method, wrapped_method) do |*args, **kwargs, &block|
PhiAttrs::Logger.tagged(*phi_log_keys) do
unless phi_allowed?
raise PhiAttrs::Exceptions::PhiAccessException, "Attempted PHI access for #{self.class.name} #{@__phi_user_id}"
end
unless self.class.method_defined?(wrapped_method)
self.class.send(:define_method, wrapped_method) do |*args, **kwargs, &block|
PhiAttrs::Logger.tagged(*phi_log_keys) do
unless phi_allowed?
raise PhiAttrs::Exceptions::PhiAccessException, "Attempted PHI access for #{self.class.name} #{@__phi_user_id}"
end

unless all_phi_context_logged?
PhiAttrs::Logger.info("#{self.class.name} access by [#{all_phi_allowed_by}]. Triggered by method: #{method_name}")
set_all_phi_context_logged
end
unless all_phi_context_logged?
PhiAttrs::Logger.info("#{self.class.name} access by [#{all_phi_allowed_by}]. Triggered by method: #{method_name}")
set_all_phi_context_logged
end

send(unwrapped_method, *args, **kwargs, &block)
send(unwrapped_method, *args, **kwargs, &block)
end
end
end

# method_name => wrapped_method => unwrapped_method
self.class.send(:alias_method, unwrapped_method, method_name)
self.class.send(:alias_method, unwrapped_method, method_name) unless self.class.method_defined?(unwrapped_method)
self.class.send(:alias_method, method_name, wrapped_method)

self.class.__phi_methods_wrapped << method_name
Expand All @@ -669,23 +671,25 @@ def phi_wrap_extension(method_name)
wrapped_method = wrapped_extended_name(method_name)
unwrapped_method = unwrapped_extended_name(method_name)

self.class.send(:define_method, wrapped_method) do |*args, **kwargs, &block|
relation = send(unwrapped_method, *args, **kwargs, &block)
unless self.class.method_defined?(wrapped_method)
self.class.send(:define_method, wrapped_method) do |*args, **kwargs, &block|
relation = send(unwrapped_method, *args, **kwargs, &block)

if phi_allowed? && (relation.present? && relation_klass(relation).included_modules.include?(PhiRecord))
relations = relation.is_a?(Enumerable) ? relation : [relation]
relations.each do |r|
r.allow_phi!(phi_allowed_by, phi_access_reason) unless @__phi_relations_extended.include?(r)
if phi_allowed? && (relation.present? && relation_klass(relation).included_modules.include?(PhiRecord))
relations = relation.is_a?(Enumerable) ? relation : [relation]
relations.each do |r|
r.allow_phi!(phi_allowed_by, phi_access_reason) unless @__phi_relations_extended.include?(r)
end
@__phi_relations_extended.merge(relations)
self.class.__instances_with_extended_phi.add(self)
end
@__phi_relations_extended.merge(relations)
self.class.__instances_with_extended_phi.add(self)
end

relation
relation
end
end

# method_name => wrapped_method => unwrapped_method
self.class.send(:alias_method, unwrapped_method, method_name)
self.class.send(:alias_method, unwrapped_method, method_name) unless self.class.method_defined?(unwrapped_method)
self.class.send(:alias_method, method_name, wrapped_method)

self.class.__phi_methods_to_extend << method_name
Expand Down