From 8663144f1f5a3d163119f17a7f9c06655e32727a Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Tue, 17 Apr 2012 01:25:28 +0200 Subject: OO Perl library for our LDAP schema. --- fripost-adduser | 46 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-) (limited to 'fripost-adduser') diff --git a/fripost-adduser b/fripost-adduser index f13868d..bd73aea 100755 --- a/fripost-adduser +++ b/fripost-adduser @@ -16,19 +16,21 @@ B [B<--verbose>] [B<--debug>] [B<--pretend>] [I] =head1 DESCRIPTION -B adds a new mailbox to the system, unless B<--pretend> -is set. +B adds a new virtual mailbox to the system, unless +B<--pretend> is set. If no I or I are given, the user is prompted for them. If I is not fully qualified, C is appended. +If I is already an existing username or alias, +B raises an error. =head1 OPTIONS =over 8 -=item B<--prentend> +=item B<--pretend> -Only simulates the insertion. (But still query the LDAP server to check -if I is already in the database.) +Only simulates the insertion. (But still query the LDAP server to ensure +that I is not already in the database.) =item B<--password=>I @@ -134,8 +136,8 @@ GetOptions( 'base_dn=s' => \$conf->{base_dn}, 'bind_dn=s' => \$conf->{bind_dn}, 'bind_pw=s' => \$conf->{bind_pw}, - 'debug' => \$conf->{debug}, 'pretend' => \$conf->{pretend}, + 'debug' => \$conf->{debug}, 'v|verbose' => \$conf->{verbose}, 'password=s' => \$conf->{password}, 'man' => sub { pod2usage(-exitstatus => 0, @@ -152,12 +154,15 @@ my $ldap = Fripost::Schema->new( $conf ); # Define the new user my $user; { - my $username = $ARGV[0]; - $username //= prompt_email("New username: ", 'is_user'); - - # Default domain - $username .= '@fripost.org' unless $username =~ /\@.+$/; - + my $username; + if (defined $ARGV[0]) { + $username = fix_username ($ARGV[0]); + Email::Valid->address($username) + or die "Error: `" .$username. "' is not a valid e-mail.\n"; + } + else { + $username = prompt_email("New username: ", 'is_user'); + } my ($domain, $login) = split /\@/, $username, 2; my $maildir = "$domain/$login/Maildir/"; # Trailing slash important my $isActive = 'TRUE'; @@ -184,9 +189,22 @@ my $user; confirm_or_abort(); } -die "Error: User already exists.\n" - if $ldap->searchUser($user->{username})->count; +# Check if the username already exists, or is an existing alias. +{ + die "Error: User `" .$user->{username}. "' already exists.\n" + if $ldap->user->search($user->{username})->count; + + my $res = $ldap->alias->search({ address => $user->{username} }); + if ($res->count) { + print STDERR "Error: Alias `" .$user->{username}. "' already exists. "; + print STDERR "(Targetting to "; + print STDERR (join ', ', map { '`' .$_->{goto}. "'"} ($res->entries)); + say STDERR ".)"; + exit 1; + } + exit 1; +} ## Insert the new user if ($conf->{pretend}) { -- cgit v1.2.3