Sha256: 2d98ba7c3a212057c0f28f30788c9271b31b5cb127a2b982d13f914591a2fe81

Contents?: true

Size: 1.52 KB

Versions: 7

Compression:

Stored size: 1.52 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 is the
  # coutnerpart of `PromoteMembership`.
  class DemoteMembership < Decidim::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 could not 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

7 entries across 7 versions & 1 rubygems

Version Path
decidim-core-0.28.4 app/commands/decidim/demote_membership.rb
decidim-core-0.28.3 app/commands/decidim/demote_membership.rb
decidim-core-0.28.2 app/commands/decidim/demote_membership.rb
decidim-core-0.28.1 app/commands/decidim/demote_membership.rb
decidim-core-0.28.0 app/commands/decidim/demote_membership.rb
decidim-core-0.28.0.rc5 app/commands/decidim/demote_membership.rb
decidim-core-0.28.0.rc4 app/commands/decidim/demote_membership.rb