Added ticket 95, ability to get a new password when forgotten.

This commit is contained in:
Loic d'Anterroches 2009-01-01 21:50:16 +01:00
parent c33b271519
commit 99f82216dd
11 changed files with 611 additions and 19 deletions

86
src/IDF/Form/Password.php Normal file
View File

@ -0,0 +1,86 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of InDefero, an open source project management application.
# Copyright (C) 2008 Céondo Ltd and contributors.
#
# InDefero is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# InDefero is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Ask a password recovery.
*
*/
class IDF_Form_Password extends Pluf_Form
{
public function initFields($extra=array())
{
$this->fields['account'] = new Pluf_Form_Field_Varchar(
array('required' => true,
'label' => __('Your login or email'),
'help_text' => __('Provide either your login or your email to recover your password.'),
));
}
/**
* Validate that a user with this login or email exists.
*/
public function clean_account()
{
$account = mb_strtolower(trim($this->cleaned_data['account']));
$sql = new Pluf_SQL('email=%s OR login=%s',
array($account, $account));
$users = Pluf::factory('Pluf_User')->getList(array('filter'=>$sql->gen()));
if ($users->count() == 0) {
throw new Pluf_Form_Invalid(__('Sorry, we cannot find a user with this email address or login. Feel free to try again.'));
}
return $account;
}
/**
* Send the reminder email.
*
*/
function save($commit=true)
{
if (!$this->isValid()) {
throw new Exception(__('Cannot save the model from an invalid form.'));
}
$account = $this->cleaned_data['account'];
$sql = new Pluf_SQL('email=%s OR login=%s',
array($account, $account));
$users = Pluf::factory('Pluf_User')->getList(array('filter'=>$sql->gen()));
foreach ($users as $user) {
$tmpl = new Pluf_Template('idf/user/passrecovery-email.txt');
$cr = new Pluf_Crypt(md5(Pluf::f('secret_key')));
$code = trim($cr->encrypt($user->email.':'.$user->id.':'.time()),
'~');
$code = substr(md5(Pluf::f('secret_key').$code), 0, 2).$code;
$url = Pluf::f('url_base').Pluf_HTTP_URL_urlForView('IDF_Views::passwordRecovery', array($code), array(), false);
$urlic = Pluf::f('url_base').Pluf_HTTP_URL_urlForView('IDF_Views::passwordRecoveryInputCode', array(), array(), false);
$context = new Pluf_Template_Context(array('url' => Pluf_Template::markSafe($url),
'urlik' => Pluf_Template::markSafe($urlic),
'user' => Pluf_Template::markSafe($user),
'key' => Pluf_Template::markSafe($code)));
$email = new Pluf_Mail(Pluf::f('from_email'), $user->email,
__('Password Recovery - InDefero'));
$email->setReturnPath(Pluf::f('bounce_email', Pluf::f('from_email')));
$email->addTextMessage($tmpl->render($context));
$email->sendMail();
}
}
}

View File

@ -0,0 +1,100 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of InDefero, an open source project management application.
# Copyright (C) 2008 Céondo Ltd and contributors.
#
# InDefero is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# InDefero is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
Pluf::loadFunction('Pluf_HTTP_URL_urlForView');
/**
* Check the validity of a confirmation key to reset a password.
*
*/
class IDF_Form_PasswordInputKey extends Pluf_Form
{
public function initFields($extra=array())
{
$this->fields['key'] = new Pluf_Form_Field_Varchar(
array('required' => true,
'label' => __('Your verification key'),
'initial' => '',
'widget_attrs' => array(
'size' => 50,
),
));
}
/**
* Validate the key.
*/
public function clean_key()
{
$this->cleaned_data['key'] = trim($this->cleaned_data['key']);
$error = __('We are sorry but this validation key is not valid. Maybe you should directly copy/paste it from your validation email.');
if (false === ($cres=self::checkKeyHash($this->cleaned_data['key']))) {
throw new Pluf_Form_Invalid($error);
}
$guser = new Pluf_User();
$sql = new Pluf_SQL('email=%s AND id=%s',
array($cres[0], $cres[1]));
if ($guser->getCount(array('filter' => $sql->gen())) != 1) {
throw new Pluf_Form_Invalid($error);
}
if ((time() - $cres[2]) > 86400) {
throw new Pluf_Form_Invalid(__('Sorry, but this verification key has expired, please restart the password recovery sequence. For security reasons, the verification key is only valid 24h.'));
}
return $this->cleaned_data['key'];
}
/**
* Save the model in the database.
*
* @param bool Commit in the database or not. If not, the object
* is returned but not saved in the database.
* @return string Url to redirect to the form.
*/
function save($commit=true)
{
if (!$this->isValid()) {
throw new Exception(__('Cannot save an invalid form.'));
}
return Pluf_HTTP_URL_urlForView('IDF_Views::passwordRecovery',
array($this->cleaned_data['key']));
}
/**
* Return false or an array with the email, id and timestamp.
*
* This is a static function to be reused by other forms.
*
* @param string Confirmation key
* @return mixed Either false or array(email, id, timestamp)
*/
public static function checkKeyHash($key)
{
$hash = substr($key, 0, 2);
$encrypted = substr($key, 2);
if ($hash != substr(md5(Pluf::f('secret_key').$encrypted), 0, 2)) {
return false;
}
$cr = new Pluf_Crypt(md5(Pluf::f('secret_key')));
return split(':', $cr->decrypt($encrypted), 3);
}
}

View File

@ -0,0 +1,135 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of InDefero, an open source project management application.
# Copyright (C) 2008 Céondo Ltd and contributors.
#
# InDefero is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# InDefero is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
Pluf::loadFunction('Pluf_HTTP_URL_urlForView');
/**
* Reset the password of a user.
*
*/
class IDF_Form_PasswordReset extends Pluf_Form
{
protected $user = null;
public function initFields($extra=array())
{
$this->user = $extra['user'];
$this->fields['key'] = new Pluf_Form_Field_Varchar(
array('required' => true,
'label' => __('Your verification key'),
'initial' => $extra['key'],
'widget' => 'Pluf_Form_Widget_HiddenInput',
));
$this->fields['password'] = new Pluf_Form_Field_Varchar(
array('required' => true,
'label' => __('Your password'),
'initial' => '',
'widget' => 'Pluf_Form_Widget_PasswordInput',
'help_text' => __('Your password must be hard for other people to find it, but easy for you to remember.'),
'widget_attrs' => array(
'maxlength' => 50,
'size' => 15,
),
));
$this->fields['password2'] = new Pluf_Form_Field_Varchar(
array('required' => true,
'label' => __('Confirm your password'),
'initial' => '',
'widget' => 'Pluf_Form_Widget_PasswordInput',
'widget_attrs' => array(
'maxlength' => 50,
'size' => 15,
),
));
}
/**
* Check the passwords.
*/
public function clean()
{
if ($this->cleaned_data['password'] != $this->cleaned_data['password2']) {
throw new Pluf_Form_Invalid(__('The two passwords must be the same.'));
}
return $this->cleaned_data;
}
/**
* Validate the key.
*/
public function clean_key()
{
$this->cleaned_data['key'] = trim($this->cleaned_data['key']);
$error = __('We are sorry but this validation key is not valid. Maybe you should directly copy/paste it from your validation email.');
if (false === ($cres=IDF_Form_PasswordInputKey::checkKeyHash($this->cleaned_data['key']))) {
throw new Pluf_Form_Invalid($error);
}
$guser = new Pluf_User();
$sql = new Pluf_SQL('email=%s AND id=%s',
array($cres[0], $cres[1]));
if ($guser->getCount(array('filter' => $sql->gen())) != 1) {
throw new Pluf_Form_Invalid($error);
}
if ((time() - $cres[2]) > 86400) {
throw new Pluf_Form_Invalid(__('Sorry, but this verification key has expired, please restart the password recovery sequence. For security reasons, the verification key is only valid 24h.'));
}
return $this->cleaned_data['key'];
}
function save($commit=true)
{
if (!$this->isValid()) {
throw new Exception(__('Cannot save an invalid form.'));
}
$this->user->setFromFormData($this->cleaned_data);
if ($commit) {
$this->user->update();
/**
* [signal]
*
* Pluf_User::passwordUpdated
*
* [sender]
*
* IDF_Form_PasswordReset
*
* [description]
*
* This signal is sent when the user reset his
* password from the password recovery page.
*
* [parameters]
*
* array('user' => $user)
*
*/
$params = array('user' => $this->user);
Pluf_Signal::send('Pluf_User::passwordUpdated',
'IDF_Form_PasswordReset', $params);
}
return $this->user;
}
}

View File

@ -176,6 +176,98 @@ class IDF_Views
$request); $request);
} }
/**
* Password recovery.
*
* Request the login or the email of the user and if the login or
* email is available in the database, send an email with a key to
* reset the password.
*
*/
function passwordRecoveryAsk($request, $match)
{
$title = __('Password Recovery');
if ($request->method == 'POST') {
$form = new IDF_Form_Password($request->POST);
if ($form->isValid()) {
$form->save();
$url = Pluf_HTTP_URL_urlForView('IDF_Views::passwordRecoveryInputCode');
return new Pluf_HTTP_Response_Redirect($url);
}
} else {
$form = new IDF_Form_Password();
}
return Pluf_Shortcuts_RenderToResponse('idf/user/passrecovery-ask.html',
array('page_title' => $title,
'form' => $form),
$request);
}
/**
* If the key is valid, provide a nice form to reset the password
* and automatically login the user.
*
* This is also firing the password change event for the plugins.
*/
public function passwordRecovery($request, $match)
{
$title = __('Password Recovery');
$key = $match[1];
// first "check", full check is done in the form.
$email_id = IDF_Form_PasswordInputKey::checkKeyHash($key);
if (false == $email_id) {
$url = Pluf_HTTP_URL_urlForView('IDF_Views::passwordRecoveryInputKey');
return new Pluf_HTTP_Response_Redirect($url);
}
$user = new Pluf_User($email_id[1]);
$extra = array('key' => $key,
'user' => $user);
if ($request->method == 'POST') {
$form = new IDF_Form_PasswordReset($request->POST, $extra);
if ($form->isValid()) {
$user = $form->save();
$request->user = $user;
$request->session->clear();
$request->session->setData('login_time', gmdate('Y-m-d H:i:s'));
$user->last_login = gmdate('Y-m-d H:i:s');
$user->update();
$request->user->setMessage(__('Welcome back! Next time, you can use your broswer options to remember the password.'));
$url = Pluf_HTTP_URL_urlForView('IDF_Views::index');
return new Pluf_HTTP_Response_Redirect($url);
}
} else {
$form = new IDF_Form_PasswordReset(null, $extra);
}
return Pluf_Shortcuts_RenderToResponse('idf/user/passrecovery.html',
array('page_title' => $title,
'new_user' => $user,
'form' => $form),
$request);
}
/**
* Just a simple input box to provide the code and redirect to
* passwordRecovery
*/
public function passwordRecoveryInputCode($request, $match)
{
$title = __('Password Recovery');
if ($request->method == 'POST') {
$form = new IDF_Form_PasswordInputKey($request->POST);
if ($form->isValid()) {
$url = $form->save();
return new Pluf_HTTP_Response_Redirect($url);
}
} else {
$form = new IDF_Form_PasswordInputKey();
}
return Pluf_Shortcuts_RenderToResponse('idf/user/passrecovery-inputkey.html',
array('page_title' => $title,
'form' => $form),
$request);
}
/** /**
* FAQ. * FAQ.
*/ */

View File

@ -64,24 +64,6 @@ $ctl[] = array('regex' => '#^/u/(.*)/$#',
'model' => 'IDF_Views_User', 'model' => 'IDF_Views_User',
'method' => 'view'); 'method' => 'view');
$ctl[] = array('regex' => '#^/register/$#',
'base' => $base,
'priority' => 4,
'model' => 'IDF_Views',
'method' => 'register');
$ctl[] = array('regex' => '#^/register/k/(.*)/$#',
'base' => $base,
'priority' => 4,
'model' => 'IDF_Views',
'method' => 'registerConfirmation');
$ctl[] = array('regex' => '#^/register/ik/$#',
'base' => $base,
'priority' => 4,
'model' => 'IDF_Views',
'method' => 'registerInputKey');
$ctl[] = array('regex' => '#^/logout/$#', $ctl[] = array('regex' => '#^/logout/$#',
'base' => $base, 'base' => $base,
'priority' => 4, 'priority' => 4,
@ -435,5 +417,44 @@ $ctl[] = array('regex' => '#^/admin/projects/create/$#',
'model' => 'IDF_Views_Admin', 'model' => 'IDF_Views_Admin',
'method' => 'projectCreate'); 'method' => 'projectCreate');
// ---------- UTILITY VIEWS -------------------------------
$ctl[] = array('regex' => '#^/register/$#',
'base' => $base,
'priority' => 4,
'model' => 'IDF_Views',
'method' => 'register');
$ctl[] = array('regex' => '#^/register/k/(.*)/$#',
'base' => $base,
'priority' => 4,
'model' => 'IDF_Views',
'method' => 'registerConfirmation');
$ctl[] = array('regex' => '#^/register/ik/$#',
'base' => $base,
'priority' => 4,
'model' => 'IDF_Views',
'method' => 'registerInputKey');
$ctl[] = array('regex' => '#^/password/$#',
'base' => $base,
'priority' => 4,
'model' => 'IDF_Views',
'method' => 'passwordRecoveryAsk');
$ctl[] = array('regex' => '#^/password/ik/$#',
'base' => $base,
'priority' => 4,
'model' => 'IDF_Views',
'method' => 'passwordRecoveryInputCode');
$ctl[] = array('regex' => '#^/password/k/(.*)/$#',
'base' => $base,
'priority' => 4,
'model' => 'IDF_Views',
'method' => 'passwordRecovery');
return $ctl; return $ctl;

View File

@ -15,6 +15,7 @@
<p><input name="action" id="action-login" value="login" type="radio" checked="checked" /> <label for="action-login">{trans 'Yes'}</label>, <label for="id_password">{trans 'my password is'}</label> <input type="password" name="password" id="id_password" /></p> <p><input name="action" id="action-login" value="login" type="radio" checked="checked" /> <label for="action-login">{trans 'Yes'}</label>, <label for="id_password">{trans 'my password is'}</label> <input type="password" name="password" id="id_password" /></p>
<p><input type="submit" value="{trans 'Sign in'}" /> <p><input type="submit" value="{trans 'Sign in'}" />
| <a href="{url 'IDF_Views::passwordRecoveryAsk'}">{trans 'I lost my password!'}</a>
</p> </p>
</form> </form>
<script type="text/javascript"> <script type="text/javascript">

View File

@ -51,7 +51,7 @@
<p><strong>{trans 'Did you know?'}</strong><br /> <p><strong>{trans 'Did you know?'}</strong><br />
{aurl 'url', 'IDF_Views::faq'} {aurl 'url', 'IDF_Views::faq'}
{blocktrans}With your account, you will able to participate in the life of all the projects hosted here. Participating in a software project must be fun, so if you have troubles, you can <a href="{$url}">let us know about your issues at anytime</a>!{/blocktrans} {blocktrans}With your account, you will able to participate in the life of all the projects hosted here. Participating in a software project must be fun, so if you have troubles, you can <a href="{$url}">let us know about your issues at anytime</a>!{/blocktrans}</p>
</div> </div>
{/block} {/block}
{block javascript}<script type="text/javascript"> {block javascript}<script type="text/javascript">

View File

@ -0,0 +1,39 @@
{extends "idf/base-simple.html"}
{block body}
{if $form.errors}
<div class="px-message-error">
<p>{trans 'Oups, please check the provided login or email address to recover your password.'}</p>
{if $form.get_top_errors}
{$form.render_top_errors|unsafe}
{/if}
</div>
{/if}
<form method="post" action=".">
<table class="form" summary="">
<tr>
<th><strong>{$form.f.account.labelTag}:</strong></th>
<td>{if $form.f.account.errors}{$form.f.account.fieldErrors}{/if}
{$form.f.account|unsafe}<br />
<span class="helptext">{$form.f.account.help_text}</span>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" value="{trans 'Recover My Password'}" name="submit" /> | <a href="{url 'IDF_Views::index'}">{trans 'Cancel'}</a>
</td>
</tr>
</table>
</form>
{/block}
{block context}
<div class="issue-submit-info">
<p>{trans 'Provide either your login or email address, if a corresponding user is found in the database, we will send you an email with the details on how to reset your password.'}</p>
</div>
{/block}
{block javascript}<script type="text/javascript">
document.getElementById('id_account').focus();
</script>
{/block}

View File

@ -0,0 +1,25 @@
{blocktrans}Hello {$user},
You lost your password and wanted to recover it.
To provide a new password for your account, you
just have to follow the provided link. You will
get a simple form to provide a new password.
{$url}
Alternatively, go to this page:
{$urlik}
and provide the following verification key:
{$key}
If you are not the one who requested to reset
your password, simply ignore this email, your
password will not be changed.
Yours faithfully,
The development team.
{/blocktrans}

View File

@ -0,0 +1,40 @@
{extends "idf/base-simple.html"}
{block body}
{if $form.errors}
<div class="px-message-error">
<p>{trans 'Oups, we found an error in the form.'}</p>
{if $form.get_top_errors}
{$form.render_top_errors|unsafe}
{/if}
</div>
{/if}
<form method="post" action=".">
<table class="form" summary="">
<tr>
<td>&nbsp;</td>
<td><strong>{$form.f.key.labelTag}:</strong><br />
{if $form.f.key.errors}{$form.f.key.fieldErrors}{/if}
{$form.f.key|unsafe}
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" value="{trans 'Recover Your Password'}" name="submit" /> | <a href="{url 'IDF_Views::index'}">{trans 'Cancel'}</a>
</td>
</tr>
</table>
</form>
{/block}
{block context}
<div class="issue-submit-info">
<h2>{trans 'Instructions'}</h2>
<p>{trans 'Use your email software to read your emails and open your verification email. Either click directly on the verification link or copy/paste the verification key in the box and submit the form.'}</p>
<p>{trans 'Just after providing the confirmation key, you will be able to reset your password and use again this website fully.'}</p>
</div>
{/block}
{block javascript}<script type="text/javascript">
document.getElementById('id_key').focus()
</script>
{/block}

View File

@ -0,0 +1,53 @@
{extends "idf/base-simple.html"}
{block body}
{if $form.errors}
<div class="px-message-error">
<p>{trans 'Oups, please check the form for errors.'}</p>
{if $form.get_top_errors}
{$form.render_top_errors|unsafe}
{/if}
{if $form.f.key.errors}{$form.f.key.fieldErrors}{/if}
</div>
{/if}
<form method="post" action=".">
<table class="form" summary="">
<tr>
<th><strong>{trans 'Login:'}</strong></th>
<td>{$new_user.login}</td>
</tr>
<tr>
<th><strong>{trans 'Email:'}</strong></th>
<td>{$new_user.email}</td>
</tr>
<tr>
<th><strong>{$form.f.password.labelTag}:</strong></th>
<td>{if $form.f.password.errors}{$form.f.password.fieldErrors}{/if}
{$form.f.password|unsafe}<br />
<span class="helptext">{$form.f.password.help_text}</span>
</td>
</tr>
<tr>
<th><strong>{$form.f.password2.labelTag}:</strong></th>
<td>{if $form.f.password2.errors}{$form.f.password2.fieldErrors}{/if}
{$form.f.password2|unsafe}
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" value="{trans 'Reset Your Password'}" name="submit" /> | <a href="{url 'IDF_Views::index'}">{trans 'Cancel'}</a>
</td>
</tr>
</table>{$form.f.key|unsafe}
</form>
{/block}
{block context}
<div class="issue-submit-info">
<p>{trans 'This is the last step, but just <strong>be sure to have the cookies enabled</strong> to log in afterwards.'}</p>
</div>
{/block}
{block javascript}<script type="text/javascript">
document.getElementById('id_password').focus()
</script>
{/block}