From 468184e7ec6bccfc343c3300baa61dce3d62d419 Mon Sep 17 00:00:00 2001 From: john-gom <116556069+john-gom@users.noreply.github.com> Date: Wed, 31 May 2023 19:14:43 +0100 Subject: [PATCH] fix: Iterate over both product collections on user delete (#8476) Iterate over both product collections --- lib/ProductOpener/Products.pm | 27 +++++++++++---------- scripts/make_user_moderator.pl | 44 ++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 13 deletions(-) create mode 100644 scripts/make_user_moderator.pl diff --git a/lib/ProductOpener/Products.pm b/lib/ProductOpener/Products.pm index 92f5c826bd9a8..55489bbec5238 100644 --- a/lib/ProductOpener/Products.pm +++ b/lib/ProductOpener/Products.pm @@ -1965,25 +1965,26 @@ sub find_and_replace_user_id_in_products ($user_id, $new_user_id) { my $query_ref = {'$or' => $or}; - my $products_collection = get_products_collection({timeout => 60 * 60 * 1000}); - my $count = 0; - my $cursor = $products_collection->query($query_ref)->fields({_id => 1, code => 1, owner => 1}); - $cursor->immortal(1); + for (my $obsolete = 0; $obsolete <= 1; $obsolete++) { + my $products_collection = get_products_collection({obsolete => $obsolete, timeout => 60 * 60 * 1000}); + my $cursor = $products_collection->query($query_ref)->fields({_id => 1, code => 1, owner => 1}); + $cursor->immortal(1); - while (my $product_ref = $cursor->next) { + while (my $product_ref = $cursor->next) { - my $product_id = $product_ref->{_id}; + my $product_id = $product_ref->{_id}; - # Ignore bogus product that might have been saved in the database - next if (not defined $product_id) or ($product_id eq ""); + # Ignore bogus product that might have been saved in the database + next if (not defined $product_id) or ($product_id eq ""); - $log->info("find_and_replace_user_id_in_products - product_id", - {user_id => $user_id, new_user_id => $new_user_id, product_id => $product_id}) - if $log->is_info(); + $log->info("find_and_replace_user_id_in_products - product_id", + {user_id => $user_id, new_user_id => $new_user_id, product_id => $product_id}) + if $log->is_info(); - replace_user_id_in_product($product_id, $user_id, $new_user_id, $products_collection); - $count++; + replace_user_id_in_product($product_id, $user_id, $new_user_id, $products_collection); + $count++; + } } $log->info("find_and_replace_user_id_in_products - done", diff --git a/scripts/make_user_moderator.pl b/scripts/make_user_moderator.pl new file mode 100644 index 0000000000000..ced9b3d7dfe82 --- /dev/null +++ b/scripts/make_user_moderator.pl @@ -0,0 +1,44 @@ +#!/usr/bin/perl -w + +# This file is part of Product Opener. +# +# Product Opener +# Copyright (C) 2011-2023 Association Open Food Facts +# Contact: contact@openfoodfacts.org +# Address: 21 rue des Iles, 94100 Saint-Maur des Fossés, France +# +# Product Opener is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +use Modern::Perl '2017'; +use utf8; + +use CGI::Carp qw(fatalsToBrowser); + +use ProductOpener::Config qw/:all/; +use ProductOpener::Store qw/:all/; +use ProductOpener::Index qw/:all/; +use ProductOpener::Display qw/:all/; +use ProductOpener::Images qw/:all/; +use ProductOpener::Users qw/:all/; +use ProductOpener::Mail qw/:all/; +use ProductOpener::Lang qw/:all/; + +use CGI qw/:cgi :form escapeHTML/; +use URI::Escape::XS; +use Encode; + +my $userid = $ARGV[0]; +my $user_ref = retrieve("$data_root/users/$userid.sto"); +$user_ref->{moderator} = 1; +store("$data_root/users/$userid.sto", $user_ref);