Sha256: 21d6afce4f80113ee73c92ffaf1c75e4e364ae7582e9c7d4c4ac2ebb0570ab4c

Contents?: true

Size: 1.46 KB

Versions: 76

Compression:

Stored size: 1.46 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 < Rectify::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

        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
    end
  end
end

Version data entries

76 entries across 76 versions & 2 rubygems

Version Path
decidim-admin-0.16.0 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.15.2 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.15.1 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.15.0 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.14.4 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.14.3 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.14.2 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.14.1 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.13.1 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.12.2 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.13.0 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.12.1 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.13.0.pre1 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.12.0 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.11.2 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.12.0.pre app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.11.1 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.11.0.pre1 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.10.1 app/commands/decidim/admin/promote_managed_user.rb
decidim-admin-0.10.0 app/commands/decidim/admin/promote_managed_user.rb