Class: Decidim::Verifications::RevokeByConditionAuthorizations

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

Overview

A command to revoke authorizations with filter

Instance Method Summary collapse

Methods inherited from Command

call, #evaluate, #method_missing, #respond_to_missing?, #transaction

Constructor Details

#initialize(organization, current_user, 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.


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

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Decidim::Command

Instance Method Details

#callObject

Executes the command. Broadcasts these events:

  • :ok when everything is valid.

  • :invalid if the handler wasn't valid and we couldn't 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 'decidim-verifications/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: organization,
                                   date: @form.before_date,
                                   granted: true,
                                   impersonated_only: @form.impersonated_only
                                 )
                               else
                                 Decidim::Verifications::AuthorizationsBeforeDate.new(
                                   organization: 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