Commit 49f28256 authored by IKEDA Soji's avatar IKEDA Soji
Browse files

Refactoring: WWSympa: add, del and add_frommod were integrated to Sympa::Request framework.

parent e35baff7
<!-- confirm_action.tt2 -->
[% IF confirm_action == 'add' ~%]
[%# Confirmation is required only by show_exclude action. ~%]
<h2><i class="fa fa-check-circle"></i>
[%|loc%]Restore selected email addresses[%END%]"
</h2>
<p><strong>
[%|loc%]Do you really want to restore subscription for ALL selected addresses?[%END%]
</strong></p>
[% IF previous_action == 'show_exclude' ~%]
<h2><i class="fa fa-check-circle"></i>
[%|loc%]Restore selected email addresses[%END%]"
</h2>
<p><strong>
[%|loc%]Do you really want to restore subscription for ALL selected addresses?[%END%]
</strong></p>
[%~ ELSE ~%]
<h2><i class="fa fa-check-circle"></i>
[%|loc%]Add subscribers[%END%]
</h2>
<p><strong>
[%|loc(email.0)%]Dou you really want to add %1?[%END%]
</strong></p>
[%~ END %]
[%~ ELSIF confirm_action == 'add_frommod' ~%]
<h2><i class="fa fa-check-circle"></i>
[%|loc%]Add subscribers[%END%]
</h2>
<p><strong>
[%|loc(email.0)%]Dou you really want to add %1?[%END%]
[%|loc(email.0.email)%]Dou you really want to add %1?[%END%]
</strong></p>
[%~ ELSIF confirm_action == 'arc' || confirm_action == 'arcsearch_id' ~%]
<h2><i class="fa fa-check-circle"></i>
......@@ -159,9 +167,7 @@
<form action="[% path_cgi %]" method="POST">
[% IF confirm_action == 'add' ~%]
[% FOREACH e = email ~%]
<input type="hidden" name="email" value="[% e %]" />
[%~ END %]
<input type="hidden" name="email" value="[% email %]" />
<div>
<input type="checkbox" id="quiet" type="checkbox" name="quiet" value="1"
[% IF quiet %]checked="checked"[%END%] />
......
......@@ -33,22 +33,38 @@
<input type="hidden" name="list" value="[% list %]" />
</form>
<hr>
<h3>[%|loc%]Add Subscribers[%END%]</h3>
<form action="[% path_cgi %]" method="post">
<h4>[%|loc%]To add an individual user:[%END%]</h4>
<fieldset>
<input type="hidden" name="previous_action" value="review" />
<input type="hidden" name="list" value="[% list %]" />
<input type="hidden" name="action" value="add" />
<label for="email_search">[%|loc%]Email address:[%END%]</label>
<input id="email_search" type="text" name="email" size="35" />
<input id="quietly" type="checkbox" name="quiet" /><label for="quietly">[%|loc%]Quiet (don't send welcome email)[%END%]</label><br />
<input class="MainMenuLinks" type="submit" name="action_add" value="[%|loc%]Add[%END%]" />
</fieldset>
</form>
<h4>[%|loc%]To add multiple users:[%END%]</h4>
<p>[%|loc%]Click the Multiple Add button below to bulk-add users using a form. (Alternatively, consider using a data-source such as SQL or an Active Directory Group).[%END%]
<br /><a class="button" href="[% 'import' | url_rel([list]) %]">[%|loc%]Multiple add[%END%]</a>
[%# If a list is not 'open' and allow_subscribe_if_pending has been set to
'off', add cannot be performed. ~%]
[% IF list_status == 'open' || conf.allow_subscribe_if_pending == 'on' ~%]
<h3>[%|loc%]Add Subscribers[%END%]</h3>
<form action="[% path_cgi %]" method="POST">
<h4>[%|loc%]To add an individual user:[%END%]</h4>
<fieldset>
<input type="hidden" name="previous_action" value="review" />
<input type="hidden" name="list" value="[% list %]" />
<input type="hidden" name="action" value="add" />
<div>
<label for="email_search">[%|loc%]Email address:[%END%]</label>
<input id="email_search" type="text" name="email" size="35" />
</div>
<div>
<input id="quietly" type="checkbox" name="quiet" />
<label for="quietly">
[%|loc%]Quiet (don't send welcome email)[%END%]
</label>
<input class="MainMenuLinks" type="submit" name="action_add"
value="[%|loc%]Add[%END%]" />
</div>
</fieldset>
</form>
<h4>[%|loc%]To add multiple users:[%END%]</h4>
<p>[%|loc%]Click the Multiple Add button below to bulk-add users using a form. (Alternatively, consider using a data-source such as SQL or an Active Directory Group).[%END%]</p>
<div>
<a class="button" href="[% 'import' | url_rel([list]) %]">
[%|loc%]Multiple add[%END%]
</a>
</div>
[%~ END %]
[% END %]
<hr>
......
......@@ -38,6 +38,9 @@
</table>
[% IF may_add ~%]
[%# If a list is not 'open' and allow_subscribe_if_pending has been set to
'off', add cannot be performed. ~%]
[% IF list_status == 'open' || conf.allow_subscribe_if_pending == 'on' ~%]
<div>
<input class="MainMenuLinks toggleButton" type="button"
value="[%|loc%]Toggle Selection[%END%]" />
......@@ -50,6 +53,7 @@
<input type="hidden" name="previous_action" value="[% action %]" />
</div>
[%~ END %]
[%~ END %]
</fieldset>
</form>
......
......@@ -7138,250 +7138,57 @@ sub do_import {
return $in{'previous_action'} || 'review';
}
 
####################################################
# do_add
####################################################
# Adds a user to a list (requested by an other user)
#
# IN : -
#
# OUT : 'loginrequest'
# | ($in{'previous_action'} || 'review')
# | undef
####################################################
## TODO: vérifier validité email
# Adds a user to a list (requested by another user).
sub do_add {
wwslog('info', '(%s)', $in{'email'});
 
my %user;
## If a list is not 'open' and allow_subscribe_if_pending has been set to
## 'off' returns undef.
unless ($list->{'admin'}{'status'} eq 'open'
or Conf::get_robot_conf($robot, 'allow_subscribe_if_pending') eq 'on')
{
Sympa::Report::reject_report_web('user', 'list_not_open',
{'status' => $list->{'admin'}{'status'}},
$param->{'action'});
wwslog('info', 'List not open');
web_db_log(
{ 'target_email' => $in{'email'},
'status' => 'error',
'error_type' => 'list_not_open'
}
);
return undef;
}
my @emails = split /\0/, $in{'email'};
if ($in{'previous_action'} and $in{'previous_action'} eq 'show_exclude') {
$param->{'email'} = [@emails];
$param->{'quiet'} = $in{'quiet'};
# Action confirmed?
my $next_action = $session->confirm_action(
$in{'action'}, $in{'response_action'},
arg => join(',', sort @emails),
previous_action => $in{'previous_action'}
);
return $next_action unless $next_action eq '1';
}
my $email_regexp = Sympa::Regexps::email();
if ($in{'dump'}) {
foreach (split /\n/, $in{'dump'}) {
if (/^\s*($email_regexp)(\s+(.*))?\s*$/) {
$user{Sympa::Tools::Text::canonic_email($1)} = $5;
}
}
} elsif ($in{'email'} =~ /,/) {
foreach my $pair (split /\0/, $in{'email'}) {
if ($pair =~ /^($email_regexp)(,(.*))?\s*$/) {
$user{Sympa::Tools::Text::canonic_email($1)} = $5;
}
}
} elsif ($in{'email'}) {
foreach my $email (split /\0/, $in{'email'}) {
$user{Sympa::Tools::Text::canonic_email($email)} = $in{'gecos'};
}
} else {
Sympa::Report::reject_report_web('user', 'no_email', {},
$param->{'action'});
wwslog('info', 'No email');
web_db_log(
{ 'target_email' => $in{'email'},
'status' => 'error',
'error_type' => 'no_email'
}
);
return undef;
}
my ($total, @new_users, @added_users);
my $comma_emails;
foreach my $email (keys %user) {
my $result = Sympa::Scenario::request_action(
$list, 'add',
$param->{'auth_method'},
{ 'sender' => $param->{'user'}{'email'},
'email' => $in{'email'},
'remote_host' => $param->{'remote_host'},
'remote_addr' => $param->{'remote_addr'}
}
);
my $add_is;
my $reason;
if (ref($result) eq 'HASH') {
$add_is = $result->{'action'};
$reason = $result->{'reason'};
}
unless ($add_is =~ /do_it/) {
Sympa::Report::reject_report_web('auth', $reason, {},
$param->{'action'}, $list);
wwslog('info', '%s may not add', $param->{'user'}{'email'});
web_db_log(
{ 'target_email' => $in{'email'},
'status' => 'error',
'error_type' => 'internal'
}
);
next;
}
unless (Sympa::Tools::Text::valid_email($email)) {
Sympa::Report::reject_report_web('user', 'incorrect_email',
{'email' => $email},
$param->{'action'}, $list);
wwslog('info', 'Incorrect email %s', $email);
web_db_log(
{ 'target_email' => $in{'email'},
'status' => 'error',
'error_type' => 'incorrect_email'
}
);
next;
}
my $user_entry = $list->get_list_member($email);
# Access control.
return undef unless defined check_authz('do_add', 'add');
 
if (defined($user_entry)) {
Sympa::Report::reject_report_web('user',
'user_already_subscriber',
{'list' => $list->{'name'}, 'email' => $email},
$param->{'action'}, $list);
wwslog('info', '%s already subscriber', $email);
web_db_log(
{ 'target_email' => $in{'email'},
'status' => 'error',
'error_type' => 'already_subscriber'
}
);
next;
}
my $email = Sympa::Tools::Text::canonic_email($in{'email'});
return $in{'previous_action'} || 'review' unless $email;
 
my $u2 = Sympa::User::get_global_user($email);
my $defaults = $list->get_default_user_options();
my $u;
%{$u} = %{$defaults};
$u->{'email'} = $email;
$u->{'gecos'} = $user{$email} || $u2->{'gecos'};
$u->{'date'} = $u->{'update_date'} = time;
$u->{'password'} = $u2->{'password'}
|| Sympa::Tools::Password::tmp_passwd($email);
##if (my $reason =
## Sympa::Tools::Password::password_validation($u->{'password'})) {
## Sympa::Report::reject_report_web('user', 'passwd_validation',
## {'reason' => $reason},
## $param->{'action'});
## wwslog('info', 'Password validation');
## web_db_log(
## { 'status' => 'error',
## 'error_type' => 'bad_parameter'
## }
## );
## return undef;
##}
##
$u->{'lang'} = $u2->{'lang'} || $list->{'admin'}{'lang'};
if ($comma_emails) {
$comma_emails = $comma_emails . ',' . $email;
} else {
$comma_emails = $email;
}
$param->{'email'} = $email;
$param->{'quiet'} = $in{'quiet'};
 
##
push @new_users, $u;
## List only email addresses ; used later to remove pending
## subrequests
push @added_users, $email;
# Action confirmed?
my $next_action = $session->confirm_action(
$in{'action'}, $in{'response_action'},
arg => $email,
previous_action => $in{'previous_action'}
);
return $next_action unless $next_action eq '1';
 
unless ($in{'quiet'} || $add_is =~ /quiet/i) {
unless ($list->send_probe_to_user('welcome', $email)) {
wwslog('err', 'Unable to send "welcome" probe to %s', $email);
}
}
my $spindle = Sympa::Spindle::ProcessRequest->new(
context => $list,
action => 'add',
email => $email,
sender => $param->{'user'}{'email'},
md5_check => 1,
scenario_context => {
email => $email,
sender => $param->{'user'}{'email'},
remote_host => $param->{'remote_host'},
remote_addr => $param->{'remote_addr'}
},
);
unless ($spindle and $spindle->spin) {
return $in{'previous_action'} || 'review';
}
 
$list->add_list_member(@new_users);
$total = $list->{'add_outcome'}{'added_members'};
if (defined $list->{'add_outcome'}{'errors'}) {
if (defined $list->{'add_outcome'}{'errors'}
{'max_list_members_exceeded'}) {
Sympa::Report::reject_report_web(
'user',
'max_list_members_exceeded',
{ max_list_members => $list->{'admin'}{'max_list_members'},
list => $list->{'name'},
'sub' => $param->{'user'}{'email'}
},
$param->{'action'},
$list,
$param->{'user'}{'email'},
$robot
);
foreach my $report (@{$spindle->{stash} || []}) {
if ($report->[1] eq 'notice') {
Sympa::Report::notice_report_web(@{$report}[2, 3],
$param->{'action'});
} else {
my $error = $language->gettext_sprintf(
"Unable to add users in list %s : %s",
$list->{'name'},
$list->{'add_outcome'}{'errors'}{'error_message'});
Sympa::Report::reject_report_web('intern', $error,
{'sub' => $param->{'user'}{'email'}},
$param->{'action'}, $list, $param->{'user'}{'email'}, $robot);
Sympa::Report::reject_report_web(@{$report}[1 .. 3],
$param->{action});
}
wwslog(
'info',
'Subscribe failed: %s',
$list->{'add_outcome'}{'errors'}{'error_message'}
);
web_db_log(
{ 'parameters' => $in{'email'},
'status' => 'error',
'error_type' => 'internal'
}
);
return 'info';
}
Sympa::Report::notice_report_web('add_performed', {'total' => $total},
$param->{'action'});
web_db_log(
{ 'target_email' => $in{'email'},
'status' => 'success'
}
);
foreach my $email (@added_users) {
web_db_log(
{ 'target_email' => $email,
'status' => 'success'
}
);
unless (@{$spindle->{stash} || []}) {
Sympa::Report::notice_report_web('performed', {}, $param->{'action'});
}
 
$in{'list'} = $in{'previous_list'} if ($in{'previous_list'});
return $in{'previous_action'} || 'review';
}
 
......@@ -8178,21 +7985,14 @@ sub do_distribute {
return 'modindex';
}
 
####################################################
# do_add_frommod
####################################################
# add user from moderation index
#
####################################################
# Adds user from moderation index.
sub do_add_frommod {
wwslog('info', '(%s)', $in{'id'});
 
my @ids = split /\0/, $in{'id'};
$param->{'id'} = [@ids];
 
my @emails;
# Load message list.
$in{'dump'} = '';
my @users;
foreach my $id (@ids) {
next unless $id and $id =~ /\A\w+\z/;
 
......@@ -8219,18 +8019,18 @@ sub do_add_frommod {
next;
}
my $email = $message->{sender};
next unless defined $email and $email =~ /\S/;
my $fullname = $message->{gecos};
next unless $email and Sympa::Tools::Text::valid_email($email);
my $fullname = $message->{gecos}
if defined $message->{gecos} and $message->{gecos} =~ /\S/;
 
if (defined $fullname and $fullname =~ /\S/) {
$in{'dump'} .= "$email $fullname\n";
} else {
$in{'dump'} .= "$email\n";
}
push @emails, $email;
push @users,
( defined $fullname
? {email => $email, gecos => $fullname}
: {email => $email});
}
$param->{'email'} = [@emails];
return 'modindex' unless @users;
$param->{'email'} = [@users];
 
# Action confirmed?
my $next_action = $session->confirm_action(
......@@ -8240,10 +8040,41 @@ sub do_add_frommod {
);
return $next_action unless $next_action eq '1';
 
if ($in{'dump'}) {
delete $in{'email'};
$in{'previous_list'} = $list->{'name'};
do_add();
my $stash = [];
my $processed = 0;
foreach my $u (@users) {
my $spindle = Sympa::Spindle::ProcessRequest->new(
context => $list,
action => 'add',
email => $u->{email},
gecos => $u->{gecos},
sender => $param->{'user'}{'email'},
md5_check => 1,
scenario_context => {
email => $u->{email},
sender => $param->{'user'}{'email'},
remote_host => $param->{'remote_host'},
remote_addr => $param->{'remote_addr'}
},
stash => $stash,
);
$processed += $spindle->spin if $spindle;
}
unless ($processed) {
return 'modindex';
}
foreach my $report (@$stash) {
if ($report->[1] eq 'notice') {
Sympa::Report::notice_report_web(@{$report}[2, 3],
$param->{'action'});
} else {
Sympa::Report::reject_report_web(@{$report}[1 .. 3],
$param->{action});
}
}
unless (@$stash) {
Sympa::Report::notice_report_web('performed', {}, $param->{'action'});
}
 
return 'modindex';
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment