Sha256: 6f927ac05348a5fb4dfb657265cafa2c12354545d81e1acc20b6766e09a44fbc

Contents?: true

Size: 1.64 KB

Versions: 16

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

module Decidim
  module Verifications
    # A command to authorize a user with an authorization handler.
    class AuthorizeUser < Rectify::Command
      # Public: Initializes the command.
      #
      # handler - An AuthorizationHandler object.
      def initialize(handler)
        @handler = handler
      end

      # 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.
      def call
        if handler.invalid?
          conflict = create_verification_conflict
          notify_admins(conflict) if conflict.present?

          return broadcast(:invalid)
        end

        Authorization.create_or_update_from(handler)

        broadcast(:ok)
      end

      private

      attr_reader :handler

      def notify_admins(conflict)
        Decidim::EventsManager.publish(
          event: "decidim.events.verifications.managed_user_error_event",
          event_class: Decidim::Verifications::ManagedUserErrorEvent,
          resource: conflict,
          affected_users: Decidim::User.where(admin: true)
        )
      end

      def create_verification_conflict
        authorization = Decidim::Authorization.find_by(unique_id: handler.unique_id)
        return if authorization.blank?

        conflict = Decidim::Verifications::Conflict.find_or_initialize_by(
          current_user: handler.user,
          managed_user: authorization.user,
          unique_id: handler.unique_id
        )

        conflict.update(times: conflict.times + 1)

        conflict
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
decidim-verifications-0.26.0 app/commands/decidim/verifications/authorize_user.rb
decidim-verifications-0.26.0.rc2 app/commands/decidim/verifications/authorize_user.rb
decidim-verifications-0.26.0.rc1 app/commands/decidim/verifications/authorize_user.rb
decidim-verifications-0.25.2 app/commands/decidim/verifications/authorize_user.rb
decidim-verifications-0.25.1 app/commands/decidim/verifications/authorize_user.rb
decidim-verifications-0.25.0 app/commands/decidim/verifications/authorize_user.rb
decidim-verifications-0.25.0.rc4 app/commands/decidim/verifications/authorize_user.rb
decidim-verifications-0.25.0.rc3 app/commands/decidim/verifications/authorize_user.rb
decidim-verifications-0.25.0.rc2 app/commands/decidim/verifications/authorize_user.rb
decidim-verifications-0.25.0.rc1 app/commands/decidim/verifications/authorize_user.rb
decidim-verifications-0.24.3 app/commands/decidim/verifications/authorize_user.rb
decidim-verifications-0.24.2 app/commands/decidim/verifications/authorize_user.rb
decidim-verifications-0.24.1 app/commands/decidim/verifications/authorize_user.rb
decidim-verifications-0.24.0 app/commands/decidim/verifications/authorize_user.rb
decidim-verifications-0.24.0.rc2 app/commands/decidim/verifications/authorize_user.rb
decidim-verifications-0.24.0.rc1 app/commands/decidim/verifications/authorize_user.rb