Adapt permissions.rb and order_cycle_permissions to rails 4

This commit is contained in:
luisramos0
2019-04-06 23:42:37 +01:00
parent a82b1d8129
commit a57a93d414
3 changed files with 74 additions and 75 deletions

View File

@@ -14,61 +14,61 @@ module OpenFoodNetwork
def visible_enterprises
return Enterprise.where("1=0") if @coordinator.blank?
if managed_enterprises.include? @coordinator
coordinator_permitted = [@coordinator]
all_active = []
coordinator_permitted_ids = [@coordinator]
all_active_ids = []
if @coordinator.sells == "any"
# If the coordinator sells any, relationships come into play
related_enterprises_granting(:add_to_order_cycle, to: [@coordinator]).pluck(:id).each do |enterprise_id|
coordinator_permitted << 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
all_active = @order_cycle.suppliers.pluck(:id) | @order_cycle.distributors.pluck(:id)
all_active_ids = @order_cycle.suppliers.pluck(:id) | @order_cycle.distributors.pluck(:id)
end
Enterprise.where(id: coordinator_permitted | all_active)
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 = related_enterprises_granting(:add_to_order_cycle, to: [@coordinator], scope: managed_participating_enterprises ).pluck(:id)
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 = related_enterprises_granted(:add_to_order_cycle, by: managed_participating_producers, scope: @order_cycle.distributors).pluck(:id)
hubs_permitted_ids = related_enterprises_granted(:add_to_order_cycle, by: managed_participating_producers.select(:id), scope: @order_cycle.distributors)
# Any hubs in this OC that have granted P-OC to producers I manage in this OC
hubs_permitting = related_enterprises_granting(:add_to_order_cycle, to: managed_participating_producers, scope: @order_cycle.distributors).pluck(:id)
hubs_permitting_ids = related_enterprises_granting(:add_to_order_cycle, to: managed_participating_producers.select(: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 = related_enterprises_granted(:add_to_order_cycle, by: managed_participating_hubs, scope: @order_cycle.suppliers).pluck(:id)
producers_permitted_ids = related_enterprises_granted(:add_to_order_cycle, by: managed_participating_hubs.select(:id), scope: @order_cycle.suppliers)
# Any producers in this OC that have granted P-OC to hubs I manage in this OC
producers_permitting = related_enterprises_granting(:add_to_order_cycle, to: managed_participating_hubs, scope: @order_cycle.suppliers).pluck(:id)
producers_permitting_ids = related_enterprises_granting(:add_to_order_cycle, to: managed_participating_hubs.select(:id), scope: @order_cycle.suppliers)
managed_active = []
hubs_active = []
managed_active_ids = []
hubs_active_ids = []
producers_active = []
if @order_cycle
# TODO: Remove this when all P-OC are sorted out
# Any enterprises that I manage that are already in the order_cycle
managed_active = managed_participating_enterprises.pluck(:id)
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)
variants = Spree::Variant.joins(:product).where('spree_products.supplier_id IN (?)', managed_enterprises.is_primary_producer.select(:id))
active_exchanges = @order_cycle.
exchanges.outgoing.with_any_variant(variants.select("spree_variants.id"))
hubs_active = 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
variants = Spree::Variant.joins(:exchanges).where("exchanges.receiver_id IN (?) AND exchanges.order_cycle_id = (?) AND exchanges.incoming = 'f'", managed_participating_hubs, @order_cycle).pluck(:id).uniq
products = Spree::Product.joins(:variants_including_master).where("spree_variants.id IN (?)", variants).pluck(:id).uniq
producers_active = Enterprise.joins(:supplied_products).where("spree_products.id IN (?)", products).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(:id), @order_cycle).pluck(:id).uniq
product_ids = Spree::Product.joins(:variants_including_master).where("spree_variants.id IN (?)", variant_ids).pluck(:id).uniq
active_producer_ids = Enterprise.joins(:supplied_products).where("spree_products.id IN (?)", product_ids).pluck(:id).uniq
end
ids = managed_permitted | hubs_permitted | hubs_permitting | producers_permitted | producers_permitting | managed_active | hubs_active | producers_active
ids = managed_permitted_ids | hubs_permitted_ids | hubs_permitting_ids | producers_permitted_ids | producers_permitting_ids | managed_active_ids | hubs_active_ids | active_producer_ids
Enterprise.where(id: ids.sort )
Enterprise.where(id: ids)
end
end
@@ -129,8 +129,8 @@ module OpenFoodNetwork
end
# Any variants of any producers that have granted the hub P-OC
producers = related_enterprises_granting(:add_to_order_cycle, to: [hub], scope: Enterprise.is_primary_producer)
permitted_variants = Spree::Variant.joins(:product).where('spree_products.supplier_id IN (?)', producers)
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)
@@ -144,13 +144,13 @@ module OpenFoodNetwork
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
producers = related_enterprises_granting(:add_to_order_cycle, to: [hub], scope: managed_participating_producers)
permitted_variants = Spree::Variant.joins(:product).where('spree_products.supplier_id IN (?)', producers)
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
active_variants = Spree::Variant.joins(:exchanges, :product).
where("exchanges.receiver_id = (?) AND spree_products.supplier_id IN (?) AND incoming = 'f'", hub, managed_enterprises.is_primary_producer)
where("exchanges.receiver_id = (?) AND spree_products.supplier_id IN (?) AND incoming = 'f'", hub.id, managed_enterprises.is_primary_producer.select(:id))
Spree::Variant.where(id: permitted_variants | active_variants)
end
@@ -168,8 +168,8 @@ module OpenFoodNetwork
end
# Any variants of any producers that have granted the hub P-OC
producers = related_enterprises_granting(:add_to_order_cycle, to: [hub], scope: Enterprise.is_primary_producer)
permitted_variants = Spree::Variant.joins(:product).where('spree_products.supplier_id IN (?)', producers)
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)
@@ -183,11 +183,11 @@ module OpenFoodNetwork
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], 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_producers = related_enterprises_granting(:add_to_order_cycle, to: [hub], scope: granted_producers)
permitted_variants = Spree::Variant.joins(:product).where('spree_products.supplier_id IN (?)', granting_producers)
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
@@ -223,38 +223,38 @@ 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
producers = related_enterprises_granting(:add_to_order_cycle, to: hubs, scope: Enterprise.is_primary_producer).pluck :id
permitted_exchanges = @order_cycle.exchanges.incoming.where(sender_id: producers).pluck :id
producer_ids = related_enterprises_granting(:add_to_order_cycle, to: hubs.select(: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
variants = Spree::Variant.joins(:exchanges).where("exchanges.receiver_id IN (?) AND exchanges.order_cycle_id = (?) AND exchanges.incoming = 'f'", hubs, @order_cycle).pluck(:id).uniq
products = Spree::Product.joins(:variants_including_master).where("spree_variants.id IN (?)", variants).pluck(:id).uniq
producers = Enterprise.joins(:supplied_products).where("spree_products.id IN (?)", products).pluck(:id).uniq
active_exchanges = @order_cycle.exchanges.incoming.where(sender_id: producers).pluck :id
variant_ids = Spree::Variant.joins(:exchanges).where("exchanges.receiver_id IN (?) AND exchanges.order_cycle_id = (?) AND exchanges.incoming = 'f'", hubs.select(: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_exchanges | active_exchanges
permitted_exchange_ids | active_exchange_ids
end
def order_cycle_exchange_ids_distributing_my_variants
# Find my producers in this order cycle
producers = managed_participating_producers.pluck :id
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
hubs = related_enterprises_granted(:add_to_order_cycle, by: producers, scope: Enterprise.is_hub)
permitted_exchanges = @order_cycle.exchanges.outgoing.where(receiver_id: hubs).pluck :id
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
# 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 producers, any outgoing exchanges with their variants
variants = Spree::Variant.
joins(:product).where('spree_products.supplier_id IN (?)', producers)
joins(:product).where('spree_products.supplier_id IN (?)', producer_ids)
active_exchanges = @order_cycle.
exchanges.outgoing.with_any_variant(variants.select("spree_variants.id")).pluck :id
permitted_exchanges | active_exchanges
permitted_exchange_ids | active_exchange_ids
end
end
end

View File

@@ -66,12 +66,12 @@ module OpenFoodNetwork
# 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_distributors = related_enterprises_granted(:add_to_order_cycle, by: managed_enterprises.is_primary_producer)
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_distributors,
related_enterprises_granting(:add_to_order_cycle, to: granted_distributors).merge(managed_enterprises.is_primary_producer)
granted_distributor_ids,
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)
@@ -105,17 +105,17 @@ module OpenFoodNetwork
def editable_products
managed_enterprise_products_ids = managed_enterprise_products.pluck :id
permitted_enterprise_products_ids = products_supplied_by(
related_enterprises_granting(:manage_products).pluck(:id)
).pluck :id
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)
end
def visible_products
managed_enterprise_products_ids = managed_enterprise_products.pluck :id
permitted_enterprise_products_ids = products_supplied_by(
related_enterprises_granting(:manage_products).pluck(:id) | related_enterprises_granting(:add_to_order_cycle).pluck(:id)
).pluck :id
permitted_enterprise_products_ids = product_ids_supplied_by(
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)
end
@@ -154,7 +154,7 @@ module OpenFoodNetwork
Enterprise.scoped
else
managed_enterprise_ids = managed_enterprises.pluck :id
permitting_enterprise_ids = related_enterprises_granting(permission).pluck :id
permitting_enterprise_ids = related_enterprises_granting(permission)
Enterprise.where('id IN (?)', managed_enterprise_ids + permitting_enterprise_ids)
end
@@ -164,10 +164,10 @@ module OpenFoodNetwork
if admin?
Enterprise.scoped
else
managed = managed_enterprises.pluck(:id)
granting = related_enterprises_granting(permission).pluck(:id)
granted = related_enterprises_granted(permission).pluck(:id)
Enterprise.where(id: managed | granting | granted)
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)
end
end
@@ -183,8 +183,7 @@ module OpenFoodNetwork
def related_enterprises_granting(permission, options = {})
parent_ids = EnterpriseRelationship.
# this options[:to].id needs to be verified/tested
permitting(options[:to].select(:id) || managed_enterprises.select(:id)).
permitting(options[:to] || managed_enterprises.select(:id)).
with_permission(permission).
pluck(:parent_id)
@@ -193,19 +192,19 @@ module OpenFoodNetwork
def related_enterprises_granted(permission, options = {})
child_ids = EnterpriseRelationship.
permitted_by(options[:by].select(:id) || managed_enterprises.select(:id)).
permitted_by(options[:by] || managed_enterprises.select(:id)).
with_permission(permission).
pluck(:child_id)
(options[:scope] || Enterprise).where('enterprises.id IN (?)', child_ids)
(options[:scope] || Enterprise).where('enterprises.id IN (?)', child_ids).select(:id)
end
def managed_enterprise_products
Spree::Product.managed_by(@user)
end
def products_supplied_by(suppliers)
Spree::Product.where('supplier_id IN (?)', suppliers)
def product_ids_supplied_by(supplier_ids)
Spree::Product.where('supplier_id IN (?)', supplier_ids).pluck(:id)
end
end
end

View File

@@ -24,7 +24,7 @@ module OpenFoodNetwork
it "returns only my managed enterprises any that have granting them P-OC" do
expect(permissions).to receive(:managed_enterprises) { Enterprise.where(id: e1) }
expect(permissions).to receive(:related_enterprises_granting).with(:some_permission) { Enterprise.where(id: e3) }
expect(permissions).to receive(:related_enterprises_granting).with(:some_permission) { Enterprise.where(id: e3).select(:id) }
expect(permissions.send(:managed_and_related_enterprises_granting, :some_permission)).to match_array [e1, e3]
end
end
@@ -46,8 +46,8 @@ module OpenFoodNetwork
it "returns only my managed enterprises any that have granting them P-OC" do
expect(permissions).to receive(:managed_enterprises) { Enterprise.where(id: e1) }
expect(permissions).to receive(:related_enterprises_granting).with(:some_permission) { Enterprise.where(id: e3) }
expect(permissions).to receive(:related_enterprises_granted).with(:some_permission) { Enterprise.where(id: e4) }
expect(permissions).to receive(:related_enterprises_granting).with(:some_permission) { Enterprise.where(id: e3).select(:id) }
expect(permissions).to receive(:related_enterprises_granted).with(:some_permission) { Enterprise.where(id: e4).select(:id) }
expect(permissions.send(:managed_and_related_enterprises_with, :some_permission)).to match_array [e1, e3, e4]
end
end
@@ -177,7 +177,7 @@ module OpenFoodNetwork
before do
allow(permissions).to receive(:managed_enterprise_products) { Spree::Product.where('1=0') }
allow(permissions).to receive(:related_enterprises_granting).with(:manage_products) { Enterprise.where("1=0") }
allow(permissions).to receive(:related_enterprises_granting).with(:manage_products) { Enterprise.where("1=0").select(:id) }
end
it "returns products produced by managed enterprises" do
@@ -187,7 +187,7 @@ module OpenFoodNetwork
it "returns products produced by permitted enterprises" do
allow(permissions).to receive(:related_enterprises_granting).
with(:manage_products) { Enterprise.where(id: p2.supplier) }
with(:manage_products) { Enterprise.where(id: p2.supplier).select(:id) }
expect(permissions.editable_products).to eq([p2])
end
end
@@ -199,8 +199,8 @@ module OpenFoodNetwork
before do
allow(permissions).to receive(:managed_enterprise_products) { Spree::Product.where("1=0") }
allow(permissions).to receive(:related_enterprises_granting).with(:manage_products) { Enterprise.where("1=0") }
allow(permissions).to receive(:related_enterprises_granting).with(:add_to_order_cycle) { Enterprise.where("1=0") }
allow(permissions).to receive(:related_enterprises_granting).with(:manage_products) { Enterprise.where("1=0").select(:id) }
allow(permissions).to receive(:related_enterprises_granting).with(:add_to_order_cycle) { Enterprise.where("1=0").select(:id) }
end
it "returns products produced by managed enterprises" do
@@ -210,13 +210,13 @@ module OpenFoodNetwork
it "returns products produced by enterprises that have granted manage products" do
allow(permissions).to receive(:related_enterprises_granting).
with(:manage_products) { Enterprise.where(id: p2.supplier) }
with(:manage_products) { Enterprise.where(id: p2.supplier).select(:id) }
expect(permissions.visible_products).to eq([p2])
end
it "returns products produced by enterprises that have granted P-OC" do
allow(permissions).to receive(:related_enterprises_granting).
with(:add_to_order_cycle) { Enterprise.where(id: p3.supplier) }
with(:add_to_order_cycle) { Enterprise.where(id: p3.supplier).select(:id) }
expect(permissions.visible_products).to eq([p3])
end
end
@@ -240,12 +240,12 @@ module OpenFoodNetwork
let!(:er) { create(:enterprise_relationship, parent: e1, child: e2, permissions_list: [permission]) }
it "returns the enterprises" do
allow(permissions).to receive(:managed_enterprises) { e2 }
allow(permissions).to receive(:managed_enterprises) { Enterprise.where(id: e2) }
expect(permissions.send(:related_enterprises_granting, permission)).to eq([e1])
end
it "returns an empty array when there are none" do
allow(permissions).to receive(:managed_enterprises) { e1 }
allow(permissions).to receive(:managed_enterprises) { Enterprise.where(id: e1) }
expect(permissions.send(:related_enterprises_granting, permission)).to eq([])
end
end
@@ -253,7 +253,7 @@ module OpenFoodNetwork
describe "finding enterprises that are managed or with a particular permission" do
before do
allow(permissions).to receive(:managed_enterprises) { Enterprise.where('1=0') }
allow(permissions).to receive(:related_enterprises_granting) { Enterprise.where('1=0') }
allow(permissions).to receive(:related_enterprises_granting) { Enterprise.where('1=0').select(:id) }
allow(permissions).to receive(:admin?) { false }
end
@@ -264,7 +264,7 @@ module OpenFoodNetwork
it "returns permitted enterprises" do
expect(permissions).to receive(:related_enterprises_granting).with(permission).
and_return(Enterprise.where(id: e2))
and_return(Enterprise.where(id: e2).select(:id))
expect(permissions.send(:managed_and_related_enterprises_granting, permission)).to eq([e2])
end
end