MojoMojo
view release on metacpan or search on metacpan
lib/MojoMojo/Controller/User.pm view on Meta::CPAN
if ( $c->pref('use_captcha')
&& ( !( $c->stash->{user} && $c->stash->{user}->is_admin ) ) )
{
my $captcha_lang = $c->session->{lang} || $c->pref('default_lang');
my $captcha = $form->element({
type => 'reCAPTCHA',
name => 'captcha',
recaptcha_options => { lang => $captcha_lang, theme => 'white' }
});
$form->process;
}
$form->model->default_values( $c->stash->{newuser} );
if ( $form->submitted_and_valid ) {
# Need to check if login or email already exists.
if ( $c->forward('is_account_taken') ) {
$c->stash->{account_taken} = $c->loc('Account Taken. Try another.');
$c->detach();
}
$c->stash->{newuser}->active(-1);
# XXX - need to add this so FormFu->model->update properly populates
# the required registered field. The other way to do this is by using
# DBIx::Class::DynamicDefaults, but I've restrained myself from adding
# yet another dependency -lestrrat
# $form->add_valid( registered => time() );
$form->model->update( $c->stash->{newuser} );
$c->stash->{newuser}->insert();
if ( $c->stash->{user} && $c->stash->{user}->is_admin ) {
$c->stash->{newuser}->update({active=>1});
$c->res->redirect( $c->uri_for('/.admin/user') );
}
else {
$c->forward( 'do_register', [ $c->stash->{newuser} ] );
}
}
}
=head2 is_account_taken
Test to see if a login or email is already in use.
=cut
sub is_account_taken : Private {
my ( $self, $c ) = @_;
my $login = $c->request->body_params->{login};
my $email = $c->request->body_params->{email};
my $person_rs =
$c->model('DBIC::Person')
->search( [ { login => $login }, { email => $email } ] );
return $person_rs->count;
}
=head2 do_register ( /.register )
New user registration processing.
B<templates:> C<user/password.tt>, C<user/validate.tt>
=cut
sub do_register : Private {
my ( $self, $c, $user ) = @_;
$c->forward('/user/login');
$c->pref('entropy') || $c->pref( 'entropy', rand );
$c->stash(
secret => md5_hex( $user->email . $c->pref('entropy') ),
email => {
from => $c->config->{system_mail},
to => $user->email,
subject => $c->loc( '~[x~] New User Validation', $c->pref('name') ),
template => 'validate.tt',
},
);
$c->model('DBIC::Page')->create_page($user->link,
$c->loc("# Home node for x\n\nPut whatever you like here.",$user->name),
$user);
$c->forward( $c->view('Email') );
if ( scalar( @{ $c->error } ) ) {
$c->clear_errors;
$c->stash->{error} = $c->loc('An error occourred. Sorry.');
}
$c->stash->{user} = $user;
$c->stash->{template} = 'user/validate.tt';
}
=head2 validate ( /.validate )
Validation of user email. Will accept a md5_hex mailed to the user
earlier. Non-validated users will only be able to log out.
=cut
sub validate : Global {
my ( $self, $c, $user, $check ) = @_;
$user = $c->model("DBIC::Person")->find( { login => $user } );
if ( $user and $check eq md5_hex( $user->email . $c->pref('entropy') ) ) {
$user->update({active=>1});
if ( $c->stash->{user} ) {
$c->res->redirect(
$c->uri_for( '/', $c->stash->{user}->link, '.edit' ) );
}
else {
$c->flash->{message} =
$c->loc( 'Welcome, x your email is validated. Please log in.',
$user->name );
return $c->res->redirect( $c->uri_for('login') );
}
return;
}
$c->stash->{template} = 'user/validate.tt';
}
=head2 reconfirm
Send the confirmation mail again to another address.
=cut
sub reconfirm : Local {
my ( $self, $c ) = @_;
$c->detach('/default') unless $c->req->method eq 'POST';
if ( $c->user->obj->email ne $c->req->param('email') ) {
if ( $c->model('DBIC::Person')
->search( { email => $c->req->param('email') } )->count )
{
return $c->stash->{error} = $c->loc('That mail is already in use');
}
}
my $user = $c->user->obj;
$user->email( $c->req->params->{email} );
$user->active(-1);
$user->update();
$c->forward( 'do_register', [$user] );
$c->flash->{message} = $c->loc('confirmation message resent');
$c->res->redirect( $c->uri_for('/') );
}
=head2 profile ( .profile )
Show user profile.
=cut
sub profile : Global {
my ( $self, $c ) = @_;
my $page = $c->stash->{page};
my $login = (
$c->stash->{proto_pages}[-1]
? $c->stash->{proto_pages}[-1]->{name} # FIXME: why not ->{name_orig}, like in editprofile() ?
: $page->name
);
my $user = $c->model('DBIC::Person')->get_user($login);
if ($user) {
$c->stash->{person} = $user;
$c->stash->{template} = 'user/profile.tt';
}
else {
$c->stash->{template} = 'message.tt';
$c->stash->{message} = $c->loc( 'User not found: x', $login );
}
}
=head2 editprofile
Form to edit a person's profile
=cut
sub editprofile : Global FormConfig {
my ( $self, $c ) = @_;
( run in 0.904 second using v1.01-cache-2.11-cpan-39bf76dae61 )