1use warnings;
2use strict;
3
4=head1 NAME
5
6Jifty::Plugin::Authentication::Password::Action::Signup - signup for an account
7
8=cut
9
10package Jifty::Plugin::Authentication::Password::Action::Signup;
11our @ISA;
12{
13    my $class = Jifty->app_class('Action', 'CreateUser');
14    push @ISA, $class;
15}
16
17=head2 arguments
18
19
20The fields for C<Signup> are:
21
22=over 4
23
24=item email: the email address
25
26=item password and password_confirm: the requested password
27
28=item name: your full name
29
30=back
31
32=cut
33
34sub arguments {
35    my $self = shift;
36    my $args = $self->SUPER::arguments();
37
38
39    my %fields = (
40        name             => 1,
41        email            => 1,
42        password         => 1,
43        password_confirm => 1,
44    );
45
46    for (keys %$args){
47        delete $args->{$_} unless $fields{$_};
48    }
49
50    $args->{'email'}{'ajax_validates'}   = 1;
51    $args->{'email'}{'mandatory'}        = 1;
52    $args->{'name'}{'ajax_validates'}   = 1;
53    $args->{'password_confirm'}{'label'} = _("Type that again?");
54    return $args;
55}
56
57=head2 validate_email
58
59Make sure their email address looks sane
60
61=cut
62
63sub validate_email {
64    my $self  = shift;
65    my $email = shift;
66    my $LoginUser   = Jifty->app_class('Model', 'User');
67    my $CurrentUser   = Jifty->app_class('CurrentUser');
68
69
70    return $self->validation_error( email => _("That doesn't look like an email address.") ) unless ( $email =~ /\S\@\S/ );
71
72    my $u = $LoginUser->new( current_user => $CurrentUser->superuser );
73    $u->load_by_cols( email => $email );
74    if ( $u->id ) {
75        return $self->validation_error( email => _('It looks like you already have an account. Perhaps you want to <a href="/login">log in</a> instead?')
76        );
77    }
78
79    return $self->validation_ok('email');
80}
81
82=head2 take_action
83
84Overrides the virtual C<take_action> method on L<Jifty::Action> to call
85the appropriate C<Jifty::Record>'s C<create> method when the action is
86run, thus creating a new object in the database.
87
88Makes sure that the user only specifies things we want them to.
89
90=cut
91
92sub take_action {
93    my $self   = shift;
94    my $LoginUser   = Jifty->app_class('Model', 'User');
95    my $CurrentUser   = Jifty->app_class('CurrentUser');
96
97    my $record = $LoginUser->new( current_user => $CurrentUser->superuser );
98
99    my %values;
100    $values{$_} = $self->argument_value($_) for grep {
101        defined $self->record->column($_) and defined $self->argument_value($_)
102    } $self->argument_names;
103
104    my ($id, $msg) = $record->create(%values);
105
106    # Handle errors?
107    unless ( $record->id ) {
108        $self->result->error( _("Something bad happened and we couldn't create your account: %1", $msg).' '.  _("Try again later. We're really, really sorry.")
109        );
110        return;
111    }
112
113    $self->result->message( _("Welcome to %1, %2.", Jifty->config->framework('ApplicationName'), $record->name)
114          . ' ' . _("We've sent a confirmation message to your email box.") );
115
116    return 1;
117}
118
1191;
120