-
-
Notifications
You must be signed in to change notification settings - Fork 371
/
Users.pm
1127 lines (875 loc) · 32.5 KB
/
Users.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file is part of Product Opener.
#
# Product Opener
# Copyright (C) 2011-2020 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 <http://www.gnu.org/licenses/>.
=head1 NAME
ProductOpener::Users - manage user profiles and sessions
=head1 SYNOPSIS
C<ProductOpener::Users> contains functions to create and edit user profiles
and to manage user sessions.
use ProductOpener::Users qw/:all/;
[..]
init_user();
=head1 DESCRIPTION
[..]
=cut
package ProductOpener::Users;
use utf8;
use Modern::Perl '2017';
use Exporter qw< import >;
BEGIN
{
use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS);
@EXPORT_OK = qw(
%User
$User_id
%Org
$Org_id
$Owner_id
$cookie
&display_user_form
&check_user_form
&process_user_form
&check_edit_owner
&init_user
&save_user
&userpath
&create_user
&create_password_hash
&check_password_hash
&check_session
&generate_token
); # symbols to export on request
%EXPORT_TAGS = (all => [@EXPORT_OK]);
}
use vars @EXPORT_OK ;
use ProductOpener::Store qw/:all/;
use ProductOpener::Config qw/:all/;
use ProductOpener::Mail qw/:all/;
use ProductOpener::Lang qw/:all/;
use ProductOpener::Cache qw/:all/;
use ProductOpener::Display qw/:all/;
use ProductOpener::Orgs qw/:all/;
use ProductOpener::Products qw/:all/;
use CGI qw/:cgi :form escapeHTML/;
use Encode;
use Email::Valid;
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);
my @user_groups = qw(producer database app bot moderator pro_moderator);
sub generate_token {
my $name_length = shift;
my @chars=('a'..'z', 'A'..'Z', 0..9);
return join '',map {$chars[irand @chars]} 1..$name_length;
}
sub create_password_hash($) {
my $password = shift;
return scrypt_hash($password);
}
sub check_password_hash($$) {
my $password = shift;
my $hash = shift;
if ($hash =~ /^\$1\$(?:.*)/) {
if ($hash eq unix_md5_crypt($password, $hash)) {
return 1;
}
else {
return 0;
}
}
else {
return scrypt_hash_verify($password, $hash);
}
}
sub userpath($) {
my $file = shift;
$file =~ s/^(...)(.)/$1\/$2/;
return $file;
}
sub create_user($) {
my $user_ref = shift;
my $name_id = get_string_id_for_lang("no_language", $user_ref->{name});
if (length($name_id) > 3) {
my $i = 1;
my $name_id2 = $name_id;
while (-e "$data_root/users/$name_id2.sto") {
$name_id2 = $name_id . "-" . ++$i;
}
$user_ref->{userid} = $name_id2;
# TODO
# Assign a random password
# Send welcome e-mail + password - Might not be ideal, as passwords should not be sent over insecure channels such as e-mail.
$log->info("creating new user file", { userid => $name_id2 }) if $log->is_info();
store("$data_root/users/$name_id2.sto", $user_ref);
}
return;
}
sub delete_user($) {
my $user_ref = shift;
my $userid = get_string_id_for_lang("no_language", $user_ref->{userid});
my $new_userid = "openfoodfacts-contributors";
$log->info("delete_user", { userid => $userid, new_userid => $new_userid }) if $log->is_info();
# Remove the user file
unlink("$data_root/users/$userid.sto");
# Remove the e-mail
my $emails_ref = retrieve("$data_root/users_emails.sto");
my $email = $user_ref->{email};
if ((defined $email) and ($email =~/\@/)) {
if (defined $emails_ref->{$email}) {
delete $emails_ref->{$email};
store("$data_root/users_emails.sto", $emails_ref);
}
}
# re-assign product edits to openfoodfacts-contributors-[random number]
find_and_replace_user_id_in_products($userid, $new_userid);
}
sub display_user_form($$$) {
my $type = shift;
my $user_ref = shift;
my $scripts_ref = shift;
my $html = '';
$html .= "\n<tr><td>$Lang{name}{$lang}</td><td>"
. textfield(-id=>'name', -name=>'name', -value=>$user_ref->{name}, -size=>80, -autocomplete=>'name', -override=>1) . "</td></tr>"
. "\n<tr><td>$Lang{email}{$lang}</td><td>"
. textfield(-name=>'email', -value=>$user_ref->{email}, -size=>80, -autocomplete=>'email', -type=>'email', -override=>1) . "</td></tr>"
. "\n<tr><td>$Lang{username}{$lang}<br/><span class=\"info\">" . (($type eq 'edit') ? '': $Lang{username_info}{$lang}) . "</span></td><td>"
. (($type eq 'edit') ? $user_ref->{userid} :
( textfield(-id=>'userid', -name=>'userid', -value=>$user_ref->{userid}, -size=>40, -onkeyup=>"normalize_string_value(this)", -autocomplete=>'username'))) . "</td></tr>"
. "\n<tr><td>$Lang{password}{$lang}</td><td>"
. password_field(-name=>'password', -value=>$user_ref->{password}, -autocomplete=>'new-password', -override=>1) . "</td></tr>"
. "\n<tr><td>$Lang{password_confirm}{$lang}</td><td>"
. password_field(-name=>'confirm_password', -value=>$user_ref->{password}, -autocomplete=>'new-password', -override=>1) . "</td></tr>"
;
# Professional account
$html .= "\n<tr><td>" . lang("pro_account") . "</td><td>";
$html .= "<p>" . lang("if_you_work_for_a_producer") . "</p>"
. "<p>" . lang("producers_platform_description_long") . "</p>";
my $teams_display = '';
if ( ( defined $user_ref->{org} ) and ( $user_ref->{org} ne "" ) ) {
# Existing user with an accepted organization
$html .= "<p>"
. sprintf(
lang("this_is_a_pro_account_for_org"),
"<b>" . $user_ref->{org} . "</b>"
) . "</p>";
}
# Pro platform is only for food right now
elsif ((defined $options{product_type}) and ($options{product_type} eq "food")) {
# New user or existing user without an accepted organization
my $pro_checked = '';
my $pro_org_display = 'style="display:none"';
# Check the "pro account" checkbox for register screen on the producers platform
if (((defined $user_ref->{pro}) and ($user_ref->{pro}))
or ((defined $server_options{producers_platform}) and ($type eq "add"))) {
$pro_checked = "checked";
$pro_org_display = "";
$teams_display = 'style="display:none"';
}
$html .= "<input type=\"checkbox\" id=\"pro\" name=\"pro\" $pro_checked>"
. "<label for=\"pro\">"
. "<input type=\"hidden\" name=\"pro_checkbox\" value=\"1\"> "
. lang("this_is_a_pro_account") . "</label>"
. "<div id=\"pro_org\" $pro_org_display>"
. "<p id=\"org-field\">" . lang("producer_or_brand") . separator_before_colon($lc) . ": "
. textfield(-id=>'requested_org', -name=>'requested_org', -value=>$user_ref->{requested_org}, -override=>1)
. "</p>";
my $requested_org_ref = retrieve_org($user_ref->{requested_org});
if ( defined $requested_org_ref ) {
$html .= "<div id=\"existing_org_warning\">"
. "<p>" . sprintf(lang("add_user_existing_org"), org_name($requested_org_ref)) . "</p>"
. "<p>" . lang("add_user_existing_org_pending") . "</p>"
. "<p>" .lang("please_email_producers") . "</p>"
. "</div>";
}
else {
$html .= "<p>" . lang("enter_name_of_org") . "</p>";
}
$html .= "</div>";
$html .= "</td></tr>";
$initjs .= <<JAVASCRIPT
\$('#pro').change(function() {
if (\$(this).prop('checked')) {
\$('#pro_org').show();
\$('#tr_teams').hide();
} else {
\$('#pro_org').hide();
\$('#tr_teams').show();
}
\$(document).foundation('equalizer', 'reflow');
});
JAVASCRIPT
;
}
# On the public web site, but not on the producers platform, offer to be part of a team
if (not ((defined $server_options{private_products}) and ($server_options{private_products}))) {
$html .= "\n<tr id=\"tr_teams\" $teams_display><td>" . lang("teams") . " (" . lang("optional") . ")</td><td><p>"
. lang("teams_description") . "</p><p>" . lang("teams_names_warning") . "</p>"
. '<div class="row">';
for (my $i = 1; $i <= 3; $i++) {
$html .= "\n<div class=\"small-3 large-2 xlarge-1 columns\">" . sprintf(lang("team_s"), $i) . lang("sep") . ":</div><div class=\"small-9 large-10 xlarge-11 columns\">"
. textfield(-name=>"team_" . $i, -value=>$user_ref->{"team_" . $i}, -style=>"max-width:20rem", -override=>1, -onkeyup=>"normalize_string_value(this)") . "</div>";
}
$html .= "</div></td></tr>";
}
${$scripts_ref} .= <<SCRIPT
<script type="text/javascript">
function normalize_string_value(inputfield) {
var value = inputfield.value.toLowerCase();
value = value.replace(new RegExp(" ", 'g'),"-");
value = value.replace(new RegExp("[àáâãäå]", 'g'),"a");
value = value.replace(new RegExp("æ", 'g'),"ae");
value = value.replace(new RegExp("ç", 'g'),"c");
value = value.replace(new RegExp("[èéêë]", 'g'),"e");
value = value.replace(new RegExp("[ìíîï]", 'g'),"i");
value = value.replace(new RegExp("ñ", 'g'),"n");
value = value.replace(new RegExp("[òóôõö]", 'g'),"o");
value = value.replace(new RegExp("œ", 'g'),"oe");
value = value.replace(new RegExp("[ùúûü]", 'g'),"u");
value = value.replace(new RegExp("[ýÿ]", 'g'),"y");
value = value.replace(new RegExp("[^a-zA-Z0-9-]", 'g'),"-");
value = value.replace(new RegExp("-+", 'g'),"-");
value = value.replace(new RegExp("^-"),"");
inputfield.value = value;
}
</script>
SCRIPT
;
return $html;
}
sub display_user_form_optional($$) {
my $type = shift;
my $user_ref = shift;
my $html = '';
# $html .= "\n<tr><td>$Lang{twitter}{$lang}</td><td>"
# . textfield(-id=>'twitter', -name=>'twitter', -value=>$user_ref->{name}, -size=>80, -override=>1) . "</td></tr>";
if ($type eq 'add') {
$html .=
"\n<tr><td colspan=\"2\">" . checkbox(-name=>'newsletter', -label=>lang("newsletter_description"), -checked=>'on') . "<br />
$Lang{unsubscribe_info}{$lang}</td></tr>";
}
return $html;
}
sub display_user_form_admin_only($$) {
my $type = shift;
my $user_ref = shift;
my $html = '';
if (not $admin) {
return '';
}
# $html .= "\n<tr><td>$Lang{twitter}{$lang}</td><td>"
# . textfield(-id=>'twitter', -name=>'twitter', -value=>$user_ref->{name}, -size=>80, -override=>1) . "</td></tr>";
if (($type eq 'add') or ($type eq 'edit')) {
$html .= "\n<tr><td colspan=\"2\" style=\"background-color:#ffcccc\">Administrator fields</td></tr>";
$html .= "\n<tr><td>$Lang{organization}{$lang}</td><td>"
. textfield(-id=>'org', -name=>'org', -value=>$user_ref->{org}, -size=>80, -autocomplete=>'org', -override=>1) . "</td></tr>";
$html .= "\n<tr><td colspan=\"2\">" . lang("user_groups") . lang("sep") . ":<ul>";
foreach my $group (@user_groups) {
$html .= "<li>"
. checkbox(-name=>"user_group_$group", -label=>" " . lang("user_group_$group") . separator_before_colon($lc) . " " . lang("user_group_${group}_description")
, -checked=>$user_ref->{$group}, -override=>1) . "</li>";
}
$html .= "</td></tr>";
}
return $html;
}
sub check_user_form($$$) {
my $type = shift;
my $user_ref = shift;
my $errors_ref = shift;
$user_ref->{userid} = remove_tags_and_quote(param('userid'));
$user_ref->{name} = remove_tags_and_quote(decode utf8=>param('name'));
my $email = remove_tags_and_quote(decode utf8=>param('email'));
$log->debug("check_user_form", { type => $type, user_ref => $user_ref, email => $email }) if $log->is_debug();
if ($user_ref->{email} ne $email) {
# check that the email is not already used
my $emails_ref = retrieve("$data_root/users_emails.sto");
if ((defined $emails_ref->{$email}) and ($emails_ref->{$email}[0] ne $user_ref->{userid})) {
$log->debug("check_user_form - email already in use", { type => $type, email => $email, existing_userid => $emails_ref->{$email} }) if $log->is_debug();
push @{$errors_ref}, $Lang{error_email_already_in_use}{$lang};
}
# Keep old email until the user is saved
$user_ref->{old_email} = $user_ref->{email};
$user_ref->{email} = $email;
}
if (defined param('twitter')) {
$user_ref->{twitter} = remove_tags_and_quote(decode utf8=>param('twitter'));
$user_ref->{twitter} =~ s/^http:\/\/twitter.com\///;
$user_ref->{twitter} =~ s/^\@//;
}
# Is there a checkbox to make a professional account
if (defined param("pro_checkbox")) {
if (param("pro")) {
$user_ref->{pro} = 1;
if (defined param("requested_org")) {
$user_ref->{requested_org} = remove_tags_and_quote(decode utf8=>param("requested_org"));
my $requested_org_id = get_string_id_for_lang("no_language", $user_ref->{requested_org});
if ($requested_org_id ne "") {
$user_ref->{requested_org_id} = $requested_org_id;
}
else {
push @{$errors_ref}, "error_missing_org";
}
}
else {
delete $user_ref->{requested_org_id}
}
}
else {
delete $user_ref->{pro};
delete $user_ref->{requested_org_id}
}
}
if ($type eq 'add') {
$user_ref->{newsletter} = remove_tags_and_quote(param('newsletter'));
$user_ref->{discussion} = remove_tags_and_quote(param('discussion'));
$user_ref->{ip} = remote_addr();
$user_ref->{initial_lc} = $lc;
$user_ref->{initial_cc} = $cc;
$user_ref->{initial_user_agent} = user_agent();
}
if ($admin) {
# Org
my $previous_org = $user_ref->{org};
$user_ref->{org} = remove_tags_and_quote(decode utf8=>param('org'));
if ($user_ref->{org} ne "") {
$user_ref->{org_id} = get_string_id_for_lang("no_language", $user_ref->{org});
# Admin field for org overrides the requested org field
delete $user_ref->{requested_org};
delete $user_ref->{requested_org_id};
my $org_ref = retrieve_or_create_org($User_id, $user_ref->{org});
add_user_to_org( $org_ref, $user_ref->{userid},
[ "admins", "members" ] );
}
else {
delete $user_ref->{org};
delete $user_ref->{org_id};
}
if ((defined $previous_org) and ($previous_org ne "") and ($previous_org ne $user_ref->{org})) {
my $org_ref = retrieve_org($previous_org);
if (defined $org_ref) {
remove_user_from_org( $org_ref, $user_ref->{userid},
[ "admins", "members" ] );
}
}
# Permission groups
foreach my $group (@user_groups) {
$user_ref->{$group} = remove_tags_and_quote(param("user_group_$group"));
}
}
defined $user_ref->{registered_t} or $user_ref->{registered_t} = time();
for (my $i = 1; $i <= 3; $i++) {
if (defined param('team_' . $i)) {
$user_ref->{'team_' . $i} = remove_tags_and_quote(decode utf8=>param('team_' . $i));
$user_ref->{'team_' . $i} =~ s/\</ /g;
$user_ref->{'team_' . $i} =~ s/\>/ /g;
$user_ref->{'team_' . $i} =~ s/\"/"/g;
}
}
# Check input parameters, redisplay if necessary
if (length($user_ref->{name}) < 2) {
push @{$errors_ref}, $Lang{error_no_name}{$lang};
}
my $address;
eval {
$address = Email::Valid->address( -address => $user_ref->{email}, -mxcheck => 1 );
};
$address = 0 if $@;
if (not $address) {
push @{$errors_ref}, $Lang{error_invalid_email}{$lang};
}
if ($type eq 'add') {
my $userid = get_string_id_for_lang("no_language", $user_ref->{userid});
if (length($user_ref->{userid}) < 2) {
push @{$errors_ref}, $Lang{error_no_username}{$lang};
}
elsif (-e "$data_root/users/$userid.sto") {
push @{$errors_ref}, $Lang{error_username_not_available}{$lang};
}
elsif ($user_ref->{userid} !~ /^[a-z0-9]+[a-z0-9\-]*[a-z0-9]+$/) {
push @{$errors_ref}, $Lang{error_invalid_username}{$lang};
}
if (length(decode utf8=>param('password')) < 6) {
push @{$errors_ref}, $Lang{error_invalid_password}{$lang};
}
}
if (param('password') ne param('confirm_password')) {
push @{$errors_ref}, $Lang{error_different_passwords}{$lang};
}
elsif (param('password') ne '') {
$user_ref->{encrypted_password} = create_password_hash( encode_utf8(decode utf8=>param('password')) );
}
return;
}
sub process_user_form($$) {
my $type = shift;
my $user_ref = shift;
my $userid = $user_ref->{userid};
my $error = 0;
$log->debug("process_user_form", { type => $type, user_ref => $user_ref }) if $log->is_debug();
# Professional account with requested org?
if (defined $user_ref->{requested_org_id}) {
my $requested_org_ref = retrieve_org($user_ref->{requested_org_id});
if (defined $requested_org_ref) {
# The requested org already exists
my $admin_mail_body = <<EMAIL
requested_org_id: $user_ref->{requested_org_id}
userid: $user_ref->{userid}
name: $user_ref->{name}
email: $user_ref->{email}
lc: $user_ref->{initial_lc}
cc: $user_ref->{initial_cc}
EMAIL
;
send_email_to_producers_admin("Org request - user: $userid - org: " . $user_ref->{requested_org_id}, $admin_mail_body);
}
else {
# The requested org does not exist, create it
my $org_ref = create_org($userid, $user_ref->{requested_org});
add_user_to_org($org_ref, $userid, ["admins", "members"]);
$user_ref->{org} = $user_ref->{requested_org_id};
$user_ref->{org_id} = get_string_id_for_lang("no_language", $user_ref->{org});
my $admin_mail_body = <<EMAIL
requested_org_id: $user_ref->{requested_org_id}
userid: $user_ref->{userid}
name: $user_ref->{name}
email: $user_ref->{email}
lc: $user_ref->{initial_lc}
cc: $user_ref->{initial_cc}
EMAIL
;
send_email_to_producers_admin(
"Org created by user: $userid - org: "
. $user_ref->{requested_org_id},
$admin_mail_body
);
delete $user_ref->{requested_org};
delete $user_ref->{requested_org_id}
}
}
store("$data_root/users/$userid.sto", $user_ref);
# Update email
my $emails_ref = retrieve("$data_root/users_emails.sto");
my $email = $user_ref->{email};
if ((defined $email) and ($email =~/\@/)) {
$emails_ref->{$email} = [$userid];
}
if (defined $user_ref->{old_email}) {
delete $emails_ref->{$user_ref->{old_email}};
delete $user_ref->{old_email};
}
store("$data_root/users_emails.sto", $emails_ref);
if ($type eq 'add') {
# Initialize the session to send a session cookie back
# so that newly created users do not have to login right after
param("user_id", $userid);
init_user();
my $email = lang("add_user_email_body");
$email =~ s/<USERID>/$userid/g;
# $email =~ s/<PASSWORD>/$user_ref->{password}/g;
$error = send_email($user_ref,lang("add_user_email_subject"), $email);
my $admin_mail_body = <<EMAIL
Bonjour,
Inscription d'un utilisateur :
name: $user_ref->{name}
email: $user_ref->{email}
twitter: https://twitter.com/$user_ref->{twitter}
newsletter: $user_ref->{newsletter}
discussion: $user_ref->{discussion}
lc: $user_ref->{initial_lc}
cc: $user_ref->{initial_cc}
EMAIL
;
$error += send_email_to_admin("Inscription de $userid", $admin_mail_body);
}
return $error;
}
sub check_edit_owner($$) {
my $user_ref = shift;
my $errors_ref = shift;
$user_ref->{pro_moderator_owner} = get_string_id_for_lang("no_language", remove_tags_and_quote(param('pro_moderator_owner')));
$log->debug("check_edit_owner", { pro_moderator_owner => $User{pro_moderator_owner} }) if $log->is_debug();
if ((not defined $user_ref->{pro_moderator_owner}) or ($user_ref->{pro_moderator_owner} eq "")) {
delete $user_ref->{pro_moderator_owner};
# Also edit the current user object so that we can display the current status directly on the form result page
delete $User{pro_moderator_owner};
}
elsif ($user_ref->{pro_moderator_owner} =~ /^org-/) {
my $orgid = $';
$User{pro_moderator_owner} = $user_ref->{pro_moderator_owner};
$log->debug("set pro_moderator_owner (org)", { orgid => $orgid, pro_moderator_owner => $User{pro_moderator_owner} }) if $log->is_debug();
}
elsif ($user_ref->{pro_moderator_owner} =~ /^user-/) {
my $userid = $';
# Add check that organization exists when we add org profiles
if (! -e "$data_root/users/$userid.sto") {
push @{$errors_ref}, sprintf($Lang{error_user_does_not_exist}{$lang}, $userid);
}
else {
$User{pro_moderator_owner} = $user_ref->{pro_moderator_owner};
$log->debug("set pro_moderator_owner (user)", { userid => $userid, pro_moderator_owner => $User{pro_moderator_owner} }) if $log->is_debug();
}
}
elsif ($user_ref->{pro_moderator_owner} eq 'all') {
# Admin mode to see all products from all owners
$User{pro_moderator_owner} = $user_ref->{pro_moderator_owner};
$log->debug("set pro_moderator_owner (all) see products from all owners", { pro_moderator_owner => $User{pro_moderator_owner} }) if $log->is_debug();
}
else {
push @{$errors_ref},$Lang{error_malformed_owner}{$lang};
$log->debug("error - malformed pro_moderator_owner", { pro_moderator_owner => $User{pro_moderator_owner} }) if $log->is_debug();
}
return;
}
sub init_user()
{
my $user_id = undef ;
my $user_ref = undef;
my $org_ref = undef;
my $cookie_name = 'session';
my $cookie_domain = "." . $server_domain; # e.g. fr.openfoodfacts.org sets the domain to .openfoodfacts.org
if ( defined $server_options{cookie_domain} ) {
$cookie_domain = "." . $server_options{cookie_domain}; # e.g. fr.import.openfoodfacts.org sets domain to .openfoodfacts.org
}
$cookie = undef;
$User_id = undef;
$Org_id = undef;
%User = ();
%Org = ();
# Remove persistent cookie if user is logging out
if ((defined param('length')) and (param('length') eq 'logout')) {
$log->debug("user logout") if $log->is_debug();
my $session = {} ;
$cookie = cookie (-name=>$cookie_name, -expires=>'-1d',-value=>$session, -path=>'/', -domain=>"$cookie_domain") ;
}
# Retrieve user_id and password from form parameters
elsif ( (defined param('user_id')) and (param('user_id') ne '') and
( ( (defined param('password')) and (param('password') ne ''))
) ) {
# CGI::param called in list context from package ProductOpener::Users line 373, this can lead to vulnerabilities.
# See the warning in "Fetching the value or values of a single named parameter"
# -> use a scalar to avoid calling param() in the list of arguments to remove_tags_and_quote
my $param_user_id = param('user_id');
$user_id = remove_tags_and_quote($param_user_id) ;
if ($user_id =~ /\@/) {
my $emails_ref = retrieve("$data_root/users_emails.sto");
$log->info("got email while initializing user", { email => $user_id }) if $log->is_info();
if (not defined $emails_ref->{$user_id}) {
$user_id = undef;
$log->info("bad user e-mail") if $log->is_info();
# Trigger an error
return ($Lang{error_bad_login_password}{$lang}) ;
}
else {
my @userids = @{$emails_ref->{$user_id}};
$user_id = $userids[0];
}
$log->info("corresponding user_id", { userid => $user_id }) if $log->is_info();
}
$log->context->{user_id} = $user_id;
$log->debug("user_id is defined") if $log->is_debug();
my $session = undef ;
# If the user exists
if (defined $user_id) {
my $user_file = "$data_root/users/" . get_string_id_for_lang("no_language", $user_id) . ".sto";
if (-e $user_file) {
$user_ref = retrieve($user_file) ;
$user_id = $user_ref->{'userid'} ;
$log->context->{user_id} = $user_id;
my $hash_is_correct = check_password_hash(encode_utf8(decode utf8=>param('password')), $user_ref->{'encrypted_password'} );
# We don't have the right password
if (not $hash_is_correct) {
$user_id = undef ;
$log->info("bad password - input does not match stored hash", { encrypted_password => $user_ref->{'encrypted_password'} }) if $log->is_info();
# Trigger an error
return ($Lang{error_bad_login_password}{$lang}) ;
}
# We have the right login/password
elsif (not defined param('no_log')) # no need to store sessions for internal requests
{
$log->info("correct password for user provided") if $log->is_info();
# Maximum of sessions for a given user
my $max_session = 10 ;
# Generate a secure session key, store the cookie
my $user_session = generate_token(64);
$log->context->{user_session} = $user_session;
# Check if we need to delete the oldest session
# delete $user_ref->{'user_session'};
if ((defined ($user_ref->{'user_sessions'})) and
((scalar keys %{$user_ref->{'user_sessions'}}) >= $max_session)) {
my %user_session_stored = %{$user_ref->{'user_sessions'}} ;
# Find the older session and remove it
my @session_by_time = sort { $user_session_stored{$a}{'time'} <=>
$user_session_stored{$b}{'time'} } (keys %user_session_stored);
while (($#session_by_time + 1)> $max_session)
{
my $oldest_session = shift @session_by_time;
delete $user_ref->{'user_sessions'}{$oldest_session};
}
}
if (not defined $user_ref->{'user_sessions'}) {
$user_ref->{'user_sessions'} = {};
}
$user_ref->{'user_sessions'}{$user_session} = {};
# Store the ip and time corresponding to the given session
$user_ref->{'user_sessions'}{$user_session}{'ip'} = remote_addr();
$user_ref->{'user_sessions'}{$user_session}{'time'} = time();
$session = { 'user_id'=>$user_id, 'user_session'=>$user_session };
# Upgrade hashed password to scrypt, if it is still in crypt format
if ($user_ref->{'encrypted_password'} =~ /^\$1\$(?:.*)/) {
$user_ref->{'encrypted_password'} = create_password_hash(encode_utf8(decode utf8=>param('password')) );
$log->info("crypt password upgraded to scrypt_hash") if $log->is_info();
}
store("$user_file", $user_ref);
$log->debug("session initialized and user info stored") if $log->is_debug();
# Check if the user is logging in
my $length = 0;
if ((defined param('length')) and (param('length') > 0))
{
$length = param('length');
}
elsif ((defined param('remember_me')) and (param('remember_me') eq 'on'))
{
$length = 31536000 * 10;
}
if ($length > 0)
{
# Set a persistent cookie
$log->debug("setting persistent cookie") if $log->is_debug();
$cookie = cookie (-name=>$cookie_name, -value=>$session, -path=>'/', -domain=>"$cookie_domain", -samesite=>'Lax',
-expires=>'+' . $length . 's');
}
else
{
# Set a session cookie
$log->debug("setting session cookie") if $log->is_debug();
$cookie = cookie (-name=>$cookie_name, -value=>$session, -path=>'/', -domain=>"$cookie_domain", -samesite=>'Lax');
}
}
}
else
{
$user_id = undef ;
$log->info("bad user") if $log->is_info();
# Trigger an error
return ($Lang{error_bad_login_password}{$lang}) ;
}
}
}
# Retrieve user_id and session from cookie
elsif ((defined cookie($cookie_name)) or ((defined param('user_session')) and (defined param('user_id')))) {
my $user_session;
if (defined param('user_session')) {
$user_session = param('user_session');
$user_id = param('user_id');
$log->debug("user_session parameter found", { user_id => $user_id, user_session => $user_session }) if $log->is_debug();
}
else {
my %session = cookie($cookie_name);
$user_session = $session{'user_session'} ;
$user_id = $session{'user_id'};
$log->debug("session cookie found", { user_id => $user_id, user_session => $user_session }) if $log->is_debug();
}
if (defined $user_id)
{
my $user_file = "$data_root/users/" . get_string_id_for_lang("no_language", $user_id) . ".sto";
if ($user_id =~/f\/(.*)$/) {
$user_file = "$data_root/facebook_users/" . get_string_id_for_lang("no_language", $1) . ".sto";
}
if (-e $user_file)
{
$user_ref = retrieve($user_file);
$log->debug("initializing user", {
user_id => $user_id,
user_session => $user_session,
stock_session => $user_ref->{'user_sessions'},
stock_ip => $user_ref->{'user_last_ip'},
current_ip => remote_addr()
}) if $log->is_debug();
# Try to keep sessions opened for users with dynamic IPs
my $short_ip = sub ($)
{
my $ip = shift;
# Remove the last two bytes
$ip =~ s/(\.\d+){2}$//;
return $ip;
};
if ((not defined $user_ref->{'user_sessions'})
or (not defined $user_session)
or (not defined $user_ref->{'user_sessions'}{$user_session})
or (not is_ip_known_or_whitelisted($user_ref, $user_session, remote_addr(), $short_ip)))
{
$log->debug("no matching session for user") if $log->is_debug();
$user_id = undef;
$user_ref = undef;
# Remove the cookie
my $session = {} ;
$cookie = cookie (-name=>$cookie_name, -expires=>'-1d',-value=>$session, -path=>'/', -domain=>"$cookie_domain") ;
}
else
{
$log->debug("user identified", { user_id => $user_id, stocked_user_id => $user_ref->{'userid'} }) if $log->is_debug();
$user_id = $user_ref->{'userid'} ;
}
}
else
{
# Remove the cookie
my $session = {} ;
$cookie = cookie (-name=>$cookie_name, -expires=>'-1d',-value=>$session, -path=>'/', -domain=>"$cookie_domain") ;
$user_id = undef ;
}
}
else
{
# Remove the cookie
my $session = {} ;
$cookie = cookie (-name=>$cookie_name, -expires=>'-1d',-value=>$session, -path=>'/', -domain=>"$cookie_domain") ;
$user_id = undef ;
}
}
else
{
$log->info("no user found") if $log->is_info();
}
$log->debug("cookie", { user_id => $user_id, cookie => $cookie }) if $log->is_debug();
$User_id = $user_id;
if (defined $user_ref) {
%User = %{$user_ref};
}
else {
%User = ();
}
# The org and org_id fields are currently properties of the user object (created by administrators through user.pl)
# Populate $Org_id and %org_ref from the user profile.
# TODO: create org profiles with customer service info etc.
if (defined $user_ref->{org_id}) {
$Org_id = $user_ref->{org_id};
$org_ref = { org => $user_ref->{org}, org_id => $user_ref->{org_id} };
}
if (defined $Org_id) {
%Org = %{$org_ref};
}
else {
%Org = ();
}
# if products are private, select the owner used to restrict the product set with the owners_tags field
if ((defined $server_options{private_products}) and ($server_options{private_products})) {