Sha256: c83783a9027d58c7d68c977715becbcbaa4b7961735a3a14f854e3701a9c0dec

Contents?: true

Size: 1.36 KB

Versions: 46

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module Decidim
  # A command with the business logic to invite a user to an organization.
  class InviteUser < Rectify::Command
    # Public: Initializes the command.
    #
    # form - A form object with the params.
    def initialize(form)
      @form = form
    end

    def call
      return broadcast(:invalid) if form.invalid?

      if user.present?
        update_user
      else
        invite_user
      end

      broadcast(:ok, user)
    end

    private

    attr_reader :form

    def user
      # rubocop:disable Rails/FindBy
      @user ||= Decidim::User.where(organization: form.organization).where(email: form.email.downcase).first
      # rubocop:enable Rails/FindBy
    end

    def update_user
      user.admin = form.role == "admin"
      user.roles << form.role if form.role != "admin"
      user.roles = user.roles.uniq.compact
      user.save!
    end

    def invite_user
      @user = Decidim::User.new(
        name: form.name,
        email: form.email.downcase,
        nickname: UserBaseEntity.nicknamize(form.name, organization: form.organization),
        organization: form.organization,
        admin: form.role == "admin",
        roles: form.role == "admin" ? [] : [form.role].compact
      )
      @user.invite!(
        form.invited_by,
        invitation_instructions: form.invitation_instructions
      )
    end
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
decidim-core-0.26.10 app/commands/decidim/invite_user.rb
decidim-core-0.26.9 app/commands/decidim/invite_user.rb
decidim-core-0.26.8 app/commands/decidim/invite_user.rb
decidim-core-0.26.7 app/commands/decidim/invite_user.rb
decidim-core-0.26.5 app/commands/decidim/invite_user.rb
decidim-core-0.26.4 app/commands/decidim/invite_user.rb
decidim-core-0.26.3 app/commands/decidim/invite_user.rb
decidim-core-0.26.2 app/commands/decidim/invite_user.rb
decidim-core-0.26.1 app/commands/decidim/invite_user.rb
decidim-core-0.26.0 app/commands/decidim/invite_user.rb
decidim-core-0.26.0.rc2 app/commands/decidim/invite_user.rb
decidim-core-0.26.0.rc1 app/commands/decidim/invite_user.rb
decidim-core-0.25.2 app/commands/decidim/invite_user.rb
decidim-core-0.25.1 app/commands/decidim/invite_user.rb
decidim-core-0.25.0 app/commands/decidim/invite_user.rb
decidim-core-0.25.0.rc4 app/commands/decidim/invite_user.rb
decidim-core-0.25.0.rc3 app/commands/decidim/invite_user.rb
decidim-core-0.25.0.rc2 app/commands/decidim/invite_user.rb
decidim-core-0.25.0.rc1 app/commands/decidim/invite_user.rb
decidim-core-0.24.3 app/commands/decidim/invite_user.rb