Sha256: 1da2067e5af6c1a06e062acced78be5c55599c8cb9fb15b9d80f910aaa9263bf

Contents?: true

Size: 1.51 KB

Versions: 46

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module Decidim
  # A command with all the business logic to demote an admin. This means
  # removing their admin righta dn converting them to a basic member. It's the
  # coutnerpart of `PromoteMembership`.
  class DemoteMembership < Rectify::Command
    # Public: Initializes the command.
    #
    # membership - the UserGroupMembership to be demoted.
    def initialize(membership, user_group)
      @membership = membership
      @user_group = user_group
    end

    # Executes the command. Broadcasts these events:
    #
    # - :ok when everything is valid.
    # - :invalid if we couldn't proceed.
    #
    # Returns nothing.
    def call
      return broadcast(:invalid) if membership.blank?
      return broadcast(:invalid) if membership.role != "admin"
      return broadcast(:invalid) if membership.user_group != user_group

      transaction do
        demote_membership
        send_notification
      end

      broadcast(:ok)
    end

    private

    attr_reader :membership, :user_group

    def demote_membership
      membership.role = :member
      membership.save!
    end

    def send_notification
      Decidim::EventsManager.publish(
        event: "decidim.events.groups.demoted_membership",
        event_class: DemotedMembershipEvent,
        resource: membership.user_group,
        affected_users: [membership.user],
        extra: {
          user_group_name: membership.user_group.name,
          user_group_nickname: membership.user_group.nickname
        }
      )
    end
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

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