Class: Decidim::VerifyWoRegistration::DoVerifyWoRegistration

Inherits:
Command
  • Object
show all
Defined in:
app/commands/decidim/verify_wo_registration/do_verify_wo_registration.rb

Overview

A command with all the business logic to verify and impersonate (managed user).

Instance Method Summary collapse

Constructor Details

#initialize(form) ⇒ DoVerifyWoRegistration

Public: Initializes the command.

form - The form with the authorization info



10
11
12
# File 'app/commands/decidim/verify_wo_registration/do_verify_wo_registration.rb', line 10

def initialize(form)
  @form = form
end

Instance Method Details

#callObject

Executes the command. Broadcasts this events:

  • :ok when everything is valid.

  • :invalid if the form is not valid.

Returns nothing.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/commands/decidim/verify_wo_registration/do_verify_wo_registration.rb', line 20

def call
  if authorizations_exists?
    if existing_registered_user?
      # ignore form data, make the participant use the already existing user
      broadcast(:use_registered_user)
    elsif existing_impersonated_user?
      # we can not reuse existing authorization because it will raise "A participant is already authorized with the same data." on @form.valid?
      # as it is an impersonated user, we can safely destroy it and perform the verification process again
      transaction do
        @user = @authorization.user
        user.skip_confirmation!
        destroy_authorization
        if @form.valid?
          authorize_user
          broadcast(:ok, user)
        else
          broadcast(:invalid)
        end
      end
    else
      Rails.logger.warn('This should never happen :(')
      broadcast(:invalid)
    end
  elsif @form.valid?
    transaction do
      create_user
      authorize_user
    end
    broadcast(:ok, user)
  else
    broadcast(:invalid)
  end
end