Sha256: 953fccfa003835901bd152e000801012c0eed72330b69c06cf6e115ede38f150

Contents?: true

Size: 1.19 KB

Versions: 18

Compression:

Stored size: 1.19 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
      @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
      user.save!
    end

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

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
decidim-core-0.6.8 app/commands/decidim/invite_user.rb
decidim-0.6.8 decidim-core/app/commands/decidim/invite_user.rb
decidim-core-0.6.7 app/commands/decidim/invite_user.rb
decidim-0.6.7 decidim-core/app/commands/decidim/invite_user.rb
decidim-core-0.6.6 app/commands/decidim/invite_user.rb
decidim-0.6.6 decidim-core/app/commands/decidim/invite_user.rb
decidim-core-0.6.5 app/commands/decidim/invite_user.rb
decidim-0.6.5 decidim-core/app/commands/decidim/invite_user.rb
decidim-core-0.6.4 app/commands/decidim/invite_user.rb
decidim-0.6.4 decidim-core/app/commands/decidim/invite_user.rb
decidim-core-0.6.3 app/commands/decidim/invite_user.rb
decidim-0.6.3 decidim-core/app/commands/decidim/invite_user.rb
decidim-core-0.6.2 app/commands/decidim/invite_user.rb
decidim-0.6.2 decidim-core/app/commands/decidim/invite_user.rb
decidim-core-0.6.1 app/commands/decidim/invite_user.rb
decidim-0.6.1 decidim-core/app/commands/decidim/invite_user.rb
decidim-core-0.6.0 app/commands/decidim/invite_user.rb
decidim-0.6.0 decidim-core/app/commands/decidim/invite_user.rb