Added the ability for a user to change his email address.
The change is not performed immediately. First a confirmation email is sent to the user and if validated, the email address is changed.
This commit is contained in:
@@ -21,6 +21,8 @@
|
||||
#
|
||||
# ***** END LICENSE BLOCK ***** */
|
||||
|
||||
Pluf::loadFunction('Pluf_HTTP_URL_urlForView');
|
||||
|
||||
/**
|
||||
* Allow a user to update its details.
|
||||
*/
|
||||
@@ -50,6 +52,13 @@ class IDF_Form_UserAccount extends Pluf_Form
|
||||
),
|
||||
));
|
||||
|
||||
$this->fields['email'] = new Pluf_Form_Field_Email(
|
||||
array('required' => true,
|
||||
'label' => __('Your mail'),
|
||||
'initial' => $this->user->email,
|
||||
'help_text' => __('If you change your email address, an email will be sent to the new address to confirm it.'),
|
||||
));
|
||||
|
||||
$this->fields['language'] = new Pluf_Form_Field_Varchar(
|
||||
array('required' => true,
|
||||
'label' => __('Language'),
|
||||
@@ -116,6 +125,31 @@ class IDF_Form_UserAccount extends Pluf_Form
|
||||
} else {
|
||||
$update_pass = true;
|
||||
}
|
||||
$old_email = $this->user->email;
|
||||
$new_email = $this->cleaned_data['email'];
|
||||
unset($this->cleaned_data['email']);
|
||||
if ($old_email != $new_email) {
|
||||
$cr = new Pluf_Crypt(md5(Pluf::f('secret_key')));
|
||||
$encrypted = trim($cr->encrypt($new_email.':'.$this->user->id.':'.time()), '~');
|
||||
$key = substr(md5(Pluf::f('secret_key').$encrypted), 0, 2).$encrypted;
|
||||
$url = Pluf::f('url_base').Pluf_HTTP_URL_urlForView('IDF_Views_User::changeEmailDo', array($key), array(), false);
|
||||
$urlik = Pluf::f('url_base').Pluf_HTTP_URL_urlForView('IDF_Views_User::changeEmailInputKey', array(), array(), false);
|
||||
$context = new Pluf_Template_Context(
|
||||
array('key' => Pluf_Template::markSafe($key),
|
||||
'url' => Pluf_Template::markSafe($url),
|
||||
'urlik' => Pluf_Template::markSafe($urlik),
|
||||
'email' => $new_email,
|
||||
'user'=> $this->user,
|
||||
)
|
||||
);
|
||||
$tmpl = new Pluf_Template('idf/user/changeemail-email.txt');
|
||||
$text_email = $tmpl->render($context);
|
||||
$email = new Pluf_Mail(Pluf::f('from_email'), $new_email,
|
||||
__('Confirm your new email address.'));
|
||||
$email->addTextMessage($text_email);
|
||||
$email->sendMail();
|
||||
$this->user->setMessage(sprintf(__('A validation email has been sent to "%s" to validate the email address change.'), Pluf_esc($new_email)));
|
||||
}
|
||||
$this->user->setFromFormData($this->cleaned_data);
|
||||
// Get keys
|
||||
$keys = $this->user->get_idf_key_list();
|
||||
@@ -181,6 +215,18 @@ class IDF_Form_UserAccount extends Pluf_Form
|
||||
return $first_name;
|
||||
}
|
||||
|
||||
function clean_email()
|
||||
{
|
||||
$this->cleaned_data['email'] = mb_strtolower(trim($this->cleaned_data['email']));
|
||||
$guser = new Pluf_User();
|
||||
$sql = new Pluf_SQL('email=%s AND id!=%s',
|
||||
array($this->cleaned_data['email'], $this->user->id));
|
||||
if ($guser->getCount(array('filter' => $sql->gen())) > 0) {
|
||||
throw new Pluf_Form_Invalid(sprintf(__('The email "%s" is already used.'), $this->cleaned_data['email']));
|
||||
}
|
||||
return $this->cleaned_data['email'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check to see if the 2 passwords are the same.
|
||||
*/
|
||||
|
84
src/IDF/Form/UserChangeEmail.php
Normal file
84
src/IDF/Form/UserChangeEmail.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?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 ***** */
|
||||
|
||||
/**
|
||||
* Change the email address of a user.
|
||||
*
|
||||
*/
|
||||
class IDF_Form_UserChangeEmail extends Pluf_Form
|
||||
{
|
||||
protected $user;
|
||||
|
||||
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,
|
||||
),
|
||||
));
|
||||
}
|
||||
|
||||
function clean_key()
|
||||
{
|
||||
self::validateKey($this->cleaned_data['key']);
|
||||
return $this->cleaned_data['key'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the key.
|
||||
*
|
||||
* Throw a Pluf_Form_Invalid exception if the key is not valid.
|
||||
*
|
||||
* @param string Key
|
||||
* @return array array($new_email, $user_id, time())
|
||||
*/
|
||||
public static function validateKey($key)
|
||||
{
|
||||
$hash = substr($key, 0, 2);
|
||||
$encrypted = substr($key, 2);
|
||||
if ($hash != substr(md5(Pluf::f('secret_key').$encrypted), 0, 2)) {
|
||||
throw new Pluf_Form_Invalid(__('The validation key is not valid. Please copy/paste it from your confirmation email.'));
|
||||
}
|
||||
$cr = new Pluf_Crypt(md5(Pluf::f('secret_key')));
|
||||
return split(':', $cr->decrypt($encrypted), 3);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 Object Model with data set from the form.
|
||||
*/
|
||||
function save($commit=true)
|
||||
{
|
||||
if (!$this->isValid()) {
|
||||
throw new Exception(__('Cannot save the model from an invalid form.'));
|
||||
}
|
||||
return Pluf::f('url_base').Pluf_HTTP_URL_urlForView('IDF_Views_User::changeEmailDo', array($this->cleaned_data['key']));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user