Sha256: dfe03dd860a715c30eb29eb0e5fb0e26d2ac9d6855c237148dd4765138348c65

Contents?: true

Size: 1.51 KB

Versions: 7

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module Decidim
  module Admin
    # This command gets called when permissions for a component are updated
    # in the admin panel.
    class UpdateComponentPermissions < Rectify::Command
      attr_reader :form, :component

      # Public: Initializes the command.
      #
      # form    - The form from which the data in this component comes from.
      # component - The component to update.
      def initialize(form, component)
        @form = form
        @component = component
      end

      # Public: Sets the permissions for a component.
      #
      # Broadcasts :ok if created, :invalid otherwise.
      def call
        return broadcast(:invalid) unless @form.valid?

        transaction do
          update_permissions
          run_hooks
        end

        broadcast(:ok)
      end

      private

      def configured_permissions
        @form.permissions.select do |_action, permission|
          permission.authorization_handler_name.present?
        end
      end

      def update_permissions
        permissions = configured_permissions.inject({}) do |result, (key, value)|
          serialized = {
            "authorization_handler_name" => value.authorization_handler_name,
            "options" => value.options
          }

          result.update(key => serialized)
        end

        @component.update!(
          permissions: permissions
        )
      end

      def run_hooks
        @component.manifest.run_hooks(:permission_update, @component)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
decidim-admin-0.13.1 app/commands/decidim/admin/update_component_permissions.rb
decidim-admin-0.12.2 app/commands/decidim/admin/update_component_permissions.rb
decidim-admin-0.13.0 app/commands/decidim/admin/update_component_permissions.rb
decidim-admin-0.12.1 app/commands/decidim/admin/update_component_permissions.rb
decidim-admin-0.13.0.pre1 app/commands/decidim/admin/update_component_permissions.rb
decidim-admin-0.12.0 app/commands/decidim/admin/update_component_permissions.rb
decidim-admin-0.12.0.pre app/commands/decidim/admin/update_component_permissions.rb