From dfa223b39e61d381b2117c4505437b4642a3c156 Mon Sep 17 00:00:00 2001 From: Patrick Georgi Date: Mon, 21 Feb 2011 10:50:07 +0100 Subject: [PATCH] Manage addition of secondary emails --- src/IDF/Form/UserAccount.php | 53 ++++++++++++++++++++------------ src/IDF/Form/UserChangeEmail.php | 2 +- src/IDF/Views/User.php | 13 ++++++-- 3 files changed, 44 insertions(+), 24 deletions(-) diff --git a/src/IDF/Form/UserAccount.php b/src/IDF/Form/UserAccount.php index 2a9914f..d850114 100644 --- a/src/IDF/Form/UserAccount.php +++ b/src/IDF/Form/UserAccount.php @@ -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(); diff --git a/src/IDF/Form/UserChangeEmail.php b/src/IDF/Form/UserChangeEmail.php index c9175bd..aa611c0 100644 --- a/src/IDF/Form/UserChangeEmail.php +++ b/src/IDF/Form/UserChangeEmail.php @@ -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) { diff --git a/src/IDF/Views/User.php b/src/IDF/Views/User.php index f31cc5b..739b44c 100644 --- a/src/IDF/Views/User.php +++ b/src/IDF/Views/User.php @@ -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);