From 41ceffd52b8c5cb606c1a57d2985f57eec2a3926 Mon Sep 17 00:00:00 2001
From: Raphael Collet
Date: Mon, 3 Dec 2012 15:44:24 +0100
Subject: [PATCH] [ADD] auth_signup: merge in functionality of
auth_reset_password
bzr revid: rco@openerp.com-20121203144424-39wrr5z78smswox2
---
addons/auth_signup/__openerp__.py | 9 ++--
addons/auth_signup/auth_signup_data.xml | 15 +++++++
addons/auth_signup/controllers/main.py | 21 +++++++++-
addons/auth_signup/res_users.py | 41 ++++++++++++++++---
addons/auth_signup/res_users_view.xml | 12 ++++++
.../auth_signup/static/src/js/auth_signup.js | 29 ++++++++++++-
.../static/src/xml/auth_signup.xml | 3 ++
7 files changed, 120 insertions(+), 10 deletions(-)
diff --git a/addons/auth_signup/__openerp__.py b/addons/auth_signup/__openerp__.py
index 235ac047346..5fe705a4440 100644
--- a/addons/auth_signup/__openerp__.py
+++ b/addons/auth_signup/__openerp__.py
@@ -22,15 +22,18 @@
{
'name': 'Signup',
'description': """
-Allow users to sign up.
-=======================
+Allow users to sign up and reset their password
+===============================================
""",
'author': 'OpenERP SA',
'version': '1.0',
'category': 'Authentication',
'website': 'http://www.openerp.com',
'installable': True,
- 'depends': ['base_setup'],
+ 'depends': [
+ 'base_setup',
+ 'email_template',
+ ],
'data': [
'auth_signup_data.xml',
'res_config.xml',
diff --git a/addons/auth_signup/auth_signup_data.xml b/addons/auth_signup/auth_signup_data.xml
index 39b62148494..32e397b1ba6 100644
--- a/addons/auth_signup/auth_signup_data.xml
+++ b/addons/auth_signup/auth_signup_data.xml
@@ -18,5 +18,20 @@
+
+
+ Reset Password
+
+ ]]>
+ ${object.email}
+ Password reset
+ A password reset was requested for the OpenERP account linked to this email.
+
+You may change your password by following this link.
+
+Note: If you do not expect this, you can safely ignore this email.
]]>
+
+
diff --git a/addons/auth_signup/controllers/main.py b/addons/auth_signup/controllers/main.py
index 888d2131f42..a2b77502737 100644
--- a/addons/auth_signup/controllers/main.py
+++ b/addons/auth_signup/controllers/main.py
@@ -19,10 +19,12 @@
#
##############################################################################
import logging
+import urllib
+
+import werkzeug
import openerp
from openerp.modules.registry import RegistryManager
-
from ..res_users import SignupError
_logger = logging.getLogger(__name__)
@@ -56,4 +58,21 @@ class Controller(openerp.addons.web.http.Controller):
cr.commit()
return {}
+ @openerp.addons.web.http.httprequest
+ def reset_password(self, req, dbname, login):
+ """ retrieve user, and perform reset password """
+ registry = RegistryManager.get(dbname)
+ with registry.cursor() as cr:
+ try:
+ res_users = registry.get('res.users')
+ res_users.reset_password(cr, openerp.SUPERUSER_ID, login)
+ cr.commit()
+ message = 'An email has been sent with credentials to reset your password'
+ except Exception as e:
+ # signup error
+ _logger.exception('error when resetting password')
+ message = e.message
+ params = [('action', 'login'), ('error_message', message)]
+ return werkzeug.utils.redirect("/#" + urllib.urlencode(params))
+
# vim:expandtab:tabstop=4:softtabstop=4:shiftwidth=4:
diff --git a/addons/auth_signup/res_users.py b/addons/auth_signup/res_users.py
index 7dbb19853a2..de6ec37a45e 100644
--- a/addons/auth_signup/res_users.py
+++ b/addons/auth_signup/res_users.py
@@ -18,14 +18,15 @@
# along with this program. If not, see
#
##############################################################################
+from datetime import datetime, timedelta
import random
-import time
import urllib
import urlparse
from openerp.osv import osv, fields
from openerp.tools.misc import DEFAULT_SERVER_DATETIME_FORMAT
from openerp.tools.safe_eval import safe_eval
+from openerp.tools.translate import _
class SignupError(Exception):
pass
@@ -33,10 +34,12 @@ class SignupError(Exception):
def random_token():
# the token has an entropy of about 120 bits (6 bits/char * 20 chars)
chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
- return ''.join(random.choice(chars) for _ in xrange(20))
+ return ''.join(random.choice(chars) for i in xrange(20))
+
+def now(**kwargs):
+ dt = datetime.now() + timedelta(**kwargs)
+ return dt.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
-def now():
- return time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
class res_partner(osv.Model):
_inherit = 'res.partner'
@@ -58,7 +61,7 @@ class res_partner(osv.Model):
# when required, make sure the partner has a valid signup token
if context and context.get('signup_valid') and not partner.user_ids:
self.signup_prepare(cr, uid, [partner.id], context=context)
-
+
action_template = None
params = {
'action': urllib.quote(action),
@@ -218,3 +221,31 @@ class res_users(osv.Model):
# create a copy of the template user (attached to a specific partner_id if given)
values['active'] = True
return self.copy(cr, uid, template_user_id, values, context=context)
+
+ def reset_password(self, cr, uid, login, context=None):
+ """ retrieve the user corresponding to login (login or email),
+ and reset their password
+ """
+ user_ids = self.search(cr, uid, [('login', '=', login)], context=context)
+ if not user_ids:
+ user_ids = self.search(cr, uid, [('email', '=', login)], context=context)
+ if len(user_ids) != 1:
+ raise Exception('Reset password: invalid username or email')
+ return self.action_reset_password(cr, uid, user_ids, context=context)
+
+ def action_reset_password(self, cr, uid, ids, context=None):
+ """ create signup token for each user, and send their signup url by email """
+ # prepare reset password signup
+ res_partner = self.pool.get('res.partner')
+ partner_ids = [user.partner_id.id for user in self.browse(cr, uid, ids, context)]
+ res_partner.signup_prepare(cr, uid, partner_ids, expiration=now(days=+1), context=context)
+
+ # send email to users with their signup url
+ template = self.pool.get('ir.model.data').get_object(cr, uid, 'auth_signup', 'reset_password_email')
+ assert template._name == 'email.template'
+ for user in self.browse(cr, uid, ids, context):
+ if not user.email:
+ raise osv.except_osv(_("Cannot send email: user has no email address."), user.name)
+ self.pool.get('email.template').send_mail(cr, uid, template.id, user.id, context=context)
+
+ return True
diff --git a/addons/auth_signup/res_users_view.xml b/addons/auth_signup/res_users_view.xml
index c42e1ab5f41..90729c54285 100644
--- a/addons/auth_signup/res_users_view.xml
+++ b/addons/auth_signup/res_users_view.xml
@@ -7,11 +7,23 @@
res.users
+
+
+
+
+
+
+
+
+
+
+
diff --git a/addons/auth_signup/static/src/js/auth_signup.js b/addons/auth_signup/static/src/js/auth_signup.js
index 241c2406d56..562332556ab 100644
--- a/addons/auth_signup/static/src/js/auth_signup.js
+++ b/addons/auth_signup/static/src/js/auth_signup.js
@@ -43,8 +43,13 @@ openerp.auth_signup = function(instance) {
.fail(self.on_token_failed)
});
}
+
+ // bind reset password link
+ this.$('a.oe_reset_password').click(this.do_reset_password);
+
return d;
},
+
on_token_loaded: function(result) {
// select the right the database
this.selected_db = result.db;
@@ -66,6 +71,7 @@ openerp.auth_signup = function(instance) {
this.$("form input[name=login]").val(result.login || "");
}
},
+
on_token_failed: function(result, ev) {
if (ev) {
ev.preventDefault();
@@ -74,6 +80,7 @@ openerp.auth_signup = function(instance) {
delete this.params.db;
delete this.params.token;
},
+
on_submit: function(ev) {
if (ev) {
ev.preventDefault();
@@ -124,6 +131,26 @@ openerp.auth_signup = function(instance) {
this._super(ev);
}
},
- });
+ do_reset_password: function(ev) {
+ if (ev) {
+ ev.preventDefault();
+ }
+ var db = this.$("form [name=db]").val();
+ var login = this.$("form input[name=login]").val();
+ if (!db) {
+ this.do_warn("Login", "No database selected !");
+ return false;
+ } else if (!login) {
+ this.do_warn("Login", "Please enter a username or email address.")
+ return false;
+ }
+ var params = {
+ dbname : db,
+ login: login,
+ };
+ var url = "/auth_signup/reset_password?" + $.param(params);
+ window.location = url;
+ },
+ });
};
diff --git a/addons/auth_signup/static/src/xml/auth_signup.xml b/addons/auth_signup/static/src/xml/auth_signup.xml
index a51ced3778d..d1ae437860f 100644
--- a/addons/auth_signup/static/src/xml/auth_signup.xml
+++ b/addons/auth_signup/static/src/xml/auth_signup.xml
@@ -24,6 +24,9 @@
Back to Login
+
+ Reset password
+