<commit_before>class AbilityDecorator
  include CanCan::Ability

  # All abilites are allocated from this initialiser, currently in 5 chunks.
  # Spree also defines other abilities.
  def initialize(user)
    add_base_abilities user if is_new_user? user
    add_enterprise_management_abilities user if can_manage_enterprises? user
    add_product_management_abilities user if can_manage_products? user
    add_order_management_abilities user if can_manage_orders? user
    add_relationship_management_abilities user if can_manage_relationships? user
  end

  # New users have no enterprises.
  def is_new_user?(user)
    user.enterprises.blank?
  end

  # Users can manage an enterprise if they have one.
  def can_manage_enterprises?(user)
    user.enterprises.present?
  end

  # Users can manage products if they have an enterprise.
  def can_manage_products?(user)
    can_manage_enterprises? user
  end

  # Users can manage orders if they have a sells own/any enterprise.
  def can_manage_orders?(user)
    ( user.enterprises.map(&:type) & %w(single full) ).any?
  end

  def can_manage_relationships?(user)
    can_manage_enterprises? user
  end

  # New users can create an enterprise, and gain other permissions from doing this.
  def add_base_abilities(user)
    can [:create], Enterprise
  end

  def add_enterprise_management_abilities(user)
    # Spree performs authorize! on (:create, nil) when creating a new order from admin, and also (:search, nil)
    # when searching for variants to add to the order
    can [:create, :search, :bulk_update], nil

    can [:admin, :index], :overview

    can [:admin, :index, :read, :create, :edit, :update_positions, :destroy], ProducerProperty

    can [:admin, :index, :create], Enterprise
    can [:read, :edit, :update, :bulk_update], Enterprise do |enterprise|
      user.enterprises.include? enterprise
    end

    # All enterprises can have fees, though possibly suppliers don't need them?
    can [:index, :create], EnterpriseFee
    can [:admin, :read, :edit, :bulk_update, :destroy], EnterpriseFee do |enterprise_fee|
      user.enterprises.include? enterprise_fee.enterprise
    end
  end

  def add_product_management_abilities(user)
    # Enterprise User can only access products that they are a supplier for
    can [:create], Spree::Product
    can [:admin, :read, :update, :product_distributions, :bulk_edit, :bulk_update, :clone, :destroy], Spree::Product do |product|
      OpenFoodNetwork::Permissions.new(user).managed_product_enterprises.include? product.supplier
    end

    can [:create], Spree::Variant
    can [:admin, :index, :read, :edit, :update, :search, :destroy], Spree::Variant do |variant|
      OpenFoodNetwork::Permissions.new(user).managed_product_enterprises.include? variant.product.supplier
    end

    can [:admin, :index, :read, :create, :edit, :update_positions, :destroy], Spree::ProductProperty
    can [:admin, :index, :read, :create, :edit, :update, :destroy], Spree::Image

    can [:admin, :index, :read, :search], Spree::Taxon
    can [:admin, :index, :read, :create, :edit], Spree::Classification

    # Reports page
    can [:admin, :index, :customers, :orders_and_fulfillment, :products_and_inventory], :report
  end

  def add_order_management_abilities(user)
    # Enterprise User can only access orders that they are a distributor for
    can [:index, :create], Spree::Order
    can [:read, :update, :fire, :resend], Spree::Order do |order|
      # We allow editing orders with a nil distributor as this state occurs
      # during the order creation process from the admin backend
      order.distributor.nil? || user.enterprises.include?(order.distributor)
    end
    can [:admin, :bulk_management], Spree::Order if user.admin? || user.enterprises.any?(&:is_distributor)<commit_msg>allow supplier enterprise manager to see bulk coop reports<commit_after>class AbilityDecorator
  include CanCan::Ability

  # All abilites are allocated from this initialiser, currently in 5 chunks.
  # Spree also defines other abilities.
  def initialize(user)
    add_base_abilities user if is_new_user? user
    add_enterprise_management_abilities user if can_manage_enterprises? user
    add_product_management_abilities user if can_manage_products? user
    add_order_management_abilities user if can_manage_orders? user
    add_relationship_management_abilities user if can_manage_relationships? user
  end

  # New users have no enterprises.
  def is_new_user?(user)
    user.enterprises.blank?
  end

  # Users can manage an enterprise if they have one.
  def can_manage_enterprises?(user)
    user.enterprises.present?
  end

  # Users can manage products if they have an enterprise.
  def can_manage_products?(user)
    can_manage_enterprises? user
  end

  # Users can manage orders if they have a sells own/any enterprise.
  def can_manage_orders?(user)
    ( user.enterprises.map(&:type) & %w(single full) ).any?
  end

  def can_manage_relationships?(user)
    can_manage_enterprises? user
  end

  # New users can create an enterprise, and gain other permissions from doing this.
  def add_base_abilities(user)
    can [:create], Enterprise
  end

  def add_enterprise_management_abilities(user)
    # Spree performs authorize! on (:create, nil) when creating a new order from admin, and also (:search, nil)
    # when searching for variants to add to the order
    can [:create, :search, :bulk_update], nil

    can [:admin, :index], :overview

    can [:admin, :index, :read, :create, :edit, :update_positions, :destroy], ProducerProperty

    can [:admin, :index, :create], Enterprise
    can [:read, :edit, :update, :bulk_update], Enterprise do |enterprise|
      user.enterprises.include? enterprise
    end

    # All enterprises can have fees, though possibly suppliers don't need them?
    can [:index, :create], EnterpriseFee
    can [:admin, :read, :edit, :bulk_update, :destroy], EnterpriseFee do |enterprise_fee|
      user.enterprises.include? enterprise_fee.enterprise
    end
  end

  def add_product_management_abilities(user)
    # Enterprise User can only access products that they are a supplier for
    can [:create], Spree::Product
    can [:admin, :read, :update, :product_distributions, :bulk_edit, :bulk_update, :clone, :destroy], Spree::Product do |product|
      OpenFoodNetwork::Permissions.new(user).managed_product_enterprises.include? product.supplier
    end

    can [:create], Spree::Variant
    can [:admin, :index, :read, :edit, :update, :search, :destroy], Spree::Variant do |variant|
      OpenFoodNetwork::Permissions.new(user).managed_product_enterprises.include? variant.product.supplier
    end

    can [:admin, :index, :read, :create, :edit, :update_positions, :destroy], Spree::ProductProperty
    can [:admin, :index, :read, :create, :edit, :update, :destroy], Spree::Image

    can [:admin, :index, :read, :search], Spree::Taxon
    can [:admin, :index, :read, :create, :edit], Spree::Classification

    # Reports page
    can [:admin, :index, :customers, :bulk_coop, :orders_and_fulfillment, :products_and_inventory], :report
  end

  def add_order_management_abilities(user)
    # Enterprise User can only access orders that they are a distributor for
    can [:index, :create], Spree::Order
    can [:read, :update, :fire, :resend], Spree::Order do |order|
      # We allow editing orders with a nil distributor as this state occurs
      # during the order creation process from the admin backend
      order.distributor.nil? || user.enterprises.include?(order.distributor)
    end
    can [:admin, :bulk_management], Spree::Order if user.admin? || user.enterprises.any?(&:is_distributor)
{'fraction_non_alphanumeric': 0.09881061299176579, 'fraction_numerical': 0.00026140373807345446, 'mean_word_length': 4.457917261055635, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4316169', 'n_tokens_mistral': 2349, 'n_tokens_neox': 2261, 'n_words': 734}