From 08d16b06780e37af82d72ac7d5bb48dc4aba8c99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Gigandet?= Date: Fri, 19 Apr 2024 17:04:52 +0200 Subject: [PATCH] fix: add cc field to auth.pl (#10152) add cc field requested by @monsieurtanuki in openfoodfacts/openfoodfacts-dart#903 note that cc can also be "world" also removed some fields if their value is undef or empty --- cgi/auth.pl | 32 +++++++++++++------ ...uthenticated-with-userid-and-password.json | 1 + 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/cgi/auth.pl b/cgi/auth.pl index bb03ddd0982a3..53c3322785a13 100755 --- a/cgi/auth.pl +++ b/cgi/auth.pl @@ -30,12 +30,14 @@ use ProductOpener::HTTP qw/write_cors_headers/; use ProductOpener::Users qw/$User_id %User is_admin_user/; use ProductOpener::Lang qw/:all/; +use ProductOpener::Tags qw/country_to_cc/; use Apache2::Const -compile => qw(OK); use CGI qw/:cgi :form escapeHTML/; use URI::Escape::XS; use Encode; use Log::Any qw($log); +use JSON::PP; $log->info('start') if $log->is_info(); @@ -48,21 +50,31 @@ $status = 200; # Return basic data about the user + + my $user_ref = { + name => $User{name}, + moderator => $User{moderator} ? 1 : 0, + admin => is_admin_user($User_id) ? 1 : 0, + }; + + # Add some fields if we have defined and non empty values for them + if ($User{preferred_language}) { + $user_ref->{preferred_language} = $User{preferred_language}; + } + if ($User{country}) { + $user_ref->{country} = $User{country}; + my $cc = country_to_cc($User{country}); + if ($cc) { + $user_ref->{cc} = $cc; + } + } + $response_ref = { status => 1, status_verbose => 'user signed-in', user_id => $User_id, - user => { - name => $User{name}, - preferred_language => $User{preferred_language}, - country => $User{country}, - moderator => $User{moderator} ? 1 : 0, - admin => is_admin_user($User_id) ? 1 : 0, - }, + user => $user_ref, }; - - use JSON::PP; - } else { $status = 403; diff --git a/tests/integration/expected_test_results/auth/auth-authenticated-with-userid-and-password.json b/tests/integration/expected_test_results/auth/auth-authenticated-with-userid-and-password.json index e662895e78022..ea5b82e575a77 100644 --- a/tests/integration/expected_test_results/auth/auth-authenticated-with-userid-and-password.json +++ b/tests/integration/expected_test_results/auth/auth-authenticated-with-userid-and-password.json @@ -3,6 +3,7 @@ "status_verbose" : "user signed-in", "user" : { "admin" : 0, + "cc" : "us", "country" : "en:united-states", "moderator" : 0, "name" : "Test",