<commit_before>require 'administrative_account'

# Validates that the same public key is used when updating/deleting an envelope
class OriginalUserValidator < ActiveModel::Validator
  attr_reader :record

  def validate(record)
    @record = record

    return unless (locations_mismatch? || keys_differ?) &&
                  !administrative_account?

    record.errors.add :resource, 'can only be updated by the original user'
  end

  private

  def locations_mismatch?
    return false unless record.processed_resource['registry_metadata']

    (original_key_locations & updated_key_locations).empty?
  end

  def keys_differ?
    record.resource_public_key_changed?
  end

  def administrative_account?
    AdministrativeAccount.exists?(public_key: record.resource_public_key)
  end

  def original_key_locations
    record.processed_resource_was.dig('registry_metadata',
                                      'digital_signature',
                                      'key_location')
  end

  def updated_key_locations
    # record.lr_metadata.digital_signature.key_location
    record.processed_resource.dig('registry_metadata',
                                  'digital_signature',
                                  'key_location')
  end
end<commit_msg>Fix issue when checking original_user for deleting envelopes<commit_after>require 'administrative_account'

# Validates that the same public key is used when updating/deleting an envelope
class OriginalUserValidator < ActiveModel::Validator
  attr_reader :record

  def validate(record)
    @record = record

    return unless (locations_mismatch? || keys_differ?) && !administrative_account?

    record.errors.add :resource, 'can only be updated by the original user'
  end

  private

  def locations_mismatch?
    return false unless record.processed_resource['registry_metadata']

    (original_key_locations & updated_key_locations).empty?
  end

  def keys_differ?
    cleaned_key(record.resource_public_key) != cleaned_key(record.resource_public_key_was)
  end

  def administrative_account?
    AdministrativeAccount.exists?(public_key: record.resource_public_key)
  end

  def original_key_locations
    record.processed_resource_was.dig('registry_metadata',
                                      'digital_signature',
                                      'key_location')
  end

  def updated_key_locations
    # record.lr_metadata.digital_signature.key_location
    record.processed_resource.dig('registry_metadata',
                                  'digital_signature',
                                  'key_location')
  end

  def cleaned_key(key)
    key.strip.gsub(/\n$/, '')
  end
end
{'fraction_non_alphanumeric': 0.06719219219219219, 'fraction_numerical': 0.0, 'mean_word_length': 2.9191176470588234, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5383191', 'n_tokens_mistral': 738, 'n_tokens_neox': 689, 'n_words': 155}