Sha256: 9b24dfb624bbc65b2950320f503f95b4e6e829d3265b1317a0479254daec8053

Contents?: true

Size: 1.61 KB

Versions: 4

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

module Decidim
  module Initiatives
    module Abilities
      # Defines the abilities related to initiatives 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, Initiative if creation_enabled?
          can :read, Initiative do |initiative|
            initiative.published? || initiative.has_authorship?(user) || user&.admin?
          end

          can :read, :admin_dashboard if has_initiatives?

          define_membership_management_abilities
        end

        private

        def creation_enabled?
          Decidim::Initiatives.creation_enabled && (
            Decidim::Initiatives.do_not_require_authorization ||
              UserAuthorizations.for(user).any? ||
              user.user_groups.verified.any?
          )
        end

        def define_membership_management_abilities
          can :request_membership, Initiative do |initiative|
            !initiative.published? &&
              !initiative.has_authorship?(user) &&
              (
                Decidim::Initiatives.do_not_require_authorization ||
                UserAuthorizations.for(user).any? ||
                user.user_groups.verified.any?
              )
          end
        end

        def has_initiatives?
          initiatives = InitiativesCreated.by(user) | InitiativesPromoted.by(user)
          initiatives.any?
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
decidim-initiatives-0.11.2 app/models/decidim/initiatives/abilities/current_user_ability.rb
decidim-initiatives-0.11.1 app/models/decidim/initiatives/abilities/current_user_ability.rb
decidim-initiatives-0.11.0.pre1 app/models/decidim/initiatives/abilities/current_user_ability.rb
decidim-initiatives-0.9.1 app/models/decidim/initiatives/abilities/current_user_ability.rb