Commit 51cde006 authored by IKEDA Soji's avatar IKEDA Soji
Browse files

Add the new list parameters default_owner_options & default_editor_options to...

Add the new list parameters default_owner_options & default_editor_options to control default attributes of owner & moderators (#989)

sympa.pl: --add can take a --role option
parent 7dadde03
......@@ -158,6 +158,9 @@ message_hook privileged_owner read
personalization owner,privileged_owner read
default_owner_options owner read
default_editor_options owner read
default privileged_owner write
default owner write
default editor read
......
......@@ -926,7 +926,7 @@ our %pinfo = (
default_user_options => {
context => [qw(list)],
order => 20.06,
order => 20.06_01,
group => 'sending',
gettext_id => "Subscription profile",
gettext_comment => 'Default profile for the subscribers of the list.',
......@@ -958,6 +958,74 @@ our %pinfo = (
}
},
},
default_owner_options => {
context => [qw(list domain site)],
order => 20.06_02,
group => 'sending',
gettext_id => "Owner profile",
gettext_comment => 'Default profile for the owners of the list.',
format => {
profile => {
context => [qw(list domain site)],
order => 1,
gettext_id => "profile",
format => ['privileged', 'normal'],
occurrence => '1',
default => 'normal'
},
reception => {
context => [qw(list domain site)],
order => 2,
gettext_id => "reception mode",
gettext_comment => 'Mail reception mode.',
format => ['mail', 'nomail'],
field_type => 'reception',
occurrence => '1',
default => 'mail'
},
visibility => {
context => [qw(list domain site)],
order => 3,
gettext_id => "visibility",
gettext_comment => 'Visibility of the owner.',
format => ['conceal', 'noconceal'],
field_type => 'visibility',
occurrence => '1',
default => 'noconceal'
},
},
not_before => '6.2.67b.2',
},
default_editor_options => {
context => [qw(list domain site)],
order => 20.06_03,
group => 'sending',
gettext_id => "Moderator profile",
gettext_comment => 'Default profile for the moderators of the list.',
format => {
reception => {
context => [qw(list domain site)],
order => 1,
gettext_id => "reception mode",
gettext_comment => 'Mail reception mode.',
format => ['mail', 'nomail'],
field_type => 'reception',
occurrence => '1',
default => 'mail'
},
visibility => {
context => [qw(list domain site)],
order => 2,
gettext_id => "visibility",
gettext_comment => 'Visibility of the moderator.',
format => ['conceal', 'noconceal'],
field_type => 'visibility',
occurrence => '1',
default => 'noconceal'
},
},
not_before => '6.2.67b.2',
},
msg_topic => {
context => [qw(list)],
......
......@@ -61,46 +61,10 @@ sub new {
die 'bug in logic. Ask developer' unless ref $list eq 'Sympa::List';
}
# Get default user options.
my ($defopts, @required);
if ($options{default_user_options}) {
$defopts = $options{default_user_options};
@required = qw(reception visibility);
} elsif ($role eq 'owner') {
my @keys = qw(visibility reception profile info);
@{$defopts}{@keys} = @options{@keys};
@required = qw(reception visibility profile);
} elsif ($role eq 'editor') {
my @keys = qw(visibility reception info);
@{$defopts}{@keys} = @options{@keys};
@required = qw(reception visibility);
}
# Complement required attributes.
#FIXME: check not only existence but also validity of values
if (@required) {
my $defdefs = {
reception => 'mail',
visibility => 'noconceal',
profile => 'normal',
};
my @missing =
grep { not(defined $defopts->{$_} and length $defopts->{$_}) }
@required;
@{$defopts}{@missing} = @{$defdefs}{@missing} if @missing;
}
my @defkeys = sort keys %{$defopts || {}};
my @defvals = @{$defopts || {}}{@defkeys} if @defkeys;
#FIXME: consider boundaries of Unicode characters (or grapheme clusters)
$options{name} = substr $options{name}, 0, 50
if $options{name} and 50 < length $options{name};
my $self = $type->_new(
%options,
_role => $role,
_defkeys => [@defkeys],
_defvals => [@defvals],
);
$options{name} = Sympa::Tools::Text::clip($options{name}, 50)
if 50 < length($options{name} // '');
my $self = $type->_new(%options, _role => $role,);
$self->{_external} = not($self->isa('Sympa::DataSource::List')
and [split /\@/, $self->{listname}, 2]->[1] eq $list->{'domain'})
if ref $list eq 'Sympa::List';
......
......@@ -1688,14 +1688,19 @@ sub get_reply_to {
## Returns a default user option
sub get_default_user_options {
$log->syslog('debug3', '(%s,%s)', @_);
my $self = shift;
my $what = shift;
$log->syslog('debug3', '(%s, %s => %s)', @_);
my $self = shift;
my %options = @_;
if ($self) {
return $self->{'admin'}{'default_user_options'};
}
return undef;
my $role = $options{role} // 'member';
return ($role eq 'member')
? {%{$self->{'admin'}{'default_user_options'} // {}}}
: ($role eq 'owner')
? {%{$self->{'admin'}{'default_owner_options'} // {}}}
: ($role eq 'editor')
? {%{$self->{'admin'}{'default_editor_options'} // {}}}
: {};
}
# Returns the number of subscribers of a list.
......@@ -3121,7 +3126,7 @@ sub add_list_member {
}
}
my $values = {%{$self->get_default_user_options // {}}};
my $values = $self->get_default_user_options(role => 'member');
while (my ($k, $v) = each %$u) {
$values->{$k} = $v if defined $v;
}
......@@ -3298,23 +3303,41 @@ sub add_list_admin {
sub _add_list_admin {
my $self = shift;
my $role = shift;
my $user = shift;
my $u = shift;
my %options = @_;
my $stash_ref = $options{stash} || [];
my $who = Sympa::Tools::Text::canonic_email($user->{'email'});
return undef unless defined $who and length $who;
return undef unless Sympa::Tools::Text::valid_email($u->{email});
my $who = Sympa::Tools::Text::canonic_email($u->{email});
my $values = $self->get_default_user_options(role => $role);
while (my ($k, $v) = each %$u) {
$values->{$k} = $v if defined $v;
}
$values->{email} = $who;
$values->{'date'} ||= time;
$values->{'update_date'} ||= $values->{'date'};
unless (defined $user->{'inclusion'}) {
# Compat.<=6.2.44 FIXME: needed?
$values->{'inclusion'} ||= $values->{'date'}
if delete $values->{'included'};
# Either is_included or is_subscribed must be set.
# Default is is_subscriber for backward compatibility reason.
$values->{'subscribed'} = 1 unless defined $values->{'inclusion'};
$values->{'subscribed'} ||= 0;
#FIXME: Is it required?
unless (defined $values->{'inclusion'}) {
# Is the email in user_table? Insert it.
#FIXME: Is it required?
unless (
Sympa::User->new(
$who,
'gecos' => $user->{'gecos'},
'lang' => $user->{'lang'},
'password' => $user->{'password'},
'gecos' => $values->{'gecos'},
'lang' => $values->{'lang'},
'password' => $values->{'password'},
)
) {
$log->syslog('err', 'Unable to add admin %s to user_table', $who);
......@@ -3324,22 +3347,6 @@ sub _add_list_admin {
}
}
$user->{'reception'} ||= 'mail';
$user->{'visibility'} ||= 'noconceal';
$user->{'profile'} ||= 'normal';
$user->{'date'} ||= time;
$user->{'update_date'} ||= $user->{'date'};
# Compat.<=6.2.44 FIXME: needed?
$user->{'inclusion'} ||= $user->{'date'}
if delete $user->{'included'};
# Either is_included or is_subscribed must be set.
# Default is is_subscriber for backward compatibility reason.
$user->{'subscribed'} = 1 unless defined $user->{'inclusion'};
$user->{'subscribed'} ||= 0;
my $sdm = Sympa::DatabaseManager->instance;
my $sth;
my %map_field = _map_list_admin_cols();
......@@ -3349,9 +3356,9 @@ sub _add_list_admin {
# Update Admin Table
@set_list =
@map_field{grep { $_ ne 'date' and exists $user->{$_} } @key_list};
@map_field{grep { $_ ne 'date' and exists $values->{$_} } @key_list};
@val_list =
@{$user}{grep { $_ ne 'date' and exists $user->{$_} } @key_list};
@{$values}{grep { $_ ne 'date' and exists $values->{$_} } @key_list};
if ( $options{replace}
and @set_list
and $sdm
......@@ -3375,7 +3382,7 @@ sub _add_list_admin {
return 1;
}
@set_list = @map_field{@key_list};
@val_list = @{$user}{@key_list};
@val_list = @{$values}{@key_list};
unless (
@set_list
and $sdm
......
......@@ -60,8 +60,12 @@ sub _twist {
my $sender = $request->{sender};
my $email = $request->{email};
my $comment = $request->{gecos};
my $role = $request->{role} || 'member';
my $ca = $request->{custom_attribute};
die 'bug in logic. Ask developer'
unless grep { $role eq $_ } qw(member owner editor);
unless ($request->{force} or $list->is_subscription_allowed) {
$log->syslog('info', 'List %s not open', $list);
$self->add_stash($request, 'user', 'list_not_open',
......@@ -97,9 +101,15 @@ sub _twist {
}
my @stash;
$list->add_list_member(
{email => $email, gecos => $comment, custom_attribute => $ca},
stash => \@stash);
if ($role eq 'member') {
$list->add_list_member(
{email => $email, gecos => $comment, custom_attribute => $ca},
stash => \@stash);
} else {
$list->add_list_admin($role,
{email => $email, gecos => $comment},
stash => \@stash);
}
foreach my $report (@stash) {
$self->add_stash($request, @$report);
if ($report->[0] eq 'intern') {
......
......@@ -486,9 +486,6 @@ sub _update_users {
($ds->is_external ? (inclusion_ext => $time) : ()),
inclusion_label => $ds->name,
};
my @defkeys = @{$ds->{_defkeys} || []};
my @defvals = @{$ds->{_defvals} || []};
@{$user}{@defkeys} = @defvals if @defkeys;
$result{added}++;
$user;
......
......@@ -522,6 +522,12 @@ if ($main::options{'dump'} or $main::options{'dump_users'}) {
exit 1;
}
$main::options{'role'} //= 'member';
unless (grep {$main::options{'role'} eq $_} qw(member owner editor)) {
printf STDERR "Unknown role \"%s\".\n", $main::options{'role'};
exit 1;
}
my (@emails, @dnames);
my $content = do { local $RS; <STDIN> };
foreach (split /\r\n|\r|\n/, $content) {
......@@ -540,6 +546,7 @@ if ($main::options{'dump'} or $main::options{'dump_users'}) {
my $spindle = Sympa::Spindle::ProcessRequest->new(
context => $list,
action => 'add',
role => $main::options{'role'},
email => [@emails],
gecos => [@dnames],
force => 1,
......@@ -1849,7 +1856,7 @@ C<sympa.pl> S<[ C<-d, --debug> ]> S<[ C<-f, --file>=I<another.sympa.conf> ]>
S<[ C<-l, --lang>=I<lang> ]> S<[ C<-m, --mail> ]>
S<[ C<-h, --help> ]> S<[ C<-v, --version> ]>
S<>
S<[ C<--add>=I<list>@I<domain> [--quiet] [--notify] [--force] ]>
S<[ C<--add>=I<list>@I<domain> [--role=I<role>] [--quiet] [--notify] [--force] ]>
S<[ C<--del>=I<list>@I<domain> [--quiet] [--notify] [--force] ]>
S<[ C<--open_list>=I<list>[I<@robot>] [--notify] ]>
S<[ C<--close_list>=I<list>[I<@robot>] ]>
......@@ -1899,7 +1906,8 @@ With the following options F<sympa.pl> will run in batch mode:
=over 4
=item C<--add=>I<list>@I<domain> [ C<--quiet> ] [ C<--notify> ] [ C<--force> ]
=item C<--add=>I<list>@I<domain> [ C<--role>=I<role> ]
[ C<--quiet> ] [ C<--notify> ] [ C<--force> ]
Add email(s) from the list. Data are read from standard input.
The data should contain one email address per line.
......@@ -1911,7 +1919,7 @@ Sample:
mary.blacksmith@another.company.com Mary Blacksmith
Note:
This options was added on Sympa 6.2.67b.1.
This option was added on Sympa 6.2.67b.1.
=item C<--add_list=>I<family_name> C<--robot=>I<robot_name>
C<--input_file=>I</path/to/file.xml>
......
......@@ -12,6 +12,8 @@ unless (eval 'Test::Net::LDAP::Util->can("ldap_mockify")') {
plan skip_all => 'Test::Net::LDAP required';
}
use Sympa::List;
$Data::Dumper::Terse = 1;
$Data::Dumper::Indent = 0;
......
Markdown is supported
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