Sha256: ef7cac9b4bebdc4210271137476af499ee0d63bf7394e70b2089e4fd15c84530

Contents?: true

Size: 1.96 KB

Versions: 4

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

module Decidim
  module Initiatives
    module Abilities
      module Admin
        # Defines the abilities related to plain users able to administer initiatives.
        # Intended to be used with `cancancan`.
        class InitiativeUserAbility
          include CanCan::Ability

          attr_reader :user, :context

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

            @user = user
            @context = context

            grant_dashboard_access
            grant_initiative_permissions
          end

          private

          def grant_dashboard_access
            can :read, :admin_dashboard if has_initiatives?(user)
          end

          def grant_initiative_permissions
            can :list, Decidim::Initiative if has_initiatives?(user)

            can :preview, Initiative do |initiative|
              initiative.has_authorship? user
            end

            can :read, Initiative do |initiative|
              initiative.has_authorship?(user) &&
                Decidim::Initiatives.print_enabled
            end

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

            can :update, Decidim::Initiative do |initiative|
              initiative.has_authorship?(user) && initiative.created?
            end

            can :send_to_technical_validation, Initiative do |initiative|
              initiative.has_authorship?(user) &&
                initiative.created? && (
                  !initiative.decidim_user_group_id.nil? ||
                    initiative.committee_members.approved.count >= Decidim::Initiatives.minimum_committee_members
                )
            end
          end

          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/initiative_user_ability.rb
decidim-initiatives-0.11.1 app/models/decidim/initiatives/abilities/admin/initiative_user_ability.rb
decidim-initiatives-0.11.0.pre1 app/models/decidim/initiatives/abilities/admin/initiative_user_ability.rb
decidim-initiatives-0.9.1 app/models/decidim/initiatives/abilities/admin/initiative_user_ability.rb