Class: Decidim::ActionDelegator::Verifications::DelegationsVerifierForm

Inherits:
AuthorizationHandler
  • Object
show all
Defined in:
app/forms/decidim/action_delegator/verifications/delegations_verifier_form.rb

Overview

A form object to be used when public users want to get verified using their phone.

Instance Method Summary collapse

Instance Method Details

#handler_nameObject



21
22
23
# File 'app/forms/decidim/action_delegator/verifications/delegations_verifier_form.rb', line 21

def handler_name
  "delegations_verifier"
end

#metadataObject



42
43
44
45
46
# File 'app/forms/decidim/action_delegator/verifications/delegations_verifier_form.rb', line 42

def 
  {
    phone: phone
  }
end

#participantObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/forms/decidim/action_delegator/verifications/delegations_verifier_form.rb', line 66

def participant
  return unless setting

  @participant ||= begin
    params = {}
    params[:email] = email if setting.email_required?
    if setting.phone_required?
      if phone.blank?
        @participant = setting.participants.none
      else
        params[:phone] = phone
        params[:phone] = phone_prefixes.map { |prefix| "#{prefix}#{phone}" }
        params[:phone] += phone_prefixes.map { |prefix| phone.delete_prefix(prefix).to_s }
      end
    end

    setting.participants.find_by(params)
  end
end

#participantsObject



62
63
64
# File 'app/forms/decidim/action_delegator/verifications/delegations_verifier_form.rb', line 62

def participants
  @participants ||= Decidim::ActionDelegator::Participant.where(setting: setting)
end

#phoneObject

When there’s a phone number, sanitize it allowing only numbers and +.



35
36
37
38
39
40
# File 'app/forms/decidim/action_delegator/verifications/delegations_verifier_form.rb', line 35

def phone
  return find_phone if setting&.verify_with_both?
  return unless super

  super.gsub(/[^+0-9]/, "")
end

#settingObject

currently, we rely on the last setting. This could be improved by allowing the user to select the setting (or related phone).



58
59
60
# File 'app/forms/decidim/action_delegator/verifications/delegations_verifier_form.rb', line 58

def setting
  @setting ||= context[:setting]
end

#unique_idObject



25
26
27
28
29
# File 'app/forms/decidim/action_delegator/verifications/delegations_verifier_form.rb', line 25

def unique_id
  Digest::MD5.hexdigest(
    "#{setting&.phone_required? ? phone : email}-#{setting&.organization&.id}-#{Digest::MD5.hexdigest(Rails.application.secrets.secret_key_base)}"
  )
end

#verification_metadataObject

The verification metadata to validate in the next step.



49
50
51
52
53
54
# File 'app/forms/decidim/action_delegator/verifications/delegations_verifier_form.rb', line 49

def 
  {
    verification_code: verification_code,
    code_sent_at: Time.current
  }
end