Sha256: 0908e2817b3748012c117522a4fbf9c095b84349f52daf04aa82466b7bb343bc

Contents?: true

Size: 1.68 KB

Versions: 12

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

module Decidim
  module Admin
    # A command with all the business logic to promote a managed user.
    #
    # Managed users can be promoted to standard users. It means they
    # will be invited to the application and will lose the managed flag
    # so the user cannot be impersonated anymore.
    class PromoteManagedUser < Decidim::Command
      # Public: Initializes the command.
      #
      # form         - A form object with the params.
      # user         - The user to promote
      # promoted_by  - The user performing the operation
      def initialize(form, user, promoted_by)
        @form = form
        @user = user
        @promoted_by = promoted_by
      end

      # Executes the command. Broadcasts these events:
      #
      # - :ok when everything is valid.
      # - :invalid if the form wasn't valid and we couldn't proceed.
      #
      # Returns nothing.
      def call
        return broadcast(:invalid) if form.invalid? || !user.managed? || email_already_exists?

        promote_user
        invite_user
        create_action_log

        broadcast(:ok)
      end

      attr_reader :form, :user, :promoted_by

      private

      def promote_user
        user.email = form.email.downcase
        user.skip_reconfirmation!
        user.save(validate: false)
      end

      def invite_user
        user.invite!(promoted_by)
      end

      def email_already_exists?
        Decidim::User.where(email: form.email.downcase).any?
      end

      def create_action_log
        Decidim.traceability.perform_action!(
          "promote",
          user,
          form.current_user,
          visibility: "admin-only"
        )
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
decidim-admin-0.27.9 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.27.8 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.27.7 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.27.6 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.27.5 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.27.4 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.27.3 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.27.2 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.27.1 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.27.0 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.27.0.rc2 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.27.0.rc1 app/commands/decidim/admin/promote_managed_user.rb