Sha256: 5639db97fbfca1f7f182514dc04ed43bf5e42d462e3ebbd82ca0f725fb7ec9de
Contents?: true
Size: 1.62 KB
Versions: 12
Compression:
Stored size: 1.62 KB
Contents
# frozen_string_literal: true module Decidim module Initiatives # Command to check if mobile phone has an authorization and # deliver sms code class ValidateMobilePhone < Decidim::Command # Public: Initializes the command. # # form - A MobilePhoneForm. # user - The user which mobile phone must be validated. def initialize(form, user) @form = form @user = user end # Executes the command. Broadcasts these events: # # - :ok when everything is valid. Returns the verification metadata of # the form. # - :invalid if the user doesn't have an authorization for sms in ok # status or the phone number associated with its # authorization doesn't match the form number. def call return broadcast(:invalid) unless authorized? && phone_match? generate_code broadcast(:ok, @verification_metadata) end private def generate_code @verification_metadata = @form.verification_metadata end def authorizer return unless authorization Decidim::Verifications::Adapter.from_element(authorization_name).authorize(authorization, {}, nil, nil) end def authorization @authorization ||= Verifications::Authorizations.new(organization: @user.organization, user: @user, name: authorization_name).first end def authorization_name "sms" end def authorized? authorizer&.first == :ok end def phone_match? authorization.unique_id == @form.unique_id end end end end
Version data entries
12 entries across 12 versions & 1 rubygems