Skip to content

Commit

Permalink
fix: add cc field to auth.pl (#10152)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
stephanegigandet committed Apr 19, 2024
1 parent 2e7ef0f commit 08d16b0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
32 changes: 22 additions & 10 deletions cgi/auth.pl
Expand Up @@ -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();

Expand All @@ -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;
Expand Down
Expand Up @@ -3,6 +3,7 @@
"status_verbose" : "user signed-in",
"user" : {
"admin" : 0,
"cc" : "us",
"country" : "en:united-states",
"moderator" : 0,
"name" : "Test",
Expand Down

0 comments on commit 08d16b0

Please sign in to comment.