aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Fripost/Commands/add_user.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Fripost/Commands/add_user.pm')
-rw-r--r--lib/Fripost/Commands/add_user.pm63
1 files changed, 21 insertions, 42 deletions
diff --git a/lib/Fripost/Commands/add_user.pm b/lib/Fripost/Commands/add_user.pm
index 4e1cf10..41cabc7 100644
--- a/lib/Fripost/Commands/add_user.pm
+++ b/lib/Fripost/Commands/add_user.pm
@@ -18,57 +18,33 @@ use Fripost::Password;
use Fripost::Prompt;
use Fripost::Schema;
use Fripost::Email;
+use Email::Valid;
our $VERSION = '0.01';
our @EXPORT = qw/main/;
our @ISA = qw(Exporter);
-sub assert {
- my $ldap = shift;
- my $username = shift;
- my ($login, $domain) = split /\@/, $username, 2;
-
- # Error if the domain is unknown.
- die "Error: Unknown domain `" .$domain. "'.\n"
- unless $ldap->domain->search({ domain => $domain })->count;
-
- # Ensure that the username doesn't already exist.
- die "Error: User `" .$username. "' already exists.\n"
- if $ldap->user->search({ username => $username })->count;
-
- # Ensure that the username doesn't correspond to an existing alias.
- my $res = $ldap->alias->search({ address => $username });
- if ($res->count) {
- print STDERR "Error: Alias $username already exists. ";
- print STDERR "(Targetting to ";
- print STDERR (join ', ', map { $_->{goto} } $res->entries);
- say STDERR ".)";
- exit 1;
- }
-}
-
-
sub main {
my $ldap = shift;
my $conf = shift;
-
- # Define the new user
- my $username;
- {
- if (defined $_[0]) {
- $username = fix_username ($_[0]);
- Email::Valid->address($username)
- or die "Error: $username is not a valid e-mail.\n";
- }
- else {
- $username = prompt_email( "New username: ", 'is_user' );
- # TODO: add the assert in the hash.
- }
- }
-
- &assert ($ldap, $username);
+ my $username = shift;
+ prompt_if_undefined ( "New username: ", \$username,
+ [ rewrite => sub { fix_username $_ }
+ , 'Invalid e-mail' => sub { Email::Valid->address($_) }
+ , 'Unknown domain' => sub { $ldap->domain->search({
+ domain => (split /\@/, $_, 2)[1]
+ })->count }
+ , 'User exists' => sub { $ldap->user->search({
+ username => $_
+ })->count == 0 }
+ , 'Alias exists' => sub { $ldap->alias->search({
+ address => $_
+ })->count == 0 }
+ ]
+ );
+
my $user;
my $clearPassword;
@@ -99,7 +75,10 @@ sub main {
my $info;
unless (defined $conf->{password}) {
if (confirm "Send email with login information? ") {
- my $to = prompt_email("Where should the email be sent? ");
+ my $to;
+ prompt_if_undefined ( "Where should the email be sent? ", $to,
+ [ 'Invalid e-mail' => sub { Email::Valid->address($_) } ]
+ );
$info = new_user_info_message ( $conf, $user->{username},
$clearPassword,
$to );