Indefero

Indefero Commit Details


Date:2009-01-29 11:44:39 (15 years 10 months ago)
Author:Loic d'Anterroches
Branch:dev, develop, feature-issue_links, feature.better-home, feature.content-md5, feature.diff-whitespace, feature.download-md5, feature.issue-links, feature.issue-of-others, feature.issue-summary, feature.search-filter, feature.webrepos, feature.wiki-default-page, master, release-1.1, release-1.2, release-1.3
Commit:766acd01f47ef95fb4fce34d0b5b513773f5de04
Parents: 1307c97ff3845d2c0520505f41b53397ca5eac68
Message: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.
Changes:

File differences

src/IDF/Form/UserAccount.php
2121
2222
2323
24
25
2426
2527
2628
......
5052
5153
5254
55
56
57
58
59
60
61
5362
5463
5564
......
116125
117126
118127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
119153
120154
121155
......
181215
182216
183217
218
219
220
221
222
223
224
225
226
227
228
229
184230
185231
186232
#
# ***** END LICENSE BLOCK ***** */
Pluf::loadFunction('Pluf_HTTP_URL_urlForView');
/**
* Allow a user to update its details.
*/
),
));
$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'),
} 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();
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.
*/
src/IDF/Form/UserChangeEmail.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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']));
}
}
src/IDF/Views/User.php
9696
9797
9898
99
100
10199
102100
103101
......
140138
141139
142140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
143190
144191
145192
'issues' => $pag,
),
$request);
}
/**
}
/**
* Enter the key to change an email address.
*
* This is redirecting to changeEmailDo
*/
public $changeEmailInputKey_precond = array('Pluf_Precondition::loginRequired');
public function changeEmailInputKey($request, $match)
{
if ($request->method == 'POST') {
$form = new IDF_Form_UserChangeEmail($request->POST);
if ($form->isValid()) {
$url = $form->save();
return new Pluf_HTTP_Response_Redirect($url);
}
} else {
$form = new IDF_Form_UserChangeEmail();
}
return Pluf_Shortcuts_RenderToResponse('idf/user/changeemail.html',
array('page_title' => __('Confirm The Email Change'),
'form' => $form),
$request);
}
/**
* Really change the email address.
*/
public $changeEmailDo_precond = array('Pluf_Precondition::loginRequired');
public function changeEmailDo($request, $match)
{
$key = $match[1];
$url = Pluf_HTTP_URL_urlForView('IDF_Views_User::changeEmailInputKey');
try {
list($email, $id, $time) = IDF_Form_UserChangeEmail::validateKey($key);
} catch (Pluf_Form_Invalid $e) {
return new Pluf_HTTP_Response_Redirect($url);
}
if ($id != $request->user->id) {
return new Pluf_HTTP_Response_Redirect($url);
}
// Now we have a change link coming from the right user.
$request->user->email = $email;
$request->user->update();
$request->user->setMessage(sprintf(__('Your new email address "%s" has been validated. Thank you!'), Pluf_esc($email)));
$url = Pluf_HTTP_URL_urlForView('IDF_Views_User::myAccount');
return new Pluf_HTTP_Response_Redirect($url);
}
/**
* Public profile of a user.
*/
public function view($request, $match)
src/IDF/conf/urls.php
473473
474474
475475
476
477
478
479
480
481
482
483
484
485
486
487
488
476489
477490
478491
'model' => 'IDF_Views',
'method' => 'passwordRecovery');
$ctl[] = array('regex' => '#^/preferences/email/ik/$#',
'base' => $base,
'priority' => 4,
'model' => 'IDF_Views_User',
'method' => 'changeEmailInputKey');
$ctl[] = array('regex' => '#^/preferences/email/ak/(.*)/$#',
'base' => $base,
'priority' => 4,
'model' => 'IDF_Views_User',
'method' => 'changeEmailDo');
return $ctl;
src/IDF/templates/idf/user/changeemail-email.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{blocktrans}Hello {$user},
To confirm that you want {$email}
to be your new email address, just follow this link:
{$url}
Alternatively, go to this page:
{$urlik}
and provide the following verification key:
{$key}
If you do not want to change your email address,
just ignore this message.
Yours faithfully,
The development team.
{/blocktrans}
src/IDF/templates/idf/user/changeemail.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
{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 'Confirm Your New Email Address'}" name="submit" /> | <a href="{url 'IDF_Views_User::myAccount'}">{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>
</div>
{/block}
{block javascript}<script type="text/javascript">
document.getElementById('id_key').focus()
</script>
{/block}
src/IDF/templates/idf/user/myaccount.html
2828
2929
3030
31
32
33
34
35
36
37
3138
3239
3340
</td>
</tr>
<tr>
<th><strong>{$form.f.email.labelTag}:</strong></th>
<td>{if $form.f.email.errors}{$form.f.email.fieldErrors}{/if}
{$form.f.email|unsafe}<br />
<span class="helptext">{$form.f.email.help_text}</span>
</td>
</tr>
<tr>
<th>{$form.f.language.labelTag}:</th>
<td>{if $form.f.language.errors}{$form.f.language.fieldErrors}{/if}
{$form.f.language|unsafe}

Archive Download the corresponding diff file

Page rendered in 0.09458s using 13 queries.