Fix long lines in order_cycle_permissions and permissions

This commit is contained in:
luisramos0
2019-07-04 17:10:14 +01:00
parent dee1c3d139
commit ef61310bad
3 changed files with 169 additions and 68 deletions

View File

@@ -152,12 +152,10 @@ Metrics/LineLength:
- lib/open_food_network/order_and_distributor_report.rb
- lib/open_food_network/order_cycle_form_applicator.rb
- lib/open_food_network/order_cycle_management_report.rb
- lib/open_food_network/order_cycle_permissions.rb
- lib/open_food_network/order_grouper.rb
- lib/open_food_network/orders_and_fulfillments_report.rb
- lib/open_food_network/payments_report.rb
- lib/open_food_network/permalink_generator.rb
- lib/open_food_network/permissions.rb
- lib/open_food_network/products_cache.rb
- lib/open_food_network/products_renderer.rb
- lib/open_food_network/proxy_order_syncer.rb
@@ -470,6 +468,7 @@ Metrics/AbcSize:
- lib/open_food_network/orders_and_fulfillments_report.rb
- lib/open_food_network/packing_report.rb
- lib/open_food_network/payments_report.rb
- lib/open_food_network/permissions.rb
- lib/open_food_network/products_and_inventory_report.rb
- lib/open_food_network/reports/line_items.rb
- lib/open_food_network/sales_tax_report.rb

View File

@@ -9,8 +9,8 @@ module OpenFoodNetwork
end
# List of any enterprises whose exchanges I should be able to see in order_cycle
# NOTE: the enterprises a given user can see actually in the OC interface depend on the relationships
# of their enterprises to the coordinator of the order cycle, rather than on the order cycle itself
# NOTE: the enterprises a given user can see actually in the OC interface depend on the
# relationships of their enterprises to the coordinator of the OC, rather than on the OC itself
def visible_enterprises
return Enterprise.where("1=0") if @coordinator.blank?
if managed_enterprises.include? @coordinator
@@ -19,30 +19,51 @@ module OpenFoodNetwork
if @coordinator.sells == "any"
# If the coordinator sells any, relationships come into play
related_enterprises_granting(:add_to_order_cycle, to: [@coordinator.id]).each do |enterprise_id|
related_enterprises_granting(:add_to_order_cycle,
to: [@coordinator.id]).each do |enterprise_id|
coordinator_permitted_ids << enterprise_id
end
# As a safety net, we should load all of the enterprises invloved in existing exchanges in this order cycle
# As a safety net, we load all the enterprises involved in existing exchanges in this OC
all_active_ids = @order_cycle.suppliers.pluck(:id) | @order_cycle.distributors.pluck(:id)
end
Enterprise.where(id: coordinator_permitted_ids | all_active_ids)
else
# Any enterprises that I manage directly, which have granted P-OC to the coordinator
managed_permitted_ids = related_enterprises_granting(:add_to_order_cycle, to: [@coordinator.id], scope: managed_participating_enterprises )
managed_permitted_ids = related_enterprises_granting(
:add_to_order_cycle,
to: [@coordinator.id],
scope: managed_participating_enterprises
)
# Any hubs in this OC that have been granted P-OC by producers I manage in this OC
hubs_permitted_ids = related_enterprises_granted(:add_to_order_cycle, by: managed_participating_producers.select("enterprises.id"), scope: @order_cycle.distributors)
hubs_permitted_ids = related_enterprises_granted(
:add_to_order_cycle,
by: managed_participating_producers.select("enterprises.id"),
scope: @order_cycle.distributors
)
# Any hubs in this OC that have granted P-OC to producers I manage in this OC
hubs_permitting_ids = related_enterprises_granting(:add_to_order_cycle, to: managed_participating_producers.select("enterprises.id"), scope: @order_cycle.distributors)
hubs_permitting_ids = related_enterprises_granting(
:add_to_order_cycle,
to: managed_participating_producers.select("enterprises.id"),
scope: @order_cycle.distributors
)
# Any producers in this OC that have been granted P-OC by hubs I manage in this OC
producers_permitted_ids = related_enterprises_granted(:add_to_order_cycle, by: managed_participating_hubs.select("enterprises.id"), scope: @order_cycle.suppliers)
producers_permitted_ids = related_enterprises_granted(
:add_to_order_cycle,
by: managed_participating_hubs.select("enterprises.id"),
scope: @order_cycle.suppliers
)
# Any producers in this OC that have granted P-OC to hubs I manage in this OC
producers_permitting_ids = related_enterprises_granting(:add_to_order_cycle, to: managed_participating_hubs.select("enterprises.id"), scope: @order_cycle.suppliers)
producers_permitting_ids = related_enterprises_granting(
:add_to_order_cycle,
to: managed_participating_hubs.select("enterprises.id"),
scope: @order_cycle.suppliers
)
managed_active_ids = []
hubs_active_ids = []
@@ -53,20 +74,38 @@ module OpenFoodNetwork
managed_active_ids = managed_participating_enterprises.pluck(:id)
# TODO: Remove this when all P-OC are sorted out
# Any hubs that currently have outgoing exchanges distributing variants of producers I manage
variants = Spree::Variant.joins(:product).where('spree_products.supplier_id IN (?)', managed_enterprises.is_primary_producer.select("enterprises.id"))
# Hubs that currently have outgoing exchanges distributing variants of producers I manage
variants = Spree::Variant.
joins(:product).
where("spree_products.supplier_id IN (?)",
managed_enterprises.is_primary_producer.select("enterprises.id"))
active_exchanges = @order_cycle.
exchanges.outgoing.with_any_variant(variants.select("spree_variants.id"))
hubs_active_ids = active_exchanges.map(&:receiver_id)
# TODO: Remove this when all P-OC are sorted out
# Any producers of variants that hubs I manage are currently distributing in this OC
variant_ids = Spree::Variant.joins(:exchanges).where("exchanges.receiver_id IN (?) AND exchanges.order_cycle_id = (?) AND exchanges.incoming = 'f'", managed_participating_hubs.select("enterprises.id"), @order_cycle).pluck(:id).uniq
product_ids = Spree::Product.joins(:variants_including_master).where("spree_variants.id IN (?)", variant_ids).pluck(:id).uniq
producers_active_ids = Enterprise.joins(:supplied_products).where("spree_products.id IN (?)", product_ids).pluck(:id).uniq
variant_ids = Spree::Variant.joins(:exchanges).
where(
"exchanges.receiver_id IN (?)
AND exchanges.order_cycle_id = (?)
AND exchanges.incoming = 'f'",
managed_participating_hubs.select("enterprises.id"),
@order_cycle
).pluck(:id).uniq
product_ids = Spree::Product.joins(:variants_including_master).
where("spree_variants.id IN (?)", variant_ids).pluck(:id).uniq
producers_active_ids = Enterprise.joins(:supplied_products).
where("spree_products.id IN (?)", product_ids).pluck(:id).uniq
end
ids = managed_permitted_ids | hubs_permitted_ids | hubs_permitting_ids | producers_permitted_ids | producers_permitting_ids | managed_active_ids | hubs_active_ids | producers_active_ids
ids = managed_permitted_ids | hubs_permitted_ids | hubs_permitting_ids \
| producers_permitted_ids | producers_permitting_ids | managed_active_ids \
| hubs_active_ids | producers_active_ids
Enterprise.where(id: ids)
end
@@ -89,9 +128,12 @@ module OpenFoodNetwork
# All variants belonging to the producer
Spree::Variant.joins(:product).where('spree_products.supplier_id = (?)', producer)
else
# All variants of the producer if it has granted P-OC to any of my managed hubs that are in this order cycle
permitted = EnterpriseRelationship.permitting(managed_participating_hubs.select("enterprises.id")).
permitted_by(producer.id).with_permission(:add_to_order_cycle).present?
# Producer variants if it has granted P-OC to any of my managed hubs that are in this OC
permitted = EnterpriseRelationship.
permitting(managed_participating_hubs.select("enterprises.id")).
permitted_by(producer.id).
with_permission(:add_to_order_cycle).
present?
if permitted
Spree::Variant.joins(:product).where('spree_products.supplier_id = (?)', producer)
else
@@ -125,32 +167,45 @@ module OpenFoodNetwork
# TODO: isn't this completely redundant given the assignment of hub_variants below?
coordinator_variants = []
if hub == @coordinator
coordinator_variants = Spree::Variant.joins(:product).where('spree_products.supplier_id = (?)', @coordinator)
coordinator_variants = Spree::Variant.joins(:product).
where('spree_products.supplier_id = (?)', @coordinator)
end
# Any variants of any producers that have granted the hub P-OC
producer_ids = related_enterprises_granting(:add_to_order_cycle, to: [hub.id], scope: Enterprise.is_primary_producer)
permitted_variants = Spree::Variant.joins(:product).where('spree_products.supplier_id IN (?)', producer_ids)
producer_ids = related_enterprises_granting(:add_to_order_cycle,
to: [hub.id],
scope: Enterprise.is_primary_producer)
permitted_variants = Spree::Variant.joins(:product).
where('spree_products.supplier_id IN (?)', producer_ids)
hub_variants = Spree::Variant.joins(:product).where('spree_products.supplier_id = (?)', hub)
# PLUS any variants that are already in an outgoing exchange of this hub, so things don't break
# PLUS variants that are already in an outgoing exchange of this hub, so things don't break
# TODO: Remove this when all P-OC are sorted out
active_variants = []
@order_cycle.exchanges.outgoing.where(receiver_id: hub).limit(1).each do |exchange|
active_variants = exchange.variants
end
Spree::Variant.where(id: coordinator_variants | hub_variants | permitted_variants | active_variants)
Spree::Variant.
where(id: coordinator_variants | hub_variants | permitted_variants | active_variants)
else
# Any variants produced by MY PRODUCERS that are in this order cycle, where my producer has granted P-OC to the hub
producer_ids = related_enterprises_granting(:add_to_order_cycle, to: [hub.id], scope: managed_participating_producers)
permitted_variants = Spree::Variant.joins(:product).where('spree_products.supplier_id IN (?)', producer_ids)
# Variants produced by MY PRODUCERS that are in this OC,
# where my producer has granted P-OC to the hub
producer_ids = related_enterprises_granting(:add_to_order_cycle,
to: [hub.id],
scope: managed_participating_producers)
permitted_variants = Spree::Variant.joins(:product).
where('spree_products.supplier_id IN (?)', producer_ids)
# PLUS any of my incoming producers' variants that are already in an outgoing exchange of this hub, so things don't break
# TODO: Remove this when all P-OC are sorted out
# PLUS my incoming producers' variants that are already in an outgoing exchange of this hub,
# so things don't break. TODO: Remove this when all P-OC are sorted out
active_variants = Spree::Variant.joins(:exchanges, :product).
where("exchanges.receiver_id = (?) AND spree_products.supplier_id IN (?) AND incoming = 'f'", hub.id, managed_enterprises.is_primary_producer.select("enterprises.id"))
where("exchanges.receiver_id = (?)
AND spree_products.supplier_id IN (?)
AND incoming = 'f'",
hub.id,
managed_enterprises.is_primary_producer.select("enterprises.id"))
Spree::Variant.where(id: permitted_variants | active_variants)
end
@@ -164,30 +219,41 @@ module OpenFoodNetwork
# Any variants produced by the coordinator, for outgoing exchanges with itself
coordinator_variants = []
if hub == @coordinator
coordinator_variants = Spree::Variant.joins(:product).where('spree_products.supplier_id = (?)', @coordinator)
coordinator_variants = Spree::Variant.joins(:product).
where('spree_products.supplier_id = (?)', @coordinator)
end
# Any variants of any producers that have granted the hub P-OC
producer_ids = related_enterprises_granting(:add_to_order_cycle, to: [hub.id], scope: Enterprise.is_primary_producer)
permitted_variants = Spree::Variant.joins(:product).where('spree_products.supplier_id IN (?)', producer_ids)
producer_ids = related_enterprises_granting(:add_to_order_cycle,
to: [hub.id],
scope: Enterprise.is_primary_producer)
permitted_variants = Spree::Variant.joins(:product).
where('spree_products.supplier_id IN (?)', producer_ids)
hub_variants = Spree::Variant.joins(:product).where('spree_products.supplier_id = (?)', hub)
# PLUS any variants that are already in an outgoing exchange of this hub, so things don't break
# PLUS variants that are already in an outgoing exchange of this hub, so things don't break
# TODO: Remove this when all P-OC are sorted out
active_variants = []
@order_cycle.exchanges.outgoing.where(receiver_id: hub).limit(1).each do |exchange|
active_variants = exchange.variants
end
Spree::Variant.where(id: coordinator_variants | hub_variants | permitted_variants | active_variants)
Spree::Variant.
where(id: coordinator_variants | hub_variants | permitted_variants | active_variants)
else
# Any of my managed producers in this order cycle granted P-OC by the hub
granted_producers = related_enterprises_granted(:add_to_order_cycle, by: [hub.id], scope: managed_participating_producers)
granted_producers = related_enterprises_granted(:add_to_order_cycle,
by: [hub.id],
scope: managed_participating_producers)
# Any variants produced by MY PRODUCERS that are in this order cycle, where my producer has granted P-OC to the hub
granting_producer_ids = related_enterprises_granting(:add_to_order_cycle, to: [hub.id], scope: granted_producers)
permitted_variants = Spree::Variant.joins(:product).where('spree_products.supplier_id IN (?)', granting_producer_ids)
# Variants produced by MY PRODUCERS that are in this OC,
# where my producer has granted P-OC to the hub
granting_producer_ids = related_enterprises_granting(:add_to_order_cycle,
to: [hub.id],
scope: granted_producers)
permitted_variants = Spree::Variant.joins(:product).
where('spree_products.supplier_id IN (?)', granting_producer_ids)
Spree::Variant.where(id: permitted_variants)
end
@@ -196,12 +262,14 @@ module OpenFoodNetwork
private
def user_manages_coordinator_or(enterprise)
managed_enterprises.pluck(:id).include?(@coordinator.id) || managed_enterprises.pluck(:id).include?(enterprise.id)
managed_enterprises.pluck(:id).include?(@coordinator.id) \
|| managed_enterprises.pluck(:id).include?(enterprise.id)
end
def managed_participating_enterprises
return @managed_participating_enterprises unless @managed_participating_enterprises.nil?
@managed_participating_enterprises = managed_enterprises.where(id: @order_cycle.suppliers | @order_cycle.distributors)
@managed_participating_enterprises = managed_enterprises.
where(id: @order_cycle.suppliers | @order_cycle.distributors)
end
def managed_participating_hubs
@@ -223,16 +291,30 @@ module OpenFoodNetwork
# Find my managed hubs in this order cycle
hubs = managed_participating_hubs
# Any incoming exchange where the producer has granted P-OC to one or more of those hubs
producer_ids = related_enterprises_granting(:add_to_order_cycle, to: hubs.select("enterprises.id"), scope: Enterprise.is_primary_producer)
permitted_exchange_ids = @order_cycle.exchanges.incoming.where(sender_id: producer_ids).pluck :id
producer_ids = related_enterprises_granting(:add_to_order_cycle,
to: hubs.select("enterprises.id"),
scope: Enterprise.is_primary_producer)
permitted_exchange_ids = @order_cycle.
exchanges.incoming.where(sender_id: producer_ids).pluck :id
# TODO: remove active_exchanges when we think it is safe to do so
# active_exchanges is for backward compatability, before we restricted variants in each
# outgoing exchange to those where the producer had granted P-OC to the distributor
# For any of my managed hubs in this OC, any incoming exchanges supplying variants in my outgoing exchanges
variant_ids = Spree::Variant.joins(:exchanges).where("exchanges.receiver_id IN (?) AND exchanges.order_cycle_id = (?) AND exchanges.incoming = 'f'", hubs.select("enterprises.id"), @order_cycle).pluck(:id).uniq
product_ids = Spree::Product.joins(:variants_including_master).where("spree_variants.id IN (?)", variant_ids).pluck(:id).uniq
producer_ids = Enterprise.joins(:supplied_products).where("spree_products.id IN (?)", product_ids).pluck(:id).uniq
# For any of my managed hubs in this OC,
# any incoming exchanges supplying variants in my outgoing exchanges
variant_ids = Spree::Variant.joins(:exchanges).
where("exchanges.receiver_id IN (?)
AND exchanges.order_cycle_id = (?)
AND exchanges.incoming = 'f'",
hubs.select("enterprises.id"),
@order_cycle).pluck(:id).uniq
product_ids = Spree::Product.joins(:variants_including_master).
where("spree_variants.id IN (?)", variant_ids).pluck(:id).uniq
producer_ids = Enterprise.joins(:supplied_products).
where("spree_products.id IN (?)", product_ids).pluck(:id).uniq
active_exchange_ids = @order_cycle.exchanges.incoming.where(sender_id: producer_ids).pluck :id
permitted_exchange_ids | active_exchange_ids
@@ -241,8 +323,10 @@ module OpenFoodNetwork
def order_cycle_exchange_ids_distributing_my_variants
# Find my producers in this order cycle
producer_ids = managed_participating_producers.pluck :id
# Any outgoing exchange where the distributor has been granted P-OC by one or more of those producers
hub_ids = related_enterprises_granted(:add_to_order_cycle, by: producer_ids, scope: Enterprise.is_hub)
# Outgoing exchanges with distributor that has been granted P-OC by 1 or more of the producers
hub_ids = related_enterprises_granted(:add_to_order_cycle,
by: producer_ids,
scope: Enterprise.is_hub)
permitted_exchange_ids = @order_cycle.exchanges.outgoing.where(receiver_id: hub_ids).pluck :id
# TODO: remove active_exchanges when we think it is safe to do so

View File

@@ -10,17 +10,17 @@ module OpenFoodNetwork
end
end
# Find enterprises that an admin is allowed to add to an order cycle
# Enterprises that an admin is allowed to add to an order cycle
def visible_enterprises_for_order_reports
managed_and_related_enterprises_with :add_to_order_cycle
end
# Enterprises that the user manages and those that have granted P-OC to managed enterprises
def visible_enterprises
# Return enterprises that the user manages and those that have granted P-OC to managed enterprises
managed_and_related_enterprises_granting :add_to_order_cycle
end
# Find enterprises for which an admin is allowed to edit their profile
# Enterprises for which an admin is allowed to edit their profile
def editable_enterprises
managed_and_related_enterprises_granting :edit_profile
end
@@ -63,15 +63,20 @@ module OpenFoodNetwork
# Any orders that I can edit
editable = editable_orders.pluck(:id)
# Any orders placed through hubs that my producers have granted P-OC, and which contain my their products
# This is pretty complicated but it's looking for order where at least one of my producers has granted
# P-OC to the distributor AND the order contains products of at least one of THE SAME producers
granted_distributor_ids = related_enterprises_granted(:add_to_order_cycle, by: managed_enterprises.is_primary_producer.pluck(:id))
# Any orders placed through hubs that my producers have granted P-OC,
# and which contain their products. This is pretty complicated but it's looking for order
# where at least one of my producers has granted P-OC to the distributor
# AND the order contains products of at least one of THE SAME producers
granted_distributor_ids = related_enterprises_granted(
:add_to_order_cycle,
by: managed_enterprises.is_primary_producer.pluck(:id)
)
produced = Spree::Order.with_line_items_variants_and_products_outer.
where(
"spree_orders.distributor_id IN (?) AND spree_products.supplier_id IN (?)",
granted_distributor_ids,
related_enterprises_granting(:add_to_order_cycle, to: granted_distributor_ids).merge(managed_enterprises.is_primary_producer)
related_enterprises_granting(:add_to_order_cycle, to: granted_distributor_ids).
merge(managed_enterprises.is_primary_producer)
).pluck(:id)
Spree::Order.where(id: editable | produced)
@@ -83,7 +88,9 @@ module OpenFoodNetwork
managed = Spree::Order.where(distributor_id: managed_enterprises.pluck(:id)).pluck(:id)
# Any order that is placed through an order cycle one of my managed enterprises coordinates
coordinated = Spree::Order.where(order_cycle_id: coordinated_order_cycles.pluck(:id)).pluck(:id)
coordinated = Spree::Order.
where(order_cycle_id: coordinated_order_cycles.pluck(:id)).
pluck(:id)
Spree::Order.where(id: managed | coordinated )
end
@@ -94,7 +101,8 @@ module OpenFoodNetwork
# Any from visible orders, where the product is produced by one of my managed producers
produced = Spree::LineItem.where(order_id: visible_orders.pluck(:id)).joins(:product).
where('spree_products.supplier_id IN (?)', managed_enterprises.is_primary_producer.pluck(:id))
where("spree_products.supplier_id IN (?)",
managed_enterprises.is_primary_producer.pluck(:id))
Spree::LineItem.where(id: editable | produced)
end
@@ -108,15 +116,18 @@ module OpenFoodNetwork
permitted_enterprise_products_ids = product_ids_supplied_by(
related_enterprises_granting(:manage_products)
)
Spree::Product.where('spree_products.id IN (?)', managed_enterprise_products_ids | permitted_enterprise_products_ids)
Spree::Product.where("spree_products.id IN (?)",
managed_enterprise_products_ids | permitted_enterprise_products_ids)
end
def visible_products
managed_enterprise_products_ids = managed_enterprise_products.pluck :id
permitted_enterprise_products_ids = product_ids_supplied_by(
related_enterprises_granting(:manage_products) | related_enterprises_granting(:add_to_order_cycle)
related_enterprises_granting(:manage_products) \
| related_enterprises_granting(:add_to_order_cycle)
)
Spree::Product.where('spree_products.id IN (?)', managed_enterprise_products_ids | permitted_enterprise_products_ids)
Spree::Product.where("spree_products.id IN (?)",
managed_enterprise_products_ids | permitted_enterprise_products_ids)
end
def managed_product_enterprises
@@ -128,11 +139,15 @@ module OpenFoodNetwork
end
def editable_schedules
Schedule.joins(:order_cycles).where(order_cycles: { id: OrderCycle.managed_by(@user).pluck(:id) }).select("DISTINCT schedules.*")
Schedule.joins(:order_cycles).
where(order_cycles: { id: OrderCycle.managed_by(@user).pluck(:id) }).
select("DISTINCT schedules.*")
end
def visible_schedules
Schedule.joins(:order_cycles).where(order_cycles: { id: OrderCycle.accessible_by(@user).pluck(:id) }).select("DISTINCT schedules.*")
Schedule.joins(:order_cycles).
where(order_cycles: { id: OrderCycle.accessible_by(@user).pluck(:id) }).
select("DISTINCT schedules.*")
end
def editable_subscriptions
@@ -167,7 +182,8 @@ module OpenFoodNetwork
managed_enterprise_ids = managed_enterprises.pluck(:id)
granting_enterprise_ids = related_enterprises_granting(permission)
granted_enterprise_ids = related_enterprises_granted(permission)
Enterprise.where(id: managed_enterprise_ids | granting_enterprise_ids | granted_enterprise_ids)
Enterprise.
where(id: managed_enterprise_ids | granting_enterprise_ids | granted_enterprise_ids)
end
end
@@ -187,7 +203,8 @@ module OpenFoodNetwork
with_permission(permission).
pluck(:parent_id)
(options[:scope] || Enterprise).where('enterprises.id IN (?)', parent_ids).select("enterprises.id")
(options[:scope] || Enterprise).where('enterprises.id IN (?)', parent_ids).
select("enterprises.id")
end
def related_enterprises_granted(permission, options = {})
@@ -196,7 +213,8 @@ module OpenFoodNetwork
with_permission(permission).
pluck(:child_id)
(options[:scope] || Enterprise).where('enterprises.id IN (?)', child_ids).select("enterprises.id")
(options[:scope] || Enterprise).where('enterprises.id IN (?)', child_ids).
select("enterprises.id")
end
def managed_enterprise_products