Sha256: 91ee157af620a094441965e9fc81d6f60397562e69e3ff7cf2d1a23a076e845d

Contents?: true

Size: 1.11 KB

Versions: 6

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

module Decidim
  module Debates
    module Abilities
      # Defines the abilities related to debates for a logged in user.
      # Intended to be used with `cancancan`.
      class CurrentUserAbility
        include CanCan::Ability

        attr_reader :user, :context

        def initialize(user, context)
          return unless user

          @user = user
          @context = context

          can :create, Debate if authorized?(:create) && creation_enabled?
          can :report, Debate
        end

        private

        def authorized?(action)
          return unless feature

          ActionAuthorizer.new(user, feature, action).authorize.ok?
        end

        def creation_enabled?
          return unless current_settings
          current_settings.creation_enabled?
        end

        def current_settings
          context.fetch(:current_settings, nil)
        end

        def feature
          feature = context.fetch(:current_feature, nil)
          return nil unless feature && feature.manifest.name == :debates

          feature
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-debates-0.10.1 app/models/decidim/debates/abilities/current_user_ability.rb
decidim-debates-0.10.0 app/models/decidim/debates/abilities/current_user_ability.rb
decidim-debates-0.9.3 app/models/decidim/debates/abilities/current_user_ability.rb
decidim-debates-0.9.2 app/models/decidim/debates/abilities/current_user_ability.rb
decidim-debates-0.9.1 app/models/decidim/debates/abilities/current_user_ability.rb
decidim-debates-0.9.0 app/models/decidim/debates/abilities/current_user_ability.rb