Sha256: 4d236fd062c0acd27436748a5dd2d40744f085b89775797f063a7cfbdb00f4c6

Contents?: true

Size: 1.72 KB

Versions: 9

Compression:

Stored size: 1.72 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, organization)
        @handler = handler
        @organization = organization
      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, organization: @organization)
        )
      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

9 entries across 9 versions & 1 rubygems

Version Path
decidim-verifications-0.26.10 app/commands/decidim/verifications/authorize_user.rb
decidim-verifications-0.26.9 app/commands/decidim/verifications/authorize_user.rb
decidim-verifications-0.26.8 app/commands/decidim/verifications/authorize_user.rb
decidim-verifications-0.26.7 app/commands/decidim/verifications/authorize_user.rb
decidim-verifications-0.26.5 app/commands/decidim/verifications/authorize_user.rb
decidim-verifications-0.26.4 app/commands/decidim/verifications/authorize_user.rb
decidim-verifications-0.26.3 app/commands/decidim/verifications/authorize_user.rb
decidim-verifications-0.26.2 app/commands/decidim/verifications/authorize_user.rb
decidim-verifications-0.26.1 app/commands/decidim/verifications/authorize_user.rb