Sha256: 381a9d904d471c92f611b91aee4e0bcb8d61b1a33abd7d16702107849b809018

Contents?: true

Size: 1.42 KB

Versions: 4

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module Decidim
  module Initiatives
    module Abilities
      module Admin
        # Defines the abilities related to plain users able to administer
        # promotal committee membership requests.
        # Intended to be used with `cancancan`.
        class CommitteeUserAbility
          include CanCan::Ability

          attr_reader :user, :context

          def initialize(user, context)
            return unless user
            return if user&.admin?

            @user = user
            @context = context

            can :manage_membership, Decidim::Initiative do |initiative|
              initiative.has_authorship?(user)
            end

            can :index, InitiativesCommitteeMember if has_initiatives?(user)
            can :approve, InitiativesCommitteeMember do |request|
              request.initiative.has_authorship?(user) &&
                !request.initiative.published? &&
                !request.accepted?
            end

            can :revoke, InitiativesCommitteeMember do |request|
              request.initiative.has_authorship?(user) &&
                !request.initiative.published? &&
                !request.rejected?
            end
          end

          private

          def has_initiatives?(user)
            initiatives = InitiativesCreated.by(user) | InitiativesPromoted.by(user)
            initiatives.any?
          end
        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/admin/committee_user_ability.rb
decidim-initiatives-0.11.1 app/models/decidim/initiatives/abilities/admin/committee_user_ability.rb
decidim-initiatives-0.11.0.pre1 app/models/decidim/initiatives/abilities/admin/committee_user_ability.rb
decidim-initiatives-0.9.1 app/models/decidim/initiatives/abilities/admin/committee_user_ability.rb