aboutsummaryrefslogtreecommitdiffstats
path: root/fripost-adduser
diff options
context:
space:
mode:
authorGuilhem Moulin <guilhem.moulin@fripost.org>2012-04-17 01:25:28 +0200
committerGuilhem Moulin <guilhem.moulin@fripost.org>2012-04-17 01:25:56 +0200
commit8663144f1f5a3d163119f17a7f9c06655e32727a (patch)
treed81953df51b5b583d85caa40008e0ee9243e6906 /fripost-adduser
parent033af5c7de65c2ba38c45ba649ad29823bfb7141 (diff)
OO Perl library for our LDAP schema.
Diffstat (limited to 'fripost-adduser')
-rwxr-xr-xfripost-adduser46
1 files changed, 32 insertions, 14 deletions
diff --git a/fripost-adduser b/fripost-adduser
index f13868d..bd73aea 100755
--- a/fripost-adduser
+++ b/fripost-adduser
@@ -16,19 +16,21 @@ B<fripost-adduser> [B<--verbose>] [B<--debug>] [B<--pretend>] [I<username>]
=head1 DESCRIPTION
-B<fripost-adduser> adds a new mailbox to the system, unless B<--pretend>
-is set.
+B<fripost-adduser> adds a new virtual mailbox to the system, unless
+B<--pretend> is set.
If no I<username> or I<password> are given, the user is prompted for them.
If I<username> is not fully qualified, C<fripost.org> is appended.
+If I<username> is already an existing username or alias,
+B<fripost-adduser> 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<username> is already in the database.)
+Only simulates the insertion. (But still query the LDAP server to ensure
+that I<username> is not already in the database.)
=item B<--password=>I<password>
@@ -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}) {