prefill signature
This commit is contained in:
@@ -5,20 +5,21 @@ module Submitters
|
||||
DEFAULT_KEYS = [AccountConfig::FORM_COMPLETED_BUTTON_KEY,
|
||||
AccountConfig::FORM_COMPLETED_MESSAGE_KEY,
|
||||
AccountConfig::FORM_WITH_CONFETTI_KEY,
|
||||
AccountConfig::FORM_PREFILL_SIGNATURE_KEY,
|
||||
AccountConfig::ALLOW_TYPED_SIGNATURE].freeze
|
||||
|
||||
module_function
|
||||
|
||||
def call(submitter, keys = [])
|
||||
configs = submitter.submission.account.account_configs
|
||||
.where(key: DEFAULT_KEYS + keys)
|
||||
configs = submitter.submission.account.account_configs.where(key: DEFAULT_KEYS + keys)
|
||||
|
||||
completed_button = configs.find { |e| e.key == AccountConfig::FORM_COMPLETED_BUTTON_KEY }&.value || {}
|
||||
completed_message = configs.find { |e| e.key == AccountConfig::FORM_COMPLETED_MESSAGE_KEY }&.value || {}
|
||||
with_typed_signature = configs.find { |e| e.key == AccountConfig::ALLOW_TYPED_SIGNATURE }&.value != false
|
||||
with_confetti = configs.find { |e| e.key == AccountConfig::FORM_WITH_CONFETTI_KEY }&.value != false
|
||||
completed_button = find_safe_value(configs, AccountConfig::FORM_COMPLETED_BUTTON_KEY) || {}
|
||||
completed_message = find_safe_value(configs, AccountConfig::FORM_COMPLETED_MESSAGE_KEY) || {}
|
||||
with_typed_signature = find_safe_value(configs, AccountConfig::ALLOW_TYPED_SIGNATURE) != false
|
||||
with_confetti = find_safe_value(configs, AccountConfig::FORM_WITH_CONFETTI_KEY) != false
|
||||
prefill_signature = find_safe_value(configs, AccountConfig::FORM_PREFILL_SIGNATURE_KEY) != false
|
||||
|
||||
attrs = { completed_button:, with_typed_signature:, with_confetti:, completed_message: }
|
||||
attrs = { completed_button:, with_typed_signature:, with_confetti:, completed_message:, prefill_signature: }
|
||||
|
||||
keys.each do |key|
|
||||
attrs[key.to_sym] = configs.find { |e| e.key == key.to_s }&.value
|
||||
@@ -26,5 +27,9 @@ module Submitters
|
||||
|
||||
attrs
|
||||
end
|
||||
|
||||
def find_safe_value(configs, key)
|
||||
configs.find { |e| e.key == key }&.value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Submitters
|
||||
module MaybeAssignDefaultBrowserSignature
|
||||
SIGNED_UUID_PURPPOSE = 'signature'
|
||||
|
||||
module_function
|
||||
|
||||
def call(submitter, params, cookies = nil, attachments = [])
|
||||
if (value = params[:signature_src].presence || params[:signature].presence)
|
||||
find_or_create_signature_from_value(submitter, value, attachments)
|
||||
elsif params[:signed_signature_uuids].present?
|
||||
find_storage_signature(submitter, params[:signed_signature_uuids], attachments)
|
||||
elsif cookies
|
||||
find_session_signature(submitter, cookies, attachments)
|
||||
end
|
||||
end
|
||||
|
||||
def find_or_create_signature_from_value(submitter, value, attachments)
|
||||
_, attachment = Submitters::NormalizeValues.normalize_attachment_value(value,
|
||||
'signature',
|
||||
submitter.account,
|
||||
attachments,
|
||||
submitter)
|
||||
|
||||
attachment.record ||= submitter
|
||||
|
||||
attachment.save!
|
||||
|
||||
attachment
|
||||
end
|
||||
|
||||
def sign_signature_uuid(uuid)
|
||||
ApplicationRecord.signed_id_verifier.generate(uuid, purpose: SIGNED_UUID_PURPPOSE)
|
||||
end
|
||||
|
||||
def verify_signature_uuid(signed_uuid)
|
||||
ApplicationRecord.signed_id_verifier.verified(signed_uuid, purpose: SIGNED_UUID_PURPPOSE)
|
||||
end
|
||||
|
||||
def find_storage_signature(submitter, signed_uuids, attachments)
|
||||
signed_uuid = signed_uuids[submitter.email]
|
||||
|
||||
return if signed_uuid.blank?
|
||||
|
||||
uuid = verify_signature_uuid(signed_uuid)
|
||||
|
||||
return if uuid.blank?
|
||||
|
||||
find_signature_from_uuid(submitter, uuid, attachments)
|
||||
end
|
||||
|
||||
def find_session_signature(submitter, cookies, attachments)
|
||||
values =
|
||||
begin
|
||||
JSON.parse(cookies.encrypted[:signature_uuids].presence || '{}')
|
||||
rescue JSON::ParserError
|
||||
{}
|
||||
end
|
||||
|
||||
return if values.blank?
|
||||
|
||||
uuid = values[submitter.email]
|
||||
|
||||
return if uuid.blank?
|
||||
|
||||
find_signature_from_uuid(submitter, uuid, attachments)
|
||||
end
|
||||
|
||||
def find_signature_from_uuid(submitter, uuid, attachments)
|
||||
signature_attachment = ActiveStorage::Attachment.find_by(uuid:)
|
||||
|
||||
return unless signature_attachment
|
||||
|
||||
return if signature_attachment.record.email != submitter.email
|
||||
|
||||
existing_attachment = attachments.find do |a|
|
||||
a.blob_id == signature_attachment.blob_id && submitter.id == a.record_id
|
||||
end
|
||||
|
||||
return existing_attachment if existing_attachment
|
||||
|
||||
submitter.attachments_attachments.create_or_find_by!(blob_id: signature_attachment.blob_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
+5
-10
@@ -1,10 +1,10 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Submitters
|
||||
module MaybeAssignDefaultSignature
|
||||
module MaybeAssignDefaultEmailSignature
|
||||
module_function
|
||||
|
||||
def call(submitter, params, attachments_index)
|
||||
def call(submitter, params, attachments)
|
||||
return if params[:t].present? && params[:t] != SubmissionEvents.build_tracking_param(submitter, 'click_email')
|
||||
return if params[:t].blank? && !submitter.submission_events.exists?(event_type: :click_email)
|
||||
|
||||
@@ -12,18 +12,13 @@ module Submitters
|
||||
|
||||
return if signature_attachment.blank?
|
||||
|
||||
existing_attachment = attachments_index.values.find do |a|
|
||||
a.blob_id == signature_attachment.blob_id && submitter.id == signature_attachment.record_id
|
||||
existing_attachment = attachments.find do |a|
|
||||
a.blob_id == signature_attachment.blob_id && submitter.id == a.record_id
|
||||
end
|
||||
|
||||
return existing_attachment if existing_attachment
|
||||
|
||||
attachment =
|
||||
submitter.attachments_attachments.create_or_find_by!(blob_id: signature_attachment.blob_id)
|
||||
|
||||
attachments_index[attachment.uuid] = attachment
|
||||
|
||||
attachment
|
||||
submitter.attachments_attachments.create_or_find_by!(blob_id: signature_attachment.blob_id)
|
||||
end
|
||||
|
||||
def find_previous_signature(submitter)
|
||||
Reference in New Issue
Block a user