Sha256: b8e532ef9434fc333f9811ccc55b54a24dddc0cd580385cc7646df4d1bcaf7a9

Contents?: true

Size: 1.37 KB

Versions: 18

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module Decidim
  module Accountability
    module Admin
      class Permissions < Decidim::DefaultPermissions
        def permissions
          return permission_action if permission_action.scope != :admin

          permission_action.allow! if can_perform_actions_on?(:result, result)
          permission_action.allow! if can_perform_actions_on?(:status, status)
          permission_action.allow! if can_perform_actions_on?(:timeline_entry, timeline_entry)

          permission_action
        end

        private

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

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

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

        def can_perform_actions_on?(subject, resource)
          return unless permission_action.subject == subject
          return false if can_create_grandchildren_results?

          case permission_action.action
          when :create, :create_children
            true
          when :update, :destroy
            resource.present?
          else
            false
          end
        end

        def can_create_grandchildren_results?
          result&.parent&.present? &&
            permission_action.action == :create_children
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
decidim-accountability-0.27.9 app/permissions/decidim/accountability/admin/permissions.rb
decidim-accountability-0.27.8 app/permissions/decidim/accountability/admin/permissions.rb
decidim-accountability-0.27.7 app/permissions/decidim/accountability/admin/permissions.rb
decidim-accountability-0.27.6 app/permissions/decidim/accountability/admin/permissions.rb
decidim-accountability-0.26.10 app/permissions/decidim/accountability/admin/permissions.rb
decidim-accountability-0.26.9 app/permissions/decidim/accountability/admin/permissions.rb
decidim-accountability-0.27.5 app/permissions/decidim/accountability/admin/permissions.rb
decidim-accountability-0.26.8 app/permissions/decidim/accountability/admin/permissions.rb
decidim-accountability-0.27.4 app/permissions/decidim/accountability/admin/permissions.rb
decidim-accountability-0.27.3 app/permissions/decidim/accountability/admin/permissions.rb
decidim-accountability-0.26.7 app/permissions/decidim/accountability/admin/permissions.rb
decidim-accountability-0.26.5 app/permissions/decidim/accountability/admin/permissions.rb
decidim-accountability-0.27.2 app/permissions/decidim/accountability/admin/permissions.rb
decidim-accountability-0.27.1 app/permissions/decidim/accountability/admin/permissions.rb
decidim-accountability-0.26.4 app/permissions/decidim/accountability/admin/permissions.rb
decidim-accountability-0.27.0 app/permissions/decidim/accountability/admin/permissions.rb
decidim-accountability-0.26.3 app/permissions/decidim/accountability/admin/permissions.rb
decidim-accountability-0.27.0.rc2 app/permissions/decidim/accountability/admin/permissions.rb