Sha256: 3683a492125614ea4480c3c26c672d5cc48d7c14df92882a0d4da3b2ab93959b

Contents?: true

Size: 1.35 KB

Versions: 5

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

module Decidim
  module Admin
    # A command with all the business logic to create a new managed user in the
    # admin panel.
    class CreateManagedUser < Rectify::Command
      # Public: Initializes the command.
      #
      # form - A form object with the params.
      def initialize(form)
        @form = form
      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?

        transaction do
          create_managed_user
          raise ActiveRecord::Rollback unless authorized_user?
        end

        broadcast(:ok)
      end

      private

      attr_reader :form, :user

      def create_managed_user
        @user = Decidim::User.create!(
          name: form.name,
          organization: form.current_organization,
          admin: false,
          managed: true,
          tos_agreement: true
        )
      end

      def authorized_user?
        form.authorization.user = @user
        Verifications::AuthorizeUser.call(form.authorization) do
          on(:ok) do
            return true
          end
          on(:invalid) do
            return false
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
decidim-admin-0.8.4 app/commands/decidim/admin/create_managed_user.rb
decidim-admin-0.8.3 app/commands/decidim/admin/create_managed_user.rb
decidim-admin-0.8.2 app/commands/decidim/admin/create_managed_user.rb
decidim-admin-0.8.1 app/commands/decidim/admin/create_managed_user.rb
decidim-admin-0.8.0 app/commands/decidim/admin/create_managed_user.rb