Sha256: 9e43edf2b1096e5e828d60b2833b22e39b3dd8f7e8f3b6d5e4c0b4d75397ae81

Contents?: true

Size: 1.23 KB

Versions: 20

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

require "active_support/concern"

module Decidim
  module ActionAuthorization
    extend ActiveSupport::Concern

    included do
      helper_method :action_authorized_to
    end

    private

    def action_authorized_to(action, resource: nil, permissions_holder: nil)
      action_authorization_cache[action_authorization_cache_key(action, resource, permissions_holder)] ||=
        ::Decidim::ActionAuthorizer.new(current_user, action, permissions_holder || resource&.component || current_component, resource).authorize
    end

    def action_authorization_cache
      request.env["decidim.action_authorization_cache"] ||= {}
    end

    def action_authorization_cache_key(action, resource, permissions_holder = nil)
      if resource && !resource.permissions.nil?
        "#{action}-#{resource.component.id}-#{resource.resource_manifest.name}-#{resource.id}"
      elsif resource && permissions_holder
        "#{action}-#{permissions_holder.class.name}-#{permissions_holder.id}-#{resource.resource_manifest.name}-#{resource.id}"
      elsif permissions_holder
        "#{action}-#{permissions_holder.class.name}-#{permissions_holder.id}"
      else
        "#{action}-#{current_component.id}"
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
decidim-core-0.24.3 lib/decidim/action_authorization.rb
decidim-core-0.23.6 lib/decidim/action_authorization.rb
decidim-core-0.24.2 lib/decidim/action_authorization.rb
decidim-core-0.23.5 lib/decidim/action_authorization.rb
decidim-core-0.24.1 lib/decidim/action_authorization.rb
decidim-core-0.24.0 lib/decidim/action_authorization.rb
decidim-core-0.24.0.rc2 lib/decidim/action_authorization.rb
decidim-core-0.23.4 lib/decidim/action_authorization.rb
decidim-core-0.24.0.rc1 lib/decidim/action_authorization.rb
decidim-core-0.23.3 lib/decidim/action_authorization.rb
decidim-core-0.23.2 lib/decidim/action_authorization.rb
decidim-core-0.23.1 lib/decidim/action_authorization.rb
decidim-core-0.23.1.rc1 lib/decidim/action_authorization.rb
decidim-core-0.23.0 lib/decidim/action_authorization.rb
decidim-core-0.22.0 lib/decidim/action_authorization.rb
decidim-core-0.21.0 app/controllers/concerns/decidim/action_authorization.rb
decidim-core-0.20.1 app/controllers/concerns/decidim/action_authorization.rb
decidim-core-0.20.0 app/controllers/concerns/decidim/action_authorization.rb
decidim-core-0.19.1 app/controllers/concerns/decidim/action_authorization.rb
decidim-core-0.19.0 app/controllers/concerns/decidim/action_authorization.rb