Manage addition of secondary emails

feature.better-home
Patrick Georgi 2011-02-21 10:50:07 +01:00
parent 0a8d771c11
commit dfa223b39e
3 changed files with 44 additions and 24 deletions

View File

@ -172,6 +172,35 @@ class IDF_Form_UserAccount extends Pluf_Form
));
}
private function send_validation_mail($new_email, $secondary_mail=false)
{
if ($secondary_mail) {
$type = "secondary";
} else {
$type = "primary";
}
$cr = new Pluf_Crypt(md5(Pluf::f('secret_key')));
$encrypted = trim($cr->encrypt($new_email.':'.$this->user->id.':'.time().':'.$type), '~');
$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)));
}
/**
* Save the model in the database.
*
@ -195,26 +224,7 @@ class IDF_Form_UserAccount extends Pluf_Form
$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->send_validation_mail($new_email);
}
$this->user->setFromFormData($this->cleaned_data);
// Add key as needed.
@ -226,6 +236,9 @@ class IDF_Form_UserAccount extends Pluf_Form
$key->create();
}
}
if ('' !== $this->cleaned_data['secondary_mail']) {
$this->send_validation_mail($this->cleaned_data['secondary_mail'], true);
}
if ($commit) {
$this->user->update();

View File

@ -53,7 +53,7 @@ class IDF_Form_UserChangeEmail extends Pluf_Form
* Throw a Pluf_Form_Invalid exception if the key is not valid.
*
* @param string Key
* @return array array($new_email, $user_id, time())
* @return array array($new_email, $user_id, time(), [primary|secondary])
*/
public static function validateKey($key)
{

View File

@ -212,7 +212,7 @@ class IDF_Views_User
$key = $match[1];
$url = Pluf_HTTP_URL_urlForView('IDF_Views_User::changeEmailInputKey');
try {
list($email, $id, $time) = IDF_Form_UserChangeEmail::validateKey($key);
list($email, $id, $time, $type) = IDF_Form_UserChangeEmail::validateKey($key);
} catch (Pluf_Form_Invalid $e) {
return new Pluf_HTTP_Response_Redirect($url);
}
@ -220,8 +220,15 @@ class IDF_Views_User
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();
if ($type == "primary") {
$request->user->email = $email;
$request->user->update();
} else {
$mailaddress = new IDF_EmailAddress();
$mailaddress->user = $request->user;
$mailaddress->address = $email;
$mailaddress->create();
}
$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);