Sha256: 06a932dd69838c7391930176e73040c7e9f77e3e20667b618bc98eb194b5ee90

Contents?: true

Size: 1.93 KB

Versions: 6

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

module Decidim
  module Plans
    class Permissions < Decidim::DefaultPermissions
      def permissions
        return permission_action unless user

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

        if permission_action.subject == :plan
          apply_plan_permissions(permission_action)
        else
          permission_action
        end

        permission_action
      end

      private

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

      def apply_plan_permissions(permission_action)
        case permission_action.action
        when :create
          can_create_plan?
        when :edit
          can_edit_plan?
        when :withdraw
          can_withdraw_plan?
        when :publish
          can_publish_plan?
        when :close
          can_close_plan?
        when :request_access
          can_request_access_plan?
        end
      end

      def can_create_plan?
        toggle_allow(authorized?(:create) && current_settings&.creation_enabled?)
      end

      def can_edit_plan?
        toggle_allow(plan.open? && !plan.closed? && plan.editable_by?(user))
      end

      def can_withdraw_plan?
        toggle_allow(plan && plan.withdrawable_by?(user))
      end

      def can_publish_plan?
        toggle_allow(plan.open? && plan.editable_by?(user))
      end

      def can_close_plan?
        toggle_allow(plan && plan.created_by?(user))
      end

      def can_request_access_plan?
        return toggle_allow(false) unless plan.open?
        return toggle_allow(false) if plan.editable_by?(user)
        return toggle_allow(false) if plan.requesters.include? user
        toggle_allow(plan && !plan.editable_by?(user))
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-plans-0.16.6 app/permissions/decidim/plans/permissions.rb
decidim-plans-0.16.5 app/permissions/decidim/plans/permissions.rb
decidim-plans-0.16.4 app/permissions/decidim/plans/permissions.rb
decidim-plans-0.16.3 app/permissions/decidim/plans/permissions.rb
decidim-plans-0.16.2 app/permissions/decidim/plans/permissions.rb
decidim-plans-0.16.1 app/permissions/decidim/plans/permissions.rb