Sha256: 787061a0bd9f18a0c6f4a402dac845c5355f701f09b764d33e72255c39b0a903

Contents?: true

Size: 1.66 KB

Versions: 72

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

module Decidim
  # Default permissions class for all components and spaces. It disauthorizes all
  # actions by any kind of user. Also works as a default implementation so other
  # components can inherit from it and get some cenvenience methods.
  class DefaultPermissions
    def initialize(user, permission_action, context = {})
      @user = user
      @permission_action = permission_action
      @context = context
    end

    def permissions
      permission_action
    end

    private

    attr_reader :user, :permission_action, :context

    def disallow!
      permission_action.trace(self.class.name, :disallowed)
      permission_action.disallow!
    end

    def allow!
      permission_action.trace(self.class.name, :allowed)
      permission_action.allow!
    end

    def toggle_allow(condition)
      condition ? allow! : disallow!
    end

    def read_participatory_space_action?
      permission_action.action == :read &&
        [:participatory_space, :component].include?(permission_action.subject)
    end

    def authorized?(permission_action, resource: nil)
      return unless resource || component
      return if component && resource && component != resource.component

      ActionAuthorizer.new(user, permission_action, component, resource).authorize.ok?
    end

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

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

    def component
      @component ||= context.fetch(:current_component)
    end

    def space
      @space ||= component.participatory_space
    end
  end
end

Version data entries

72 entries across 72 versions & 1 rubygems

Version Path
decidim-core-0.28.4 app/permissions/decidim/default_permissions.rb
decidim-core-0.27.9 app/permissions/decidim/default_permissions.rb
decidim-core-0.28.3 app/permissions/decidim/default_permissions.rb
decidim-core-0.27.8 app/permissions/decidim/default_permissions.rb
decidim-core-0.28.2 app/permissions/decidim/default_permissions.rb
decidim-core-0.27.7 app/permissions/decidim/default_permissions.rb
decidim-core-0.28.1 app/permissions/decidim/default_permissions.rb
decidim-core-0.27.6 app/permissions/decidim/default_permissions.rb
decidim-core-0.26.10 app/permissions/decidim/default_permissions.rb
decidim-core-0.26.9 app/permissions/decidim/default_permissions.rb
decidim-core-0.28.0 app/permissions/decidim/default_permissions.rb
decidim-core-0.27.5 app/permissions/decidim/default_permissions.rb
decidim-core-0.28.0.rc5 app/permissions/decidim/default_permissions.rb
decidim-core-0.28.0.rc4 app/permissions/decidim/default_permissions.rb
decidim-core-0.26.8 app/permissions/decidim/default_permissions.rb
decidim-core-0.27.4 app/permissions/decidim/default_permissions.rb
decidim-core-0.27.3 app/permissions/decidim/default_permissions.rb
decidim-core-0.26.7 app/permissions/decidim/default_permissions.rb
decidim-core-0.26.5 app/permissions/decidim/default_permissions.rb
decidim-core-0.27.2 app/permissions/decidim/default_permissions.rb