2017-01-03 23:06:14 +01:00
|
|
|
package Lemonldap::NG::Portal::Plugins::Register;
|
|
|
|
|
|
|
|
use strict;
|
2017-01-14 20:31:48 +01:00
|
|
|
use Encode;
|
2017-01-03 23:06:14 +01:00
|
|
|
use Mouse;
|
2017-01-14 20:31:48 +01:00
|
|
|
use POSIX qw(strftime);
|
2018-06-25 22:45:00 +02:00
|
|
|
use Lemonldap::NG::Common::FormEncode;
|
2017-01-03 23:06:14 +01:00
|
|
|
use Lemonldap::NG::Portal::Main::Constants qw(
|
2020-10-09 22:26:00 +02:00
|
|
|
PE_OK
|
|
|
|
PE_MAILOK
|
|
|
|
PE_NOTOKEN
|
|
|
|
PE_MAILERROR
|
2019-02-05 23:12:17 +01:00
|
|
|
PE_BADMAILTOKEN
|
|
|
|
PE_CAPTCHAEMPTY
|
|
|
|
PE_CAPTCHAERROR
|
2020-10-09 22:26:00 +02:00
|
|
|
PE_TOKENEXPIRED
|
2019-02-05 23:12:17 +01:00
|
|
|
PE_MAILCONFIRMOK
|
2020-10-09 22:26:00 +02:00
|
|
|
PE_MALFORMEDUSER
|
2019-02-05 23:12:17 +01:00
|
|
|
PE_REGISTERFORMEMPTY
|
2020-10-09 22:26:00 +02:00
|
|
|
PE_REGISTERFIRSTACCESS
|
|
|
|
PE_REGISTERALREADYEXISTS
|
|
|
|
PE_MAILCONFIRMATION_ALREADY_SENT
|
2017-01-03 23:06:14 +01:00
|
|
|
);
|
|
|
|
|
2021-07-26 23:06:47 +02:00
|
|
|
our $VERSION = '2.0.13';
|
2017-01-03 23:06:14 +01:00
|
|
|
|
2020-10-09 22:26:00 +02:00
|
|
|
extends qw(
|
|
|
|
Lemonldap::NG::Portal::Lib::SMTP
|
|
|
|
Lemonldap::NG::Portal::Main::Plugin
|
|
|
|
Lemonldap::NG::Portal::Lib::_tokenRule
|
|
|
|
);
|
2017-01-03 23:06:14 +01:00
|
|
|
|
2017-01-26 22:42:40 +01:00
|
|
|
# PROPERTIES
|
|
|
|
|
2017-01-26 22:42:42 +01:00
|
|
|
# Sub module (Demo, LDAP,...)
|
2017-01-26 22:42:40 +01:00
|
|
|
has registerModule => ( is => 'rw' );
|
|
|
|
|
2017-01-26 22:42:42 +01:00
|
|
|
# Register url to set in the mail
|
2017-01-26 22:42:40 +01:00
|
|
|
has registerUrl => (
|
|
|
|
is => 'rw',
|
2017-03-27 18:51:18 +02:00
|
|
|
lazy => 1,
|
2017-01-26 22:42:40 +01:00
|
|
|
default => sub {
|
|
|
|
my $p = $_[0]->conf->{portal};
|
|
|
|
$p =~ s#/*$##;
|
|
|
|
return "$p/register";
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2017-01-26 22:42:42 +01:00
|
|
|
# Mail timeout token generator
|
|
|
|
has mailott => (
|
2017-01-23 12:28:13 +01:00
|
|
|
is => 'rw',
|
2017-03-27 18:51:18 +02:00
|
|
|
lazy => 1,
|
2017-01-23 12:28:13 +01:00
|
|
|
default => sub {
|
2019-02-05 23:12:17 +01:00
|
|
|
my $ott =
|
|
|
|
$_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken');
|
2017-12-11 15:32:58 +01:00
|
|
|
$ott->cache(0);
|
2017-01-28 13:58:22 +01:00
|
|
|
$ott->timeout( $_[0]->conf->{registerTimeout}
|
2019-02-05 23:12:17 +01:00
|
|
|
|| $_[0]->conf->{timeout} );
|
2017-01-23 12:34:38 +01:00
|
|
|
return $ott;
|
2017-01-23 12:28:13 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2017-01-26 22:42:42 +01:00
|
|
|
# Form timout token generator (used if requireToken is set)
|
|
|
|
has ott => ( is => 'rw' );
|
|
|
|
|
|
|
|
# Captcha generator
|
2017-01-26 22:42:40 +01:00
|
|
|
has captcha => ( is => 'rw' );
|
|
|
|
|
2017-01-03 23:06:14 +01:00
|
|
|
# INITIALIZATION
|
|
|
|
|
|
|
|
sub init {
|
|
|
|
my ($self) = @_;
|
2017-01-26 22:42:40 +01:00
|
|
|
|
|
|
|
# Declare REST route
|
2017-01-14 09:17:45 +01:00
|
|
|
$self->addUnauthRoute( register => 'register', [ 'POST', 'GET' ] );
|
2017-01-26 22:42:40 +01:00
|
|
|
|
|
|
|
# Initialize Captcha if needed
|
2017-01-03 23:06:14 +01:00
|
|
|
if ( $self->conf->{captcha_register_enabled} ) {
|
2017-01-26 22:42:40 +01:00
|
|
|
$self->captcha( $self->p->loadModule('::Lib::Captcha') ) or return 0;
|
2017-01-26 22:42:42 +01:00
|
|
|
}
|
2017-01-03 23:06:14 +01:00
|
|
|
|
2017-01-26 22:42:42 +01:00
|
|
|
# Initialize form token if needed (captcha provides also a token)
|
2019-04-05 20:03:42 +02:00
|
|
|
else {
|
2017-01-26 22:42:42 +01:00
|
|
|
$_[0]->ott(
|
|
|
|
$_[0]->p->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken') )
|
2019-02-05 23:12:17 +01:00
|
|
|
or return 0;
|
2017-01-26 22:42:42 +01:00
|
|
|
$_[0]->ott->timeout( $_[0]->conf->{formTimeout} );
|
2017-01-03 23:06:14 +01:00
|
|
|
}
|
2017-01-26 22:42:40 +01:00
|
|
|
|
2017-01-26 22:42:42 +01:00
|
|
|
# Load registered module
|
2017-01-14 09:17:45 +01:00
|
|
|
$self->registerModule(
|
|
|
|
$self->p->loadPlugin( '::Register::' . $self->conf->{registerDB} ) )
|
2019-02-05 23:12:17 +01:00
|
|
|
or return 0;
|
2017-01-03 23:06:14 +01:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
# RUNNIG METHODS
|
|
|
|
|
2017-01-27 06:51:19 +01:00
|
|
|
# Handle register requests
|
2017-01-03 23:06:14 +01:00
|
|
|
sub register {
|
|
|
|
my ( $self, $req ) = @_;
|
2017-01-26 22:42:42 +01:00
|
|
|
|
2018-01-18 18:07:58 +01:00
|
|
|
$self->p->controlUrl($req);
|
|
|
|
|
2017-01-26 22:42:42 +01:00
|
|
|
# Check parameters
|
2017-01-03 23:06:14 +01:00
|
|
|
$req->error( $self->_register($req) );
|
2017-01-26 22:42:42 +01:00
|
|
|
|
|
|
|
# Display form
|
2017-01-03 23:06:14 +01:00
|
|
|
my ( $tpl, $prms ) = $self->display($req);
|
|
|
|
return $self->p->sendHtml( $req, $tpl, params => $prms );
|
|
|
|
}
|
|
|
|
|
2017-01-26 22:42:42 +01:00
|
|
|
# Parameters check
|
2017-01-03 23:06:14 +01:00
|
|
|
sub _register {
|
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
|
2017-01-26 22:42:42 +01:00
|
|
|
# Check if it's a first access
|
2017-03-04 09:36:26 +01:00
|
|
|
unless ( ( $req->method =~ /^POST$/i and $req->param('mail') )
|
|
|
|
or $req->param('register_token') )
|
|
|
|
{
|
2017-01-26 22:42:42 +01:00
|
|
|
|
|
|
|
# Set captcha or token
|
|
|
|
$self->setSecurity($req);
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug('First access to register form');
|
2017-01-28 13:58:22 +01:00
|
|
|
return PE_REGISTERFIRSTACCESS if ( $req->method eq 'GET' );
|
2017-01-03 23:06:14 +01:00
|
|
|
return PE_REGISTERFORMEMPTY;
|
|
|
|
}
|
|
|
|
|
2017-01-26 22:42:42 +01:00
|
|
|
# Get register token (mail link)
|
2018-07-05 22:56:16 +02:00
|
|
|
$req->data->{register_token} = $req->param('register_token');
|
2017-01-03 23:06:14 +01:00
|
|
|
|
|
|
|
# If a register token is present, find the corresponding info
|
2018-07-05 22:56:16 +02:00
|
|
|
if ( $req->data->{register_token} ) {
|
2017-01-03 23:06:14 +01:00
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
2021-07-28 12:10:55 +02:00
|
|
|
"Token provided for register: " . $req->data->{register_token} );
|
2017-01-03 23:06:14 +01:00
|
|
|
|
|
|
|
# Get the corresponding session
|
2019-02-05 23:12:17 +01:00
|
|
|
if ( my $data =
|
|
|
|
$self->mailott->getToken( $req->data->{register_token} ) )
|
2017-01-23 12:28:13 +01:00
|
|
|
{
|
2017-12-11 22:39:28 +01:00
|
|
|
$self->logger->debug(
|
2018-07-05 22:56:16 +02:00
|
|
|
'Token ' . $req->data->{register_token} . ' found' );
|
2017-01-03 23:06:14 +01:00
|
|
|
foreach (qw(mail firstname lastname ipAddr)) {
|
2018-07-05 22:56:16 +02:00
|
|
|
$req->data->{registerInfo}->{$_} = $data->{$_};
|
2017-01-03 23:06:14 +01:00
|
|
|
}
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug( "User associated to token: "
|
2019-02-05 23:12:17 +01:00
|
|
|
. $req->data->{registerInfo}->{mail} );
|
2017-01-03 23:06:14 +01:00
|
|
|
}
|
2017-01-23 12:28:13 +01:00
|
|
|
else {
|
|
|
|
return PE_BADMAILTOKEN;
|
|
|
|
}
|
2017-01-03 23:06:14 +01:00
|
|
|
}
|
2017-01-26 22:42:42 +01:00
|
|
|
|
|
|
|
# Case else: user tries to register
|
2017-01-03 23:06:14 +01:00
|
|
|
else {
|
|
|
|
|
|
|
|
# Use submitted value
|
2018-07-05 22:56:16 +02:00
|
|
|
$req->data->{registerInfo}->{mail} = $req->param('mail');
|
|
|
|
$req->data->{registerInfo}->{firstname} = $req->param('firstname');
|
|
|
|
$req->data->{registerInfo}->{lastname} = $req->param('lastname');
|
|
|
|
$req->data->{registerInfo}->{ipAddr} = $req->address;
|
2017-01-03 23:06:14 +01:00
|
|
|
|
2017-01-26 22:42:42 +01:00
|
|
|
# Check captcha/token only if register session does not already exist
|
2018-07-05 22:56:16 +02:00
|
|
|
if ( $req->data->{registerInfo}->{mail}
|
2017-01-26 22:42:42 +01:00
|
|
|
and
|
2018-07-05 22:56:16 +02:00
|
|
|
!$self->getRegisterSession( $req->data->{registerInfo}->{mail} ) )
|
2017-01-03 23:06:14 +01:00
|
|
|
{
|
2019-05-11 20:18:43 +02:00
|
|
|
|
2017-01-26 22:42:42 +01:00
|
|
|
# Check if token exists
|
|
|
|
my $token;
|
2019-04-05 20:03:42 +02:00
|
|
|
if ( $self->ottRule->( $req, {} ) or $self->captcha ) {
|
2017-01-26 22:42:42 +01:00
|
|
|
$token = $req->param('token');
|
|
|
|
unless ($token) {
|
|
|
|
$self->setSecurity($req);
|
2017-02-15 15:16:59 +01:00
|
|
|
$self->userLogger->warn('Register try without token');
|
2017-01-26 22:42:42 +01:00
|
|
|
return PE_NOTOKEN;
|
|
|
|
}
|
2017-01-03 23:06:14 +01:00
|
|
|
}
|
|
|
|
|
2017-01-26 22:42:42 +01:00
|
|
|
# Captcha for register form
|
|
|
|
if ( $self->captcha ) {
|
|
|
|
my $captcha = $req->param('captcha');
|
|
|
|
|
|
|
|
unless ($captcha) {
|
2017-02-15 15:16:59 +01:00
|
|
|
$self->userLogger->warn(
|
2017-01-26 22:42:42 +01:00
|
|
|
'Register try with captcha not filled');
|
|
|
|
|
|
|
|
# Set captcha or token
|
|
|
|
$self->setSecurity($req);
|
|
|
|
return PE_CAPTCHAEMPTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check captcha
|
2019-02-05 23:12:17 +01:00
|
|
|
unless ( $self->captcha->validateCaptcha( $token, $captcha ) ) {
|
2017-02-15 15:16:59 +01:00
|
|
|
$self->userLogger->info('Captcha failed: wrong code');
|
2017-01-26 22:42:42 +01:00
|
|
|
|
|
|
|
# Set captcha or token
|
|
|
|
$self->setSecurity($req);
|
|
|
|
return PE_CAPTCHAERROR;
|
|
|
|
}
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Captcha code verified");
|
2017-01-26 22:42:42 +01:00
|
|
|
}
|
2019-04-05 20:03:42 +02:00
|
|
|
elsif ( $self->ottRule->( $req, {} ) ) {
|
2017-01-26 22:42:42 +01:00
|
|
|
unless ( $self->ott->getToken($token) ) {
|
|
|
|
$self->setSecurity($req);
|
2017-02-17 21:47:01 +01:00
|
|
|
$self->userLogger->notice(
|
|
|
|
'Register try with expired/bad token');
|
2017-01-26 22:42:42 +01:00
|
|
|
return PE_TOKENEXPIRED;
|
|
|
|
}
|
2017-01-03 23:06:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check mail
|
|
|
|
return PE_MALFORMEDUSER
|
2019-02-05 23:12:17 +01:00
|
|
|
unless ( $req->data->{registerInfo}->{mail} =~
|
|
|
|
m/$self->{conf}->{userControl}/o );
|
2017-01-03 23:06:14 +01:00
|
|
|
|
|
|
|
# Search for user using UserDB module
|
|
|
|
# If the user already exists, register is forbidden
|
2018-07-05 22:56:16 +02:00
|
|
|
$req->user( $req->data->{registerInfo}->{mail} );
|
2018-01-22 21:57:48 +01:00
|
|
|
if ( $self->p->_userDB->getUser( $req, useMail => 1 ) == PE_OK ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->userLogger->error(
|
2019-02-05 23:12:17 +01:00
|
|
|
"Register: refuse mail $req->{data}->{registerInfo}->{mail} because already exists in UserDB"
|
2017-01-03 23:06:14 +01:00
|
|
|
);
|
|
|
|
return PE_REGISTERALREADYEXISTS;
|
|
|
|
}
|
2019-02-05 23:12:17 +01:00
|
|
|
my $register_session =
|
|
|
|
$self->getRegisterSession( $req->data->{registerInfo}->{mail} );
|
|
|
|
$req->data->{mail_already_sent} =
|
|
|
|
( $register_session and !$req->id ) ? 1 : 0;
|
2017-01-03 23:06:14 +01:00
|
|
|
|
|
|
|
# Skip this step if confirmation was already sent
|
2018-07-05 22:56:16 +02:00
|
|
|
unless ( $req->data->{register_token} or $register_session ) {
|
2017-01-03 23:06:14 +01:00
|
|
|
|
2017-01-26 22:42:42 +01:00
|
|
|
# Create mail token
|
2019-02-07 09:27:56 +01:00
|
|
|
$register_session = $self->mailott->createToken( {
|
2019-02-05 23:12:17 +01:00
|
|
|
mail => $req->data->{registerInfo}->{mail},
|
2018-07-05 22:56:16 +02:00
|
|
|
firstname => $req->data->{registerInfo}->{firstname},
|
|
|
|
lastname => $req->data->{registerInfo}->{lastname},
|
|
|
|
ipAddr => $req->data->{registerInfo}->{ipAddr},
|
2017-01-23 12:28:13 +01:00
|
|
|
_type => 'register',
|
|
|
|
}
|
|
|
|
);
|
2017-12-11 22:39:28 +01:00
|
|
|
$self->logger->debug("Token $register_session created");
|
2017-01-03 23:06:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Send confirmation mail
|
|
|
|
|
|
|
|
# Skip this step if user clicked on the confirmation link
|
2018-07-05 22:56:16 +02:00
|
|
|
unless ( $req->data->{register_token} ) {
|
2017-01-03 23:06:14 +01:00
|
|
|
|
|
|
|
# Check if confirmation mail has already been sent
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug('No register_token');
|
2017-01-03 23:06:14 +01:00
|
|
|
|
|
|
|
# Read session to get creation and expiration dates
|
|
|
|
$req->id($register_session) unless $req->id;
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Register session found: $register_session");
|
2017-01-03 23:06:14 +01:00
|
|
|
|
|
|
|
# Mail session expiration date
|
2019-02-05 23:12:17 +01:00
|
|
|
my $expTimestamp =
|
2021-07-28 10:38:45 +02:00
|
|
|
( $self->conf->{registerTimeout} || $self->conf->{timeout} ) + time;
|
2017-01-03 23:06:14 +01:00
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Register expiration timestamp: $expTimestamp");
|
2017-01-03 23:06:14 +01:00
|
|
|
|
2019-02-05 23:12:17 +01:00
|
|
|
$req->data->{expMailDate} =
|
|
|
|
strftime( "%d/%m/%Y", localtime $expTimestamp );
|
|
|
|
$req->data->{expMailTime} =
|
|
|
|
strftime( "%H:%M", localtime $expTimestamp );
|
2017-01-03 23:06:14 +01:00
|
|
|
|
|
|
|
# Mail session start date
|
2017-01-23 12:28:13 +01:00
|
|
|
my $startTimestamp = time;
|
2017-01-03 23:06:14 +01:00
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Register start timestamp: $startTimestamp");
|
2017-01-03 23:06:14 +01:00
|
|
|
|
2019-02-05 23:12:17 +01:00
|
|
|
$req->data->{startMailDate} =
|
|
|
|
strftime( "%d/%m/%Y", localtime $startTimestamp );
|
|
|
|
$req->data->{startMailTime} =
|
|
|
|
strftime( "%H:%M", localtime $startTimestamp );
|
2017-01-03 23:06:14 +01:00
|
|
|
|
|
|
|
# Ask if user want another confirmation email
|
2018-07-05 22:56:16 +02:00
|
|
|
if ( $req->data->{mail_already_sent}
|
2017-01-14 20:31:48 +01:00
|
|
|
and !$req->param('resendconfirmation') )
|
2017-01-03 23:06:14 +01:00
|
|
|
{
|
|
|
|
return PE_MAILCONFIRMATION_ALREADY_SENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Build confirmation url
|
2018-10-19 11:29:11 +02:00
|
|
|
my $req_url = $req->data->{_url};
|
|
|
|
my $skin = $self->p->getSkin($req);
|
2019-02-05 23:12:17 +01:00
|
|
|
my $url =
|
|
|
|
$self->registerUrl . '?'
|
|
|
|
. build_urlencoded(
|
2018-06-25 22:45:00 +02:00
|
|
|
register_token => $req->{id},
|
2018-10-19 11:29:11 +02:00
|
|
|
skin => $skin,
|
|
|
|
( $req_url ? ( url => $req_url ) : () ),
|
2019-02-05 23:12:17 +01:00
|
|
|
);
|
2017-01-03 23:06:14 +01:00
|
|
|
|
|
|
|
# Build mail content
|
2017-02-19 09:07:21 +01:00
|
|
|
my $tr = $self->translate($req);
|
2017-01-03 23:06:14 +01:00
|
|
|
my $subject = $self->conf->{registerConfirmSubject};
|
2017-02-19 09:07:21 +01:00
|
|
|
unless ($subject) {
|
2021-07-27 00:27:21 +02:00
|
|
|
$self->logger->debug('Use default confirm subject');
|
2017-02-19 09:07:21 +01:00
|
|
|
$subject = 'registerConfirmSubject';
|
|
|
|
$tr->( \$subject );
|
|
|
|
}
|
2021-07-26 23:06:47 +02:00
|
|
|
my ( $body, $html );
|
|
|
|
if ( $self->conf->{registerConfirmBody} ) {
|
2017-01-03 23:06:14 +01:00
|
|
|
|
2021-07-26 23:06:47 +02:00
|
|
|
# We use a specific text message, no html
|
2021-07-27 00:27:21 +02:00
|
|
|
$self->logger->debug('Use specific confirm body message');
|
2021-07-26 23:06:47 +02:00
|
|
|
$body = $self->conf->{registerConfirmBody};
|
|
|
|
|
|
|
|
# Replace variables in body
|
|
|
|
$body =~ s/\$url/$url/g;
|
|
|
|
$body =~ s/\$expMailDate/$req->{data}->{expMailDate}/g;
|
|
|
|
$body =~ s/\$expMailTime/$req->{data}->{expMailTime}/g;
|
|
|
|
$body =~ s/\$(\w+)/$req->{data}->{registerInfo}->{$1} || ''/ge;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
# Use HTML template
|
2021-07-27 00:27:21 +02:00
|
|
|
$self->logger->debug('Use default confirm HTML template body');
|
2021-07-26 23:06:47 +02:00
|
|
|
$body = $self->loadMailTemplate(
|
|
|
|
$req,
|
|
|
|
'mail_register_confirm',
|
|
|
|
filter => $tr,
|
|
|
|
params => {
|
|
|
|
expMailDate => $req->data->{expMailDate},
|
|
|
|
expMailTime => $req->data->{expMailTime},
|
|
|
|
url => $url,
|
|
|
|
%{ $req->data->{registerInfo} || {} },
|
|
|
|
},
|
|
|
|
);
|
|
|
|
$html = 1;
|
|
|
|
}
|
2017-01-03 23:06:14 +01:00
|
|
|
|
|
|
|
# Send mail
|
|
|
|
return PE_MAILERROR
|
2019-02-05 23:12:17 +01:00
|
|
|
unless $self->send_mail( $req->data->{registerInfo}->{mail},
|
2017-01-03 23:06:14 +01:00
|
|
|
$subject, $body, $html );
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug('Register message sent');
|
2017-01-14 09:17:45 +01:00
|
|
|
return PE_MAILCONFIRMOK;
|
2017-01-03 23:06:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Generate a complex password
|
|
|
|
my $password = $self->gen_password( $self->conf->{randomPasswordRegexp} );
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug( "Generated password: " . $password );
|
2017-01-03 23:06:14 +01:00
|
|
|
|
2018-07-05 22:56:16 +02:00
|
|
|
$req->data->{registerInfo}->{password} = $password;
|
|
|
|
$req->data->{forceReset} = 1;
|
2017-01-03 23:06:14 +01:00
|
|
|
|
|
|
|
# Find a login
|
2017-01-14 09:17:45 +01:00
|
|
|
my $result = $self->registerModule->computeLogin($req);
|
2017-01-03 23:06:14 +01:00
|
|
|
unless ( $result == PE_OK ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error( "Could not compute login for "
|
2019-02-05 23:12:17 +01:00
|
|
|
. $req->data->{registerInfo}->{mail} );
|
2017-01-03 23:06:14 +01:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Create user
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
2018-07-05 22:56:16 +02:00
|
|
|
'Create new user ' . $req->data->{registerInfo}->{login} );
|
2017-01-03 23:06:14 +01:00
|
|
|
$result = $self->registerModule->createUser($req);
|
|
|
|
unless ( $result == PE_OK ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error(
|
2018-07-05 22:56:16 +02:00
|
|
|
"Could not create user " . $req->data->{registerInfo}->{login} );
|
2017-01-03 23:06:14 +01:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2018-01-18 18:07:58 +01:00
|
|
|
# Build portal url
|
|
|
|
my $url = $self->conf->{portal};
|
|
|
|
$url =~ s#/*$##;
|
2018-10-19 11:29:11 +02:00
|
|
|
my $req_url = $req->data->{_url};
|
|
|
|
my $skin = $self->p->getSkin($req);
|
2018-06-25 22:45:00 +02:00
|
|
|
$url .= '/?'
|
2019-02-05 23:12:17 +01:00
|
|
|
. build_urlencoded(
|
2018-10-19 11:29:11 +02:00
|
|
|
skin => $skin,
|
|
|
|
( $req_url ? ( url => $req_url ) : () ),
|
2019-02-05 23:12:17 +01:00
|
|
|
);
|
2018-01-18 18:07:58 +01:00
|
|
|
|
2021-07-26 23:06:47 +02:00
|
|
|
# Build mail content
|
|
|
|
my $tr = $self->translate($req);
|
|
|
|
my $subject = $self->conf->{registerDoneSubject};
|
|
|
|
unless ($subject) {
|
2021-07-27 00:27:21 +02:00
|
|
|
$self->logger->debug('Use default done subject');
|
2021-07-26 23:06:47 +02:00
|
|
|
$subject = 'registerDoneSubject';
|
|
|
|
$tr->( \$subject );
|
|
|
|
}
|
|
|
|
my ( $body, $html );
|
|
|
|
if ( $self->conf->{registerDoneBody} ) {
|
|
|
|
|
|
|
|
# We use a specific text message, no html
|
2021-07-27 00:27:21 +02:00
|
|
|
$self->logger->debug('Use specific done body message');
|
2021-07-26 23:06:47 +02:00
|
|
|
$body = $self->conf->{registerDoneBody};
|
|
|
|
|
|
|
|
# Replace variables in body
|
|
|
|
$body =~ s/\$url/$url/g;
|
|
|
|
$body =~ s/\$(\w+)/$req->{data}->{registerInfo}->{$1} || ''/ge;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
# Use HTML template
|
2021-07-27 00:27:21 +02:00
|
|
|
$self->logger->debug('Use default done HTML template body');
|
2021-07-26 23:06:47 +02:00
|
|
|
$body = $self->loadMailTemplate(
|
|
|
|
$req,
|
|
|
|
'mail_register_done',
|
|
|
|
filter => $tr,
|
|
|
|
params => {
|
|
|
|
url => $url,
|
|
|
|
%{ $req->data->{registerInfo} || {} },
|
|
|
|
},
|
|
|
|
);
|
|
|
|
$html = 1;
|
|
|
|
}
|
2021-03-31 12:08:24 +02:00
|
|
|
|
2017-01-03 23:06:14 +01:00
|
|
|
# Send mail
|
|
|
|
return PE_MAILERROR
|
2019-02-05 23:12:17 +01:00
|
|
|
unless $self->send_mail( $req->data->{registerInfo}->{mail},
|
2017-01-14 20:31:48 +01:00
|
|
|
$subject, $body, $html );
|
2017-01-03 23:06:14 +01:00
|
|
|
|
|
|
|
return PE_MAILOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub display {
|
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
my %templateParams = (
|
2018-12-17 21:31:38 +01:00
|
|
|
SKIN_PATH => $self->conf->{staticPrefix},
|
2017-02-28 19:43:27 +01:00
|
|
|
SKIN => $self->p->getSkin($req),
|
2017-01-03 23:06:14 +01:00
|
|
|
SKIN_BG => $self->conf->{portalSkinBackground},
|
2018-10-15 19:37:29 +02:00
|
|
|
MAIN_LOGO => $self->conf->{portalMainLogo},
|
2017-01-03 23:06:14 +01:00
|
|
|
AUTH_ERROR => $req->error,
|
|
|
|
AUTH_ERROR_TYPE => $req->error_type,
|
2022-02-01 22:25:28 +01:00
|
|
|
AUTH_ERROR_ROLE => $req->error_role,
|
2018-07-05 22:56:16 +02:00
|
|
|
AUTH_URL => $req->data->{_url},
|
2017-01-03 23:06:14 +01:00
|
|
|
CHOICE_PARAM => $self->conf->{authChoiceParam},
|
2018-07-05 22:56:16 +02:00
|
|
|
CHOICE_VALUE => $req->data->{_authChoice},
|
|
|
|
EXPMAILDATE => $req->data->{expMailDate},
|
|
|
|
EXPMAILTIME => $req->data->{expMailTime},
|
|
|
|
STARTMAILDATE => $req->data->{startMailDate},
|
|
|
|
STARTMAILTIME => $req->data->{startMailTime},
|
|
|
|
MAILALREADYSENT => $req->data->{mail_already_sent},
|
2019-02-05 23:12:17 +01:00
|
|
|
MAIL => $self->p->checkXSSAttack(
|
|
|
|
'mail', $req->data->{registerInfo}->{mail}
|
|
|
|
) ? ""
|
2018-07-05 22:56:16 +02:00
|
|
|
: $req->data->{registerInfo}->{mail},
|
2017-01-03 23:06:14 +01:00
|
|
|
FIRSTNAME => $self->p->checkXSSAttack( 'firstname',
|
2018-07-05 22:56:16 +02:00
|
|
|
$req->data->{registerInfo}->{firstname} ) ? ""
|
|
|
|
: $req->data->{registerInfo}->{firstname},
|
2017-01-14 20:31:48 +01:00
|
|
|
LASTNAME => $self->p->checkXSSAttack( 'lastname',
|
2018-07-05 22:56:16 +02:00
|
|
|
$req->data->{registerInfo}->{lastname} ) ? ""
|
|
|
|
: $req->data->{registerInfo}->{lastname},
|
2017-01-14 20:31:48 +01:00
|
|
|
REGISTER_TOKEN => $self->p->checkXSSAttack( 'register_token',
|
2018-07-05 22:56:16 +02:00
|
|
|
$req->data->{register_token} ) ? ""
|
|
|
|
: $req->data->{register_token},
|
2017-01-03 23:06:14 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
# Display form the first time
|
2019-02-07 09:27:56 +01:00
|
|
|
if ( (
|
2019-02-05 23:12:17 +01:00
|
|
|
$req->error == PE_REGISTERFORMEMPTY
|
2017-01-03 23:06:14 +01:00
|
|
|
or $req->error == PE_REGISTERFIRSTACCESS
|
|
|
|
or $req->error == PE_CAPTCHAERROR
|
|
|
|
or $req->error == PE_CAPTCHAEMPTY
|
2017-01-26 22:42:42 +01:00
|
|
|
or $req->error == PE_NOTOKEN
|
|
|
|
or $req->error == PE_TOKENEXPIRED
|
2017-01-03 23:06:14 +01:00
|
|
|
)
|
|
|
|
and !$req->param('mail_token')
|
2019-02-05 23:12:17 +01:00
|
|
|
)
|
2018-12-17 22:45:37 +01:00
|
|
|
{
|
2017-01-03 23:06:14 +01:00
|
|
|
%templateParams = (
|
|
|
|
%templateParams,
|
|
|
|
DISPLAY_FORM => 1,
|
|
|
|
DISPLAY_RESEND_FORM => 0,
|
|
|
|
DISPLAY_CONFIRMMAILSENT => 0,
|
|
|
|
DISPLAY_MAILSENT => 0,
|
|
|
|
DISPLAY_PASSWORD_FORM => 0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Display captcha if it's enabled
|
2017-01-26 22:42:42 +01:00
|
|
|
if ( $req->captcha ) {
|
2019-07-02 20:03:40 +02:00
|
|
|
$templateParams{CAPTCHA_SRC} = $req->captcha;
|
2017-01-26 22:42:42 +01:00
|
|
|
$templateParams{CAPTCHA_SIZE} = $self->conf->{captcha_size} || 6;
|
|
|
|
}
|
|
|
|
if ( $req->token ) {
|
|
|
|
$templateParams{TOKEN} = $req->token;
|
2017-01-03 23:06:14 +01:00
|
|
|
}
|
|
|
|
|
2018-12-17 22:45:37 +01:00
|
|
|
if ( $req->error == PE_REGISTERALREADYEXISTS ) {
|
|
|
|
%templateParams = (
|
|
|
|
%templateParams,
|
|
|
|
DISPLAY_FORM => 0,
|
|
|
|
DISPLAY_RESEND_FORM => 0,
|
|
|
|
DISPLAY_CONFIRMMAILSENT => 0,
|
|
|
|
DISPLAY_MAILSENT => 0,
|
|
|
|
DISPLAY_PASSWORD_FORM => 0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-03 23:06:14 +01:00
|
|
|
# Display mail confirmation resent form
|
|
|
|
if ( $req->{error} == PE_MAILCONFIRMATION_ALREADY_SENT ) {
|
|
|
|
%templateParams = (
|
|
|
|
%templateParams,
|
|
|
|
DISPLAY_FORM => 0,
|
|
|
|
DISPLAY_RESEND_FORM => 1,
|
|
|
|
DISPLAY_CONFIRMMAILSENT => 0,
|
|
|
|
DISPLAY_MAILSENT => 0,
|
|
|
|
DISPLAY_PASSWORD_FORM => 0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Display confirmation mail sent
|
|
|
|
if ( $req->{error} == PE_MAILCONFIRMOK ) {
|
|
|
|
%templateParams = (
|
|
|
|
%templateParams,
|
|
|
|
DISPLAY_FORM => 0,
|
|
|
|
DISPLAY_RESEND_FORM => 0,
|
|
|
|
DISPLAY_CONFIRMMAILSENT => 1,
|
|
|
|
DISPLAY_MAILSENT => 0,
|
|
|
|
DISPLAY_PASSWORD_FORM => 0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Display mail sent
|
|
|
|
if ( $req->{error} == PE_MAILOK ) {
|
|
|
|
%templateParams = (
|
|
|
|
%templateParams,
|
|
|
|
DISPLAY_FORM => 0,
|
|
|
|
DISPLAY_RESEND_FORM => 0,
|
|
|
|
DISPLAY_CONFIRMMAILSENT => 0,
|
|
|
|
DISPLAY_MAILSENT => 1,
|
|
|
|
DISPLAY_PASSWORD_FORM => 0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Display password change form
|
|
|
|
if ( $req->param('mail_token')
|
|
|
|
and $req->{error} != PE_MAILERROR
|
|
|
|
and $req->{error} != PE_BADMAILTOKEN
|
|
|
|
and $req->{error} != PE_MAILOK )
|
|
|
|
{
|
|
|
|
%templateParams = (
|
|
|
|
%templateParams,
|
|
|
|
DISPLAY_FORM => 0,
|
|
|
|
DISPLAY_RESEND_FORM => 0,
|
|
|
|
DISPLAY_CONFIRMMAILSENT => 0,
|
|
|
|
DISPLAY_MAILSENT => 0,
|
|
|
|
DISPLAY_PASSWORD_FORM => 1,
|
|
|
|
);
|
|
|
|
}
|
2019-01-31 14:22:15 +01:00
|
|
|
|
2017-01-03 23:06:14 +01:00
|
|
|
return ( 'register', \%templateParams );
|
|
|
|
}
|
|
|
|
|
2017-01-26 22:42:42 +01:00
|
|
|
sub setSecurity {
|
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
if ( $self->captcha ) {
|
|
|
|
$self->captcha->setCaptcha($req);
|
|
|
|
}
|
2019-04-05 20:03:42 +02:00
|
|
|
elsif ( $self->ottRule->( $req, {} ) ) {
|
2017-01-26 22:42:42 +01:00
|
|
|
$self->ott->setToken($req);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-03 23:06:14 +01:00
|
|
|
1;
|