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

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

Overview

This verifier checks if there is some setting in which the participant is required to verify it’s phone (the first active setting will be used for that). If no setting requires phone verification, it will check if there is some setting in which the participant is required to verify it’s email. If no setting requires email verification, the user won’t be able to proceed. If there are multiple active settings, the user will be verified for the first one

Note that the ActionAuthorizer associated with this handler will check the current status of the settings and delegations regardless of this verification metadata

Instance Method Summary collapse

Instance Method Details

#active_settingsObject

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



73
74
75
# File 'app/forms/decidim/action_delegator/verifications/delegations_verifier_form.rb', line 73

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

#handler_nameObject



29
30
31
# File 'app/forms/decidim/action_delegator/verifications/delegations_verifier_form.rb', line 29

def handler_name
  "delegations_verifier"
end

#metadataObject



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

def 
  {
    phone:,
    setting_ids:
  }
end

#participantObject

find the participant in any of the active settings If phone is required, just find the first participant and validate the phone if not, find by email in any of the active settings



80
81
82
# File 'app/forms/decidim/action_delegator/verifications/delegations_verifier_form.rb', line 80

def participant
  valid_participants&.first
end

#phoneObject

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



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

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

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

#settingObject

find the first setting where phone is required or, if not, the first setting where email is required This works because the email is unique per user so it does not matter which setting we use to find the participant If the setting requires phone, only one active setting with phone verification is allowed to exist at a time



106
107
108
# File 'app/forms/decidim/action_delegator/verifications/delegations_verifier_form.rb', line 106

def setting
  @setting ||= active_settings&.phone_required&.first || active_settings&.email_required&.first
end

#setting_idsObject



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

def setting_ids
  return [] unless current_user

  valid_participants&.map(&:decidim_action_delegator_setting_id)&.uniq || []
end

#unique_idObject



33
34
35
36
37
# File 'app/forms/decidim/action_delegator/verifications/delegations_verifier_form.rb', line 33

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

#valid_participantsObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/forms/decidim/action_delegator/verifications/delegations_verifier_form.rb', line 84

def valid_participants
  return [] unless setting

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

    setting.participants.where(params)
  end
end

#verification_metadataObject

The verification metadata to validate in the next step.



64
65
66
67
68
69
# File 'app/forms/decidim/action_delegator/verifications/delegations_verifier_form.rb', line 64

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