mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Adding column 'permission_revoked_at' to VOs
And a migration to flag any VOs which should not be permitted, update previous migration so that new enterprise relationships don't try to revoke variant overrides
This commit is contained in:
@@ -2,20 +2,26 @@ class GrantExplicitVariantOverridePermissions < ActiveRecord::Migration
|
||||
def up
|
||||
hubs = Enterprise.is_distributor
|
||||
|
||||
hubs.each do |hub|
|
||||
next if hub.owner.admin?
|
||||
explicitly_granting_producer_ids = hub.relationships_as_child
|
||||
.with_permission(:create_variant_overrides).map(&:parent_id)
|
||||
begin
|
||||
EnterpriseRelationship.skip_callback :save, :after, :apply_variant_override_permissions
|
||||
|
||||
managed_producer_ids = Enterprise.managed_by(hub.owner).is_primary_producer.pluck(:id)
|
||||
implicitly_granting_producer_ids = managed_producer_ids - explicitly_granting_producer_ids - [hub.id]
|
||||
hubs.each do |hub|
|
||||
next if hub.owner.admin?
|
||||
explicitly_granting_producer_ids = hub.relationships_as_child
|
||||
.with_permission(:create_variant_overrides).map(&:parent_id)
|
||||
|
||||
# create explicit VO permissions for producers currently granting implicit permission
|
||||
Enterprise.where(id: implicitly_granting_producer_ids).each do |producer|
|
||||
relationship = producer.relationships_as_parent.find_or_initialize_by_child_id(hub.id)
|
||||
permission = relationship.permissions.find_or_initialize_by_name(:create_variant_overrides)
|
||||
relationship.save! unless permission.persisted?
|
||||
managed_producer_ids = Enterprise.managed_by(hub.owner).is_primary_producer.pluck(:id)
|
||||
implicitly_granting_producer_ids = managed_producer_ids - explicitly_granting_producer_ids - [hub.id]
|
||||
|
||||
# create explicit VO permissions for producers currently granting implicit permission
|
||||
Enterprise.where(id: implicitly_granting_producer_ids).each do |producer|
|
||||
relationship = producer.relationships_as_parent.find_or_initialize_by_child_id(hub.id)
|
||||
permission = relationship.permissions.find_or_initialize_by_name(:create_variant_overrides)
|
||||
relationship.save! unless permission.persisted?
|
||||
end
|
||||
end
|
||||
ensure
|
||||
EnterpriseRelationship.set_callback :save, :after, :apply_variant_override_permissions
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
class AddPermissionRevokedAtToVariantOverrides < ActiveRecord::Migration
|
||||
def up
|
||||
add_column :variant_overrides, :permission_revoked_at, :datetime, default: nil
|
||||
|
||||
variant_override_hubs = Enterprise.where(id: VariantOverride.all.map(&:hub_id).uniq)
|
||||
|
||||
variant_override_hubs.each do |hub|
|
||||
permitting_producer_ids = hub.relationships_as_child
|
||||
.with_permission(:create_variant_overrides).map(&:parent_id)
|
||||
|
||||
variant_overrides_with_revoked_permissions = VariantOverride.for_hubs(hub)
|
||||
.joins(variant: :product).where("spree_products.supplier_id NOT IN (?)", permitting_producer_ids)
|
||||
|
||||
variant_overrides_with_revoked_permissions.update_all(permission_revoked_at: Time.now)
|
||||
end
|
||||
end
|
||||
|
||||
def down
|
||||
remove_column :variant_overrides, :permission_revoked_at
|
||||
end
|
||||
end
|
||||
19
db/schema.rb
19
db/schema.rb
@@ -11,7 +11,7 @@
|
||||
#
|
||||
# It's strongly recommended to check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(:version => 20160224034034) do
|
||||
ActiveRecord::Schema.define(:version => 20160224230143) do
|
||||
|
||||
create_table "account_invoices", :force => true do |t|
|
||||
t.integer "user_id", :null => false
|
||||
@@ -1166,14 +1166,15 @@ ActiveRecord::Schema.define(:version => 20160224034034) do
|
||||
add_index "tags", ["name"], :name => "index_tags_on_name", :unique => true
|
||||
|
||||
create_table "variant_overrides", :force => true do |t|
|
||||
t.integer "variant_id", :null => false
|
||||
t.integer "hub_id", :null => false
|
||||
t.decimal "price", :precision => 8, :scale => 2
|
||||
t.integer "count_on_hand"
|
||||
t.integer "default_stock"
|
||||
t.boolean "resettable"
|
||||
t.string "sku"
|
||||
t.boolean "on_demand"
|
||||
t.integer "variant_id", :null => false
|
||||
t.integer "hub_id", :null => false
|
||||
t.decimal "price", :precision => 8, :scale => 2
|
||||
t.integer "count_on_hand"
|
||||
t.integer "default_stock"
|
||||
t.boolean "resettable"
|
||||
t.string "sku"
|
||||
t.boolean "on_demand"
|
||||
t.datetime "permission_revoked_at"
|
||||
end
|
||||
|
||||
add_index "variant_overrides", ["variant_id", "hub_id"], :name => "index_variant_overrides_on_variant_id_and_hub_id"
|
||||
|
||||
Reference in New Issue
Block a user