Sha256: eaa4d3ea9e18f88291b8fb0db6bb415d3abe34558abc4ba48477baaa1c5cd6d6

Contents?: true

Size: 1.62 KB

Versions: 44

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 < Rectify::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

44 entries across 44 versions & 1 rubygems

Version Path
decidim-initiatives-0.26.10 app/commands/decidim/initiatives/validate_mobile_phone.rb
decidim-initiatives-0.26.9 app/commands/decidim/initiatives/validate_mobile_phone.rb
decidim-initiatives-0.26.8 app/commands/decidim/initiatives/validate_mobile_phone.rb
decidim-initiatives-0.26.7 app/commands/decidim/initiatives/validate_mobile_phone.rb
decidim-initiatives-0.26.5 app/commands/decidim/initiatives/validate_mobile_phone.rb
decidim-initiatives-0.26.4 app/commands/decidim/initiatives/validate_mobile_phone.rb
decidim-initiatives-0.26.3 app/commands/decidim/initiatives/validate_mobile_phone.rb
decidim-initiatives-0.26.2 app/commands/decidim/initiatives/validate_mobile_phone.rb
decidim-initiatives-0.26.1 app/commands/decidim/initiatives/validate_mobile_phone.rb
decidim-initiatives-0.26.0 app/commands/decidim/initiatives/validate_mobile_phone.rb
decidim-initiatives-0.26.0.rc2 app/commands/decidim/initiatives/validate_mobile_phone.rb
decidim-initiatives-0.26.0.rc1 app/commands/decidim/initiatives/validate_mobile_phone.rb
decidim-initiatives-0.25.2 app/commands/decidim/initiatives/validate_mobile_phone.rb
decidim-initiatives-0.25.1 app/commands/decidim/initiatives/validate_mobile_phone.rb
decidim-initiatives-0.25.0 app/commands/decidim/initiatives/validate_mobile_phone.rb
decidim-initiatives-0.25.0.rc4 app/commands/decidim/initiatives/validate_mobile_phone.rb
decidim-initiatives-0.25.0.rc3 app/commands/decidim/initiatives/validate_mobile_phone.rb
decidim-initiatives-0.25.0.rc2 app/commands/decidim/initiatives/validate_mobile_phone.rb
decidim-initiatives-0.25.0.rc1 app/commands/decidim/initiatives/validate_mobile_phone.rb
decidim-initiatives-0.24.3 app/commands/decidim/initiatives/validate_mobile_phone.rb