From be35fb4f80dfa99cff265ed990dad1b653d677e4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois-Xavier=20Algrain?= <fxalgrain@tiolive.com>
Date: Tue, 26 Apr 2011 07:55:34 +0000
Subject: [PATCH] Add possibility to specifify the sender. Add possibility to
 use a notification message to send password recovery url. Keeps compatibility
 with older usages.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@45677 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/Tool/PasswordTool.py | 57 ++++++++++++++++++++-----------
 1 file changed, 38 insertions(+), 19 deletions(-)

diff --git a/product/ERP5/Tool/PasswordTool.py b/product/ERP5/Tool/PasswordTool.py
index 5247fbf0dc..3e2fdaa4be 100644
--- a/product/ERP5/Tool/PasswordTool.py
+++ b/product/ERP5/Tool/PasswordTool.py
@@ -96,10 +96,17 @@ class PasswordTool(BaseTool):
     self._password_request_dict[random_url] = (user_login, expiration_date)
     return url
 
-  def mailPasswordResetRequest(self, user_login=None, REQUEST=None):
+  def mailPasswordResetRequest(self, user_login=None, REQUEST=None, 
+                              notification_message=None, sender=None):
     """
     Create a random string and expiration date for request
-    """
+    Parameters:
+    user_login -- Reference of the user to send password reset link
+    REQUEST -- Request object
+    notification_message -- Notification Message Document used to build the email. 
+                            As default, a standart text will be used.
+    sender -- Sender (Person or Organisation) of the email.
+            As default, the default email address will be used"""
     if REQUEST is None:
       REQUEST = get_request()
 
@@ -158,23 +165,35 @@ class PasswordTool(BaseTool):
     self._password_request_dict[random_url] = (user_login, expiration_date)
 
     # send mail
-    subject = translateString("[${instance_name}] Reset of your password",
-        mapping={'instance_name': self.getPortalObject().getTitle()})
-    subject = subject.translate()
-    message = translateString("\nYou requested to reset your ${instance_name}"\
-              " account password.\n\n" \
-              "Please copy and paste the following link into your browser: \n"\
-              "${reset_password_link}\n\n" \
-              "Please note that this link will be valid only one time, until "\
-              "${expiration_date}.\n" \
-              "After this date, or after having used this link, you will have to make " \
-              "a new request\n\n" \
-              "Thank you",
-              mapping={'instance_name':self.getPortalObject().getTitle(),
-                       'reset_password_link':url,
-                       'expiration_date':expiration_date})
-    message = message.translate()
-    self.getPortalObject().portal_notifications.sendMessage(sender=None, recipient=[user,], subject=subject, message=message)
+    message_dict = {'instance_name':self.getPortalObject().getTitle(),
+                    'reset_password_link':url,
+                    'expiration_date':expiration_date}
+
+    if notification_message is None:
+      subject = translateString("[${instance_name}] Reset of your password",
+          mapping={'instance_name': self.getPortalObject().getTitle()})
+      subject = subject.translate()
+      message = translateString("\nYou requested to reset your ${instance_name}"\
+                " account password.\n\n" \
+                "Please copy and paste the following link into your browser: \n"\
+                "${reset_password_link}\n\n" \
+                "Please note that this link will be valid only one time, until "\
+                "${expiration_date}.\n" \
+                "After this date, or after having used this link, you will have to make " \
+                "a new request\n\n" \
+                "Thank you",
+                mapping=message_dict)
+      message = message.translate()
+    else:
+      subject = notification_message.getTitle()
+      if notification_message.getContentType() == "text/html":
+        message = notification_message.asEntireHTML(substitution_method_parameter_dict=message_dict)
+      else:
+        message = notification_message.asText(substitution_method_parameter_dict=message_dict)
+
+    self.getPortalObject().portal_notifications.sendMessage(sender=sender, recipient=[user,],
+                                                            subject=subject, message=message)
+                                                            
     if REQUEST is not None:
       msg = translateString("An email has been sent to you.")
       parameter = urlencode(dict(portal_status_message=msg))
-- 
2.30.9