Skip to content

Commit

Permalink
feat: Update delete_user to append unique hash (#8472)
Browse files Browse the repository at this point in the history
* Update delete_user to append unique hash

* Switched to own-generated unique suffix

* Make products_collection mandatory
  • Loading branch information
john-gom committed May 29, 2023
1 parent de2fe3e commit a510fec
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
1 change: 1 addition & 0 deletions cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ requires 'LWP::UserAgent'; # libwww-perl
requires 'Image::Magick'; # libimage-magick-perl
requires 'XML::Encoding'; # libxml-encoding-perl
requires 'MIME::Lite'; # libmime-lite-perl
requires 'MIME::Base32';
requires 'Cache::Memcached::Fast'; #libcache-memcached-fast-perl
requires 'JSON'; # libjson-perl
requires 'JSON::PP'; # libjson-pp-perl
Expand Down
24 changes: 8 additions & 16 deletions lib/ProductOpener/Products.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1844,15 +1844,16 @@ we can rename it to a generic user account like openfoodfacts-contributors.
# Fields that contain usernames
my @users_fields = qw(editors_tags photographers_tags informers_tags correctors_tags checkers_tags weighers_tags);

sub replace_user_id_in_product ($product_id, $user_id, $new_user_id) {
sub replace_user_id_in_product ($product_id, $user_id, $new_user_id, $products_collection) {

my $path = product_path_from_id($product_id);

# List of changes

my $changes_ref = retrieve("$data_root/products/$path/changes.sto");
if (not defined $changes_ref) {
$changes_ref = [];
$log->warn("replace_user_id_in_products - no changes file found for " . $product_id);
return;
}

my $most_recent_product_ref;
Expand Down Expand Up @@ -1925,7 +1926,6 @@ sub replace_user_id_in_product ($product_id, $user_id, $new_user_id) {
}

if ((defined $most_recent_product_ref) and (not $most_recent_product_ref->{deleted})) {
my $products_collection = get_products_collection();
$products_collection->replace_one({"_id" => $most_recent_product_ref->{_id}},
$most_recent_product_ref, {upsert => 1});
}
Expand Down Expand Up @@ -1964,18 +1964,9 @@ sub find_and_replace_user_id_in_products ($user_id, $new_user_id) {

my $query_ref = {'$or' => $or};

my $products_collection = get_products_collection();

my $count = $products_collection->count_documents($query_ref);

$log->info(
"find_and_replace_user_id_in_products - matching products",
{user_id => $user_id, new_user_id => $new_user_id, count => $count}
) if $log->is_info();

# wait to give time to display the product count
sleep(2) if $log->is_debug();
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);

Expand All @@ -1987,10 +1978,11 @@ sub find_and_replace_user_id_in_products ($user_id, $new_user_id) {
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 => $product_id, product_id => $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);
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
4 changes: 3 additions & 1 deletion lib/ProductOpener/Users.pm
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ use Crypt::PasswdMD5 qw(unix_md5_crypt);
use Math::Random::Secure qw(irand);
use Crypt::ScryptKDF qw(scrypt_hash scrypt_hash_verify);
use Log::Any qw($log);
use MIME::Base32 qw(encode_base32);

my @user_groups = qw(producer database app bot moderator pro_moderator);

Expand Down Expand Up @@ -193,7 +194,8 @@ Takes in the $user_ref of the user to be deleted
sub delete_user ($user_ref) {

my $userid = get_string_id_for_lang("no_language", $user_ref->{userid});
my $new_userid = "openfoodfacts-contributors";
# Suffix is a combination of seconds since epoch plus a 16 bit random number
my $new_userid = "anonymous-" . lc(encode_base32(pack('LS', time(), rand(65536))));

$log->info("delete_user", {userid => $userid, new_userid => $new_userid}) if $log->is_info();

Expand Down

0 comments on commit a510fec

Please sign in to comment.