* A token is sent when user ask for password reset * The token is linked to an apache session * The password is reset if the token is validenvironments/ppa-mbqj77/deployments/1
parent
26db0f0d42
commit
f6c250207c
@ -0,0 +1,42 @@ |
||||
#!/usr/bin/perl |
||||
|
||||
use Lemonldap::NG::Portal::MailReset; |
||||
use HTML::Template; |
||||
use strict; |
||||
|
||||
my $skin_dir = "__SKINDIR__"; |
||||
|
||||
# Load portal module |
||||
my $portal = Lemonldap::NG::Portal::MailReset->new(); |
||||
|
||||
my $skin = $portal->{portalSkin}; |
||||
my $portal_url = $portal->{portal}; |
||||
|
||||
# Process |
||||
$portal->process(); |
||||
|
||||
# Template creation |
||||
my $template = HTML::Template->new( |
||||
filename => "$skin_dir/$skin/mail.tpl", |
||||
die_on_bad_params => 0, |
||||
cache => 0, |
||||
filter => sub { $portal->translate_template(@_) } |
||||
); |
||||
|
||||
$template->param( PORTAL_URL => "$portal_url" ); |
||||
$template->param( SKIN => "$skin" ); |
||||
$template->param( AUTH_ERROR => $portal->error ); |
||||
$template->param( AUTH_ERROR_TYPE => $portal->error_type ); |
||||
|
||||
# Display form the first time |
||||
$template->param( DISPLAY_FORM => 1 ) |
||||
if ( $portal->{error} == PE_MAILFORMEMPTY |
||||
or ( $portal->{error} == PE_BADCREDENTIALS and !$portal->{mail_token} ) ); |
||||
|
||||
# Display password if change is OK |
||||
$template->param( NEW_PASSWORD => $portal->{reset_password} ) |
||||
if ( $portal->{error} == PE_PASSWORD_OK ); |
||||
|
||||
print $portal->header('text/html; charset=utf8'); |
||||
print $template->output; |
||||
|
@ -0,0 +1,40 @@ |
||||
<TMPL_INCLUDE NAME="header.tpl"> |
||||
|
||||
<div class="message <TMPL_VAR NAME="AUTH_ERROR_TYPE">"><ul><li><TMPL_VAR NAME="AUTH_ERROR"></li></ul></div> |
||||
|
||||
<div class="loginlogo"></div> |
||||
|
||||
<TMPL_IF NAME="DISPLAY_FORM"> |
||||
|
||||
<form action="#" method="post" class="login"> |
||||
|
||||
<h3><lang en="Forgot your password?" fr="Mot de passe oublié ?"/></h3> |
||||
|
||||
<table> |
||||
<tr><th><lang en="Mail" fr="Adresse mail"/></th> |
||||
<td><input name="mail" type="text"/></td> |
||||
</tr> |
||||
<tr><td colspan="2"> |
||||
<div class="buttons"> |
||||
<button type="submit" class="positive"> |
||||
<img src="skins/common/accept.png" alt="" /> |
||||
<lang en="Send me a new password" fr="Envoyez-moi un nouveau mot de passe" /> |
||||
</button> |
||||
</div></td></tr> |
||||
</table> |
||||
|
||||
</form> |
||||
</TMPL_IF> |
||||
|
||||
<div class="link"> |
||||
|
||||
<TMPL_IF NAME="NEW_PASSWORD"> |
||||
<h3><lang en="Your new password is" fr="Votre nouveau mot de passe est"/> <TMPL_VAR NAME="NEW_PASSWORD"></h3> |
||||
</TMPL_IF> |
||||
|
||||
<a href="<TMPL_VAR NAME="PORTAL_URL">"> |
||||
<lang en="Go back to portal" fr="Retourner au portail" /> |
||||
</a> |
||||
</div> |
||||
|
||||
<TMPL_INCLUDE NAME="footer.tpl"> |
@ -0,0 +1,213 @@ |
||||
##@file |
||||
# Module for password reset by mail |
||||
|
||||
##@class Lemonldap::NG::Portal::MailReset |
||||
# Module for password reset by mail |
||||
package Lemonldap::NG::Portal::MailReset; |
||||
|
||||
use strict; |
||||
use warnings; |
||||
|
||||
our $VERSION = '0.1'; |
||||
|
||||
use Lemonldap::NG::Portal::Simple qw(:all); |
||||
use base qw(Lemonldap::NG::Portal::SharedConf Exporter); |
||||
|
||||
*EXPORT_OK = *Lemonldap::NG::Portal::Simple::EXPORT_OK; |
||||
*EXPORT_TAGS = *Lemonldap::NG::Portal::Simple::EXPORT_TAGS; |
||||
*EXPORT = *Lemonldap::NG::Portal::Simple::EXPORT; |
||||
|
||||
##@method boolean process() |
||||
# Call functions to handle password reset by mail issued from |
||||
# - itself: |
||||
# - smtpInit |
||||
# - extractMailInfo |
||||
# - storeMailSession |
||||
# - sendConfirmationMail |
||||
# - portal core module: |
||||
# - setMacros |
||||
# - setLocalGroups |
||||
# - setGroups |
||||
# - userDB module: |
||||
# - userDBInit |
||||
# - getUser |
||||
# - setSessionInfo |
||||
# - passwordDB module: |
||||
# - passwordDBInit |
||||
# - resetPassword |
||||
#@return 1 if all is OK |
||||
sub process { |
||||
my ($self) = @_; |
||||
|
||||
# Process subroutines |
||||
$self->{error} = PE_OK; |
||||
$self->{error} = $self->_subProcess( |
||||
qw(smtpInit userDBInit passwordDBInit extractMailInfo |
||||
getUser setSessionInfo setMacros setLocalGroups setGroups |
||||
storeMailSession sendConfirmationMail resetPassword) |
||||
); |
||||
return ( |
||||
( |
||||
$self->{error} <= 0 |
||||
or $self->{error} == PE_PASSWORD_OK |
||||
or $self->{error} == PE_MAILOK |
||||
) ? 0 : 1 |
||||
); |
||||
} |
||||
|
||||
##@method int smtpInit() |
||||
# Load SMTP methods |
||||
#@return Lemonldap::NG::Portal constant |
||||
sub smtpInit { |
||||
my ($self) = @_; |
||||
eval { use base qw(Lemonldap::NG::Portal::_SMTP) }; |
||||
if ($@) { |
||||
$self->lmLog( "Unable to load SMTP functions ($@)", 'error' ); |
||||
return PE_ERROR; |
||||
} |
||||
|
||||
PE_OK; |
||||
} |
||||
|
||||
##@method int extractMailInfo |
||||
# Get mail from form or from mail_token |
||||
#@return Lemonldap::NG::Portal constant |
||||
sub extractMailInfo { |
||||
my ($self) = @_; |
||||
|
||||
return PE_MAILFORMEMPTY |
||||
unless ( $self->param('mail') || $self->param('mail_token') ); |
||||
|
||||
$self->{mail_token} = $self->param('mail_token'); |
||||
|
||||
# If a mail token is present, find the corresponding mail |
||||
if ( $self->{mail_token} ) { |
||||
|
||||
$self->lmLog( "Token given for password reset: " . $self->{mail_token}, |
||||
'debug' ); |
||||
|
||||
# Get the corresponding session |
||||
my $h = $self->getApacheSession( $self->{mail_token} ); |
||||
|
||||
if ( ref $h ) { |
||||
$self->{mail} = $h->{ $self->{mailSessionKey} }; |
||||
$self->lmLog( "Mail associated to token: " . $self->{mail}, |
||||
'debug' ); |
||||
} |
||||
|
||||
# Mail token can be used only one time, delete the session |
||||
tied(%$h)->delete() if ref $h; |
||||
|
||||
return PE_BADMAILTOKEN unless ( $self->{mail} ); |
||||
} |
||||
else { |
||||
|
||||
# Use submitted value |
||||
$self->{mail} = $self->param('mail'); |
||||
} |
||||
|
||||
PE_OK; |
||||
} |
||||
|
||||
sub storeMailSession { |
||||
my ($self) = @_; |
||||
|
||||
# Skip this step if confirmation was already sent |
||||
return PE_OK if $self->{mail_token}; |
||||
|
||||
# Create a new session |
||||
my $h = $self->getApacheSession(); |
||||
|
||||
# Set _utime for session autoremove |
||||
$h->{_utime} = time(); |
||||
|
||||
# Store mail |
||||
$h->{ $self->{mailSessionKey} } = $self->{mail}; |
||||
|
||||
# Untie session |
||||
untie %$h; |
||||
|
||||
PE_OK; |
||||
} |
||||
|
||||
sub sendConfirmationMail { |
||||
my ($self) = @_; |
||||
|
||||
# Skip this step if confirmation was already sent |
||||
return PE_OK if $self->{mail_token}; |
||||
|
||||
# Build confirmation url |
||||
my $url = $self->{mailUrl} . "?mail_token=" . $self->{id}; |
||||
|
||||
# Replace variables in mail body |
||||
$self->{mailBody} =~ s/\$url/$url/g; |
||||
$self->{mailBody} =~ s/\$(\w+)/$self->{sessionInfo}->{$1}/g; |
||||
|
||||
# Send mail |
||||
return PE_MAILERROR unless $self->send_mail( $self->{mail} ); |
||||
|
||||
PE_MAILOK; |
||||
} |
||||
|
||||
1; |
||||
|
||||
__END__ |
||||
|
||||
=head1 NAME |
||||
|
||||
=encoding utf8 |
||||
|
||||
Lemonldap::NG::Portal::MailReset - Manage password reset by mail |
||||
|
||||
=head1 SYNOPSIS |
||||
|
||||
use Lemonldap::NG::Portal::MailReset; |
||||
|
||||
my $portal = new Lemonldap::NG::Portal::Reset(); |
||||
|
||||
$portal->process(); |
||||
|
||||
# Write here HTML to manage errors and confirmation messages |
||||
|
||||
=head1 DESCRIPTION |
||||
|
||||
Lemonldap::NG::Portal::MailReset enables password reset by mail |
||||
|
||||
See L<Lemonldap::NG::Portal::SharedConf> for a complete example of use of |
||||
Lemonldap::Portal::* libraries. |
||||
|
||||
=head1 METHODS |
||||
|
||||
=head3 process |
||||
|
||||
Main method. |
||||
|
||||
=head1 SEE ALSO |
||||
|
||||
L<Lemonldap::NG::Handler>, L<Lemonldap::NG::Portal::SharedConf>, L<CGI>, |
||||
http://wiki.lemonldap.objectweb.org/xwiki/bin/view/NG/Presentation |
||||
|
||||
=head1 AUTHOR |
||||
|
||||
Clement Oudot, E<lt>clement@oodo.netE<gt> |
||||
|
||||
=head1 BUG REPORT |
||||
|
||||
Use OW2 system to report bug or ask for features: |
||||
L<http://forge.objectweb.org/tracker/?group_id=274> |
||||
|
||||
=head1 DOWNLOAD |
||||
|
||||
Lemonldap::NG is available at |
||||
L<http://forge.objectweb.org/project/showfiles.php?group_id=274> |
||||
|
||||
=head1 COPYRIGHT AND LICENSE |
||||
|
||||
Copyright (C) 2005-2009 by Xavier Guimard E<lt>x.guimard@free.frE<gt> and |
||||
Clement Oudot, E<lt>clement@oodo.netE<gt> |
||||
|
||||
This library is free software; you can redistribute it and/or modify |
||||
it under the same terms as Perl itself, either Perl version 5.8.4 or, |
||||
at your option, any later version of Perl 5 you may have available. |
||||
|
||||
=cut |
Loading…
Reference in new issue