MKDoc::Auth::Plugin::Signup - Let users open new user accounts.


MKDoc-Auth documentation Contained in the MKDoc-Auth distribution.

Index


Code Index:

NAME

Top

MKDoc::Auth::Plugin::Signup - Let users open new user accounts.

SUMMARY

Top

This module lets users open new user accounts.

Rather than creating MKDoc::Auth::User objects, this module creates MKDoc::Auth::TempUser objects and sends an email containing a confirmation link.

MKDoc::Auth::TempUser objects live for a limited amount of time in a disk cache rather than in the database.

When a user visits the confirmation page, the MKDoc::Auth::TempUser is deleted and turned into a regular MKDoc::Auth::User object which lives in the database.

This process has a triple advantage:

Account subscription is truly opt-in.
User email address is guaranteed at the time of subscription.
Your user table is not filled up with bogus user accounts.

INHERITS FROM

Top

MKDoc::Core::Plugin

API

Top

$self->location();

Returns the PATH_INFO which will trigger this plugin.

$self->uri_hint();

Helps deciding what the URI of this plugin should be.

By default, returns 'signup.html'.

Can be overriden by setting the MKD__AUTH_SIGNUP_URI_HINT environment variable or by subclassing.

$self->http_post();

When a form is submitted, it means that a user has filled in the subscription form and pressed OK.

This method processes the POST operation.

It attempts to create a new MKDoc::Auth::TempUser based on the information passed in the signup form.

$self->send_mail();

This method is used when a MKDoc::Auth::TempUser was successfully created.

It sends an email containing a confirmation link (see MKDoc::Auth::Plugin::Confirm) as well as the user details.

TEMPLATE METHODS

Top

self/object

Returns the newly created MKDoc::Auth::TempUser object, if any. If self/object exists, then you can invoke MKDoc::Auth::TempUser methods.

  <ul petal:condition="true: self/object">
    <li>Login: <span petal:replace="self/object/login">fred</span></li>
    <li>Full Name: <span petal:replace="self/object/full_name">Fred FlintStone</span></li>
    <!--? etc. ?-->
  </ul>

self/password

Returns the password which has been generated for the new user.

self/login

Returns the login which has been genereated for the new user.


MKDoc-Auth documentation Contained in the MKDoc-Auth distribution.
package MKDoc::Auth::Plugin::Signup;
use MKDoc::Core::Request;
use MKDoc::Auth::TempUser;
use Crypt::PassGen;
use Petal::Mail;
use strict;
use warnings;
use base qw /MKDoc::Core::Plugin/;


sub location
{
    my $self = shift;
    return '/.' . $self->uri_hint();
}


sub uri_hint
{
    return $ENV{MKD__AUTH_SIGNUP_URI_HINT} || 'signup.html';
}


sub http_post
{
    my $self = shift;
    my $req  = MKDoc::Core::Request->instance();

    my $user = MKDoc::Auth::TempUser->new (
	login      => $self->login(),
        email      => $req->param ('email'),
        full_name  => $req->param ('full_name'),
        password_1 => $self->password(),
        password_2 => $self->password(),
    ) || return $self->http_get();


    $self->set_object ($user);
    $self->send_mail();
    return $self->http_get (@_);
}


sub send_mail
{
    my $self = shift;
    eval
    {
        my $mail = new Petal::Mail (
            language => $self->language(),
            file     => 'auth/emails/signup',
        );

        $mail->send (self => $self);
    };

    $@ and do {
        warn $@;
        new MKDoc::Core::Error 'auth/email/cannot_send';
        return 0;
    };

    return 1;
}


sub set_object
{
    my $self = shift;
    $self->{'.object'} = shift;
}


sub object
{
    my $self = shift;
    return $self->{'.object'};
}


sub password
{
    my $self = shift;
    $self->{'.password'} ||= do {
        my ($pass) = Crypt::PassGen::passgen(); 
        $pass;
    };

    return $self->{'.password'};
}


sub login
{
    my $self  = shift;
    $self->{'.login'} ||= $self->_find_free_login(); 
    return $self->{'.login'};
}


sub _find_free_login
{
    my $self  = shift;
    my $req   = $self->request();

    my $login = $req->param ('email') || '';
    $login    =~ s/\@.+//;
    $login    = lc ($login);
    $login    =~ s/[^a-z]//g;
    $login  ||= 'login';

    length ($login) > 12 && do { $login = substr ($login, 0, 12) };
  
    MKDoc::Auth::TempUser->load_from_login ($login) || return $login;
    my $count = 2;
    my $new_login = $login . $count;
    while (MKDoc::Auth::TempUser->load_from_login ($new_login))
    {
        $count++;
        $new_login = $login . $count;
    }

    $new_login;
}


1;