Sha256: b2ac9c907f68fe295db668d4c2ff7f5a8086d6e4f501a602df88430ab6afdba6

Contents?: true

Size: 1.78 KB

Versions: 9

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

module Decidim
  module Abilities
    # Defines the abilities for a User. Intended to be used with `cancancan`.
    class BaseAbility
      include CanCan::Ability

      # Initializes the ability class for the given user. Automatically merges
      # injected abilities fmor the configuration. In order to inject more
      # abilities, add this code in the `engine.rb` file of your own engine, for
      # example, inside an initializer:
      #
      #   Decidim.configure do |config|
      #     config.abilities << Decidim::MyEngine::Abilities::MyAbility
      #   end
      #
      # Note that, in development, this will force you to restart the server
      # every time you change things in your ability classes.
      #
      # user - the User that needs its abilities checked.
      # context - a Hash with some context related to the current request.
      def initialize(user, context = {})
        Decidim.abilities.each do |ability|
          merge ability.constantize.new(user, context)
        end

        can :create, Authorization do |authorization|
          authorization.user == user && not_already_active?(user, authorization)
        end

        can :update, Authorization do |authorization|
          authorization.user == user && !authorization.granted?
        end

        can :manage, Follow do |follow|
          follow.user == user
        end

        can :manage, Notification do |notification|
          notification.user == user
        end

        can :manage, Messaging::Conversation do |conversation|
          conversation.participants.include?(user)
        end
      end

      private

      def not_already_active?(user, authorization)
        Verifications::Authorizations.new(user: user, name: authorization.name).none?
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
decidim-core-0.9.3 app/models/decidim/abilities/base_ability.rb
decidim-core-0.9.2 app/models/decidim/abilities/base_ability.rb
decidim-core-0.9.1 app/models/decidim/abilities/base_ability.rb
decidim-core-0.9.0 app/models/decidim/abilities/base_ability.rb
decidim-core-0.8.4 app/models/decidim/abilities/base_ability.rb
decidim-core-0.8.3 app/models/decidim/abilities/base_ability.rb
decidim-core-0.8.2 app/models/decidim/abilities/base_ability.rb
decidim-core-0.8.1 app/models/decidim/abilities/base_ability.rb
decidim-core-0.8.0 app/models/decidim/abilities/base_ability.rb