Skip to content

Commit

Permalink
fix: Iterate over both product collections on user delete (openfoodfa…
Browse files Browse the repository at this point in the history
…cts#8476)

Iterate over both product collections
  • Loading branch information
john-gom authored and MonalikaPatnaik committed May 31, 2023
1 parent fd398bd commit 468184e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 13 deletions.
27 changes: 14 additions & 13 deletions lib/ProductOpener/Products.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
44 changes: 44 additions & 0 deletions scripts/make_user_moderator.pl
Original file line number Diff line number Diff line change
@@ -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: [email protected]
# 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 <https://www.gnu.org/licenses/>.

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);

0 comments on commit 468184e

Please sign in to comment.