Sha256: 64e59a83ee7c3450ffe9ad1691802477c94239664c087725ffad7fc6c65f93d0

Contents?: true

Size: 1.61 KB

Versions: 9

Compression:

Stored size: 1.61 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
      delegate :current_user, to: :form
      # Public: Initializes the command.
      #
      # form         - A form object with the params.
      # user         - The user to promote
      def initialize(form, user)
        @form = form
        @user = user
      end

      # Executes the command. Broadcasts these events:
      #
      # - :ok when everything is valid.
      # - :invalid if the form was not valid and we could not 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

      private

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

      def invite_user
        user.invite!(current_user)
      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

9 entries across 9 versions & 1 rubygems

Version Path
decidim-admin-0.30.0.rc2 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.30.0.rc1 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.29.2 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.29.1 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.29.0 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.29.0.rc4 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.29.0.rc3 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.29.0.rc2 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.29.0.rc1 app/commands/decidim/admin/promote_managed_user.rb