Sha256: eaea6d606f136e66cd51a3abe1ad71b62c87430ac83cb1e4a8aa247e01675759

Contents?: true

Size: 1.49 KB

Versions: 16

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module Decidim
  module Consultations
    class Permissions < Decidim::DefaultPermissions
      def permissions
        allowed_public_anonymous_action?

        permission_action unless user
        allowed_public_action?

        permission_action unless permission_action.scope == :admin
        return Decidim::Consultations::Admin::Permissions.new(user, permission_action, context).permissions if permission_action.scope == :admin

        permission_action
      end

      private

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

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

      def allowed_public_anonymous_action?
        return unless permission_action.action == :read
        return unless permission_action.scope == :public

        case permission_action.subject
        when :consultation_list
          allow!
        when :consultation
          toggle_allow(consultation.published? || user&.admin?)
        when :question
          toggle_allow(question.published? || user&.admin?)
        end
      end

      def allowed_public_action?
        return unless permission_action.scope == :public
        return unless permission_action.subject == :question

        case permission_action.action
        when :vote
          toggle_allow(question.can_be_voted_by?(user))
        when :unvote
          toggle_allow(question.can_be_unvoted_by?(user))
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
decidim-consultations-0.16.1 app/permissions/decidim/consultations/permissions.rb
decidim-consultations-0.16.0 app/permissions/decidim/consultations/permissions.rb
decidim-consultations-0.15.2 app/permissions/decidim/consultations/permissions.rb
decidim-consultations-0.15.1 app/permissions/decidim/consultations/permissions.rb
decidim-consultations-0.15.0 app/permissions/decidim/consultations/permissions.rb
decidim-consultations-0.14.4 app/permissions/decidim/consultations/permissions.rb
decidim-consultations-0.14.3 app/permissions/decidim/consultations/permissions.rb
decidim-consultations-0.14.2 app/permissions/decidim/consultations/permissions.rb
decidim-consultations-0.14.1 app/permissions/decidim/consultations/permissions.rb
decidim-consultations-0.13.1 app/permissions/decidim/consultations/permissions.rb
decidim-consultations-0.12.2 app/permissions/decidim/consultations/permissions.rb
decidim-consultations-0.13.0 app/permissions/decidim/consultations/permissions.rb
decidim-consultations-0.12.1 app/permissions/decidim/consultations/permissions.rb
decidim-consultations-0.13.0.pre1 app/permissions/decidim/consultations/permissions.rb
decidim-consultations-0.12.0 app/permissions/decidim/consultations/permissions.rb
decidim-consultations-0.12.0.pre app/permissions/decidim/consultations/permissions.rb