Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allowing Org Admins to Add/Invite Users #8763

25 changes: 24 additions & 1 deletion cgi/org.pl
Original file line number Diff line number Diff line change
Expand Up @@ -358,18 +358,41 @@
if ($type eq "edit") {

store_org($org_ref);
$template_data_ref->{result} = lang("edit_org_result");
}
elsif ($type eq 'user_delete') {

if (is_user_in_org_group($org_ref, $User_id, "admins")) {
remove_user_by_org_admin($orgid, single_param('user_id'));
$template_data_ref->{result} = lang("edit_org_result");
}
else {
display_error_and_exit($Lang{error_no_permission}{$lang}, 403);
}

}
$template_data_ref->{result} = lang("edit_org_result");
elsif ($type eq 'add_users') {
if (is_user_in_org_group($org_ref, $User_id, "admins")) {
my $email_list = remove_tags_and_quote(single_param('email_list'));
my $email_ref = add_users_to_org_by_admin($orgid, $email_list);

my @messages;
MonalikaPatnaik marked this conversation as resolved.
Show resolved Hide resolved

if (@{$email_ref->{invited}}) {
# Some users need to create an OFF account
my $emails_invited_str = join("<br>&rarr; ", @{$email_ref->{invited}});
push @messages,
"<h3>Please ask the following users to create an Open Food Facts account first:</h3><br>&rarr; $emails_invited_str.";
MonalikaPatnaik marked this conversation as resolved.
Show resolved Hide resolved
}
if (@{$email_ref->{added}}) {
# All users added successfully
my $emails_added_str = join("<br>&rarr; ", @{$email_ref->{added}});
push @messages, "<h3>Users added to the organization successfully:</h3><br>&rarr; $emails_added_str.";
}
$template_data_ref->{result} = join("<br>", @messages);
}
}

$template_data_ref->{profile_url} = canonicalize_tag_link("editors", "org-" . $orgid);
$template_data_ref->{profile_name} = sprintf(lang('user_s_page'), $org_ref->{name});
}
Expand Down
52 changes: 52 additions & 0 deletions lib/ProductOpener/Users.pm
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ BEGIN {
&check_password_hash
&retrieve_user
&remove_user_by_org_admin
&add_users_to_org_by_admin

&check_session

Expand Down Expand Up @@ -936,6 +937,25 @@ sub retrieve_user ($user_id) {
return $user_ref;
}

sub is_email_has_off_account ($email) {

# First, check if the email exists in the users_emails.sto file
my $emails_ref = retrieve("$data_root/users/users_emails.sto");

if (defined $emails_ref->{$email}) {
my $user_id = $emails_ref->{$email}[0];

# Next, check if the user file exists and has the 'userid' field
my $user_file = "$data_root/users/" . get_string_id_for_lang("no_language", $user_id) . ".sto";
if (-e $user_file) {
my $user_ref = retrieve($user_file);
return $user_ref->{userid} if defined $user_ref->{userid};
}
}

return; # Email is not associated with an OFF account
}

sub remove_user_by_org_admin ($orgid, $user_id) {
my $groups_ref = ['admins', 'members'];
remove_user_from_org($orgid, $user_id, $groups_ref);
Expand All @@ -949,6 +969,38 @@ sub remove_user_by_org_admin ($orgid, $user_id) {
return;
}

sub add_users_to_org_by_admin ($org_id, $email_list) {
MonalikaPatnaik marked this conversation as resolved.
Show resolved Hide resolved

my @emails_added;
my @emails_invited;

# Convert the email_list into an array of email addresses
my @emails = split(/,\s*/, $email_list);

foreach my $email (@emails) {

# Check if the email is associated with an OpenFoodFacts account
my $user_id = is_email_has_off_account($email);
if (defined $user_id) {
# Add the user to the organization
add_user_to_org($org_id, $user_id, ["members"]);
push @emails_added, $email;
}
else {

push @emails_invited, $email;

}
}
my $email_ref = {
added => \@emails_added,
invited => \@emails_invited,
};
$log->debug("The list of email ids ", {emails_list => $email_ref}) if $log->is_debug();

return $email_ref;
}

sub init_user ($request_ref) {

my $user_id = undef;
Expand Down
9 changes: 9 additions & 0 deletions templates/web/pages/org_form/org_form.tt.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ <h2>[% lang("organization_members") %]</h2>
</div>
[% END %]

<form method="post" action="/cgi/org.pl" enctype="multipart/form-data" style="margin-bottom: 20px;">
<p>Enter email addresses of users to invite (comma-separated):</p>
MonalikaPatnaik marked this conversation as resolved.
Show resolved Hide resolved
<textarea id="email_list" name="email_list" style="height:100px;width:50vw"></textarea>
<input type="hidden" name="action" value="process" />
<input type="hidden" name="type" value="add_users" />
<input type="hidden" name="orgid" value="[% orgid %]" />
<input type="submit" name=".submit" class="button" value="Invite Users" />
MonalikaPatnaik marked this conversation as resolved.
Show resolved Hide resolved
</form>

<!-- Start form -->

<p>[% lang('org_profile_description') %]</p>
Expand Down
Loading