Sha256: 37e450825cc568fdc2688affd0c660c2976b023b0d183882cb3f9213be2e5674

Contents?: true

Size: 1.44 KB

Versions: 54

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

module Decidim
  module Debates
    class Permissions < Decidim::DefaultPermissions
      def permissions
        # Delegate the admin permission checks to the admin permissions class
        return Decidim::Debates::Admin::Permissions.new(user, permission_action, context).permissions if permission_action.scope == :admin
        return permission_action if permission_action.scope != :public

        return permission_action if permission_action.subject != :debate

        case permission_action.action
        when :create
          toggle_allow(can_create_debate?)
        when :report
          allow!
        when :edit
          can_edit_debate?
        when :endorse
          can_endorse_debate?
        when :close
          can_close_debate?
        end

        permission_action
      end

      private

      def can_create_debate?
        authorized?(:create) &&
          current_settings&.creation_enabled? && component.participatory_space.can_participate?(user)
      end

      def can_edit_debate?
        return allow! if debate&.editable_by?(user)

        disallow!
      end

      def can_close_debate?
        return allow! if debate&.closeable_by?(user)

        disallow!
      end

      def can_endorse_debate?
        return disallow! if debate.closed?

        allow!
      end

      def debate
        @debate ||= context.fetch(:debate, nil) || context.fetch(:resource, nil)
      end
    end
  end
end

Version data entries

54 entries across 54 versions & 1 rubygems

Version Path
decidim-debates-0.29.1 app/permissions/decidim/debates/permissions.rb
decidim-debates-0.28.4 app/permissions/decidim/debates/permissions.rb
decidim-debates-0.29.0 app/permissions/decidim/debates/permissions.rb
decidim-debates-0.28.3 app/permissions/decidim/debates/permissions.rb
decidim-debates-0.29.0.rc4 app/permissions/decidim/debates/permissions.rb
decidim-debates-0.29.0.rc3 app/permissions/decidim/debates/permissions.rb
decidim-debates-0.29.0.rc2 app/permissions/decidim/debates/permissions.rb
decidim-debates-0.29.0.rc1 app/permissions/decidim/debates/permissions.rb
decidim-debates-0.28.2 app/permissions/decidim/debates/permissions.rb
decidim-debates-0.28.1 app/permissions/decidim/debates/permissions.rb
decidim-debates-0.26.10 app/permissions/decidim/debates/permissions.rb
decidim-debates-0.26.9 app/permissions/decidim/debates/permissions.rb
decidim-debates-0.28.0 app/permissions/decidim/debates/permissions.rb
decidim-debates-0.27.5 app/permissions/decidim/debates/permissions.rb
decidim-debates-0.28.0.rc5 app/permissions/decidim/debates/permissions.rb
decidim-debates-0.28.0.rc4 app/permissions/decidim/debates/permissions.rb
decidim-debates-0.26.8 app/permissions/decidim/debates/permissions.rb
decidim-debates-0.27.4 app/permissions/decidim/debates/permissions.rb
decidim-debates-0.27.3 app/permissions/decidim/debates/permissions.rb
decidim-debates-0.26.7 app/permissions/decidim/debates/permissions.rb