aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Fripost/Commands/user_passwd.pm
blob: 64f35d95413df97595b2951bce32c9466943bc95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/perl
package Fripost::Commands::user_passwd;

use 5.010_000;
use warnings;
use strict;
use utf8;

=head1 NAME

user_passwd - Change user password

=cut

use FindBin qw($Bin);
use lib "$Bin/lib";

use Fripost::Password;
use Fripost::Prompt;
use Fripost::Schema;

our $VERSION = '0.01';

sub main {
    my $ldap = shift;
    my $conf = shift;

    my $username = shift;
    prompt_if_undefined ( "Username: ", \$username,
        [ rewrite => sub { fix_username $_ }
        , 'Invalid e-mail' => sub { Email::Valid->address($_) }
        , 'Unknown domain' => sub { $ldap->domain->search({
                                        domain => (split /\@/, $_, 2)[1]
                                    })->count }
        , 'Unknown user'   => sub { $ldap->user->search({
                                       username => $_
                                    })->count }
        ]
    );

    my $password   = $conf->{password};
    $password    //= hash( prompt_password() );


    if ($conf->{pretend}) {
        say STDERR "Did not change password since we are pretending."
            if $conf->{verbose} or $conf->{debug};
    }
    else {
        # Change the password.
        $ldap->user->passwd({ username => $username,
                              userPassword => $password });
        say "Updated password for $username.";
    }
}


=head1 AUTHOR

Stefan Kangas C<< <skangas at skangas.se> >>

Guilhem Moulin C<< <guilhem at fripost.org> >>

=head1 COPYRIGHT

Copyright 2010 Stefan Kangas.

Copyright 2012 Guilhem Moulin.

=head1 LICENSE

This program is free software; you can redistribute it and/or modify it
under the same terms as perl itself.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

=cut

1; # End of user_passwd.pm

__END__