Added ticket 95, ability to get a new password when forgotten.
This commit is contained in:
86
src/IDF/Form/Password.php
Normal file
86
src/IDF/Form/Password.php
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
100
src/IDF/Form/PasswordInputKey.php
Normal file
100
src/IDF/Form/PasswordInputKey.php
Normal 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);
|
||||
}
|
||||
}
|
135
src/IDF/Form/PasswordReset.php
Normal file
135
src/IDF/Form/PasswordReset.php
Normal 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user