Sha256: 287bef7d0107a69f6019b6b427cbbaa6d81086e26cb6a5eaf2700d75bcaa7acc

Contents?: true

Size: 1.29 KB

Versions: 25

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module Decidim
  # A command with the business logic to invite a user to an organization.
  class InviteUser < Decidim::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
      @user ||= Decidim::User.where(organization: form.organization).where(email: form.email.downcase).first
    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

25 entries across 25 versions & 1 rubygems

Version Path
decidim-core-0.29.1 app/commands/decidim/invite_user.rb
decidim-core-0.28.4 app/commands/decidim/invite_user.rb
decidim-core-0.27.9 app/commands/decidim/invite_user.rb
decidim-core-0.29.0 app/commands/decidim/invite_user.rb
decidim-core-0.28.3 app/commands/decidim/invite_user.rb
decidim-core-0.27.8 app/commands/decidim/invite_user.rb
decidim-core-0.29.0.rc4 app/commands/decidim/invite_user.rb
decidim-core-0.29.0.rc3 app/commands/decidim/invite_user.rb
decidim-core-0.29.0.rc2 app/commands/decidim/invite_user.rb
decidim-core-0.29.0.rc1 app/commands/decidim/invite_user.rb
decidim-core-0.28.2 app/commands/decidim/invite_user.rb
decidim-core-0.27.7 app/commands/decidim/invite_user.rb
decidim-core-0.28.1 app/commands/decidim/invite_user.rb
decidim-core-0.27.6 app/commands/decidim/invite_user.rb
decidim-core-0.28.0 app/commands/decidim/invite_user.rb
decidim-core-0.27.5 app/commands/decidim/invite_user.rb
decidim-core-0.28.0.rc5 app/commands/decidim/invite_user.rb
decidim-core-0.28.0.rc4 app/commands/decidim/invite_user.rb
decidim-core-0.27.4 app/commands/decidim/invite_user.rb
decidim-core-0.27.3 app/commands/decidim/invite_user.rb