Class: Decidim::Verifications::RevokeByConditionAuthorizations

Inherits:
Command
  • Object
show all
Defined in:
app/commands/decidim/verifications/revoke_by_condition_authorizations.rb

Overview

A command to revoke authorizations with filter

Instance Method Summary collapse

Constructor Details

#initialize(organization, form) ⇒ RevokeByConditionAuthorizations

Public: Initializes the command.

organization - Organization object. current_user - The current user. form - A form object with the verification data to confirm it.



13
14
15
16
# File 'app/commands/decidim/verifications/revoke_by_condition_authorizations.rb', line 13

def initialize(organization, form)
  @organization = organization
  @form = form
end

Instance Method Details

#callObject

Executes the command. Broadcasts these events:

  • :ok when everything is valid.

  • :invalid if the handler was not valid and we could not proceed.

Returns nothing.



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
53
54
55
56
57
58
59
60
61
# File 'app/commands/decidim/verifications/revoke_by_condition_authorizations.rb', line 24

def call
  return broadcast(:invalid) unless @organization
  return broadcast(:invalid) unless @form.valid?

  # Check before date
  if @form.before_date.present?
    authorizations_to_revoke = if @form.impersonated_only?
                                 Decidim::Verifications::AuthorizationsBeforeDate.new(
                                   organization:,
                                   date: @form.before_date,
                                   granted: true,
                                   impersonated_only: @form.impersonated_only
                                 )
                               else
                                 Decidim::Verifications::AuthorizationsBeforeDate.new(
                                   organization:,
                                   date: @form.before_date,
                                   granted: true
                                 )
                               end

    auths = authorizations_to_revoke.query
    auths.find_each do |auth|
      Decidim.traceability.perform_action!(
        :destroy,
        auth,
        current_user
      ) do
        auth.destroy
      end
    end

    broadcast(:ok)

  else
    broadcast(:invalid)
  end
end