From 875d7541d99fd9bd9eb01d0a2bb7599e8911cf96 Mon Sep 17 00:00:00 2001 From: Antony Lesuisse Date: Sun, 12 Aug 2012 19:06:03 +0200 Subject: [PATCH] sign up with controller bzr revid: al@openerp.com-20120812170603-mnp54q5xn9htg6vj --- addons/auth_signup/__init__.py | 3 +- addons/auth_signup/__openerp__.py | 5 +- addons/auth_signup/auth_signup.py | 39 -------- addons/auth_signup/controllers/__init__.py | 3 + addons/auth_signup/controllers/main.py | 37 ++++++++ addons/auth_signup/res_users.py | 47 ++++++++++ .../static/src/css/auth_signup.css | 9 -- .../auth_signup/static/src/js/auth_signup.js | 90 +++++-------------- .../static/src/xml/auth_signup.xml | 8 +- 9 files changed, 117 insertions(+), 124 deletions(-) delete mode 100644 addons/auth_signup/auth_signup.py create mode 100644 addons/auth_signup/controllers/__init__.py create mode 100644 addons/auth_signup/controllers/main.py create mode 100644 addons/auth_signup/res_users.py delete mode 100644 addons/auth_signup/static/src/css/auth_signup.css diff --git a/addons/auth_signup/__init__.py b/addons/auth_signup/__init__.py index 2e404949550..8b5f4d767b5 100644 --- a/addons/auth_signup/__init__.py +++ b/addons/auth_signup/__init__.py @@ -1,2 +1,3 @@ +import controllers import res_config -import auth_signup +import res_users diff --git a/addons/auth_signup/__openerp__.py b/addons/auth_signup/__openerp__.py index a869c623db0..3dbe97394a0 100644 --- a/addons/auth_signup/__openerp__.py +++ b/addons/auth_signup/__openerp__.py @@ -6,16 +6,13 @@ 'category': 'Authentication', 'website': 'http://www.openerp.com', 'installable': True, - 'depends': ['auth_anonymous', 'base_setup'], + 'depends': ['base_setup'], 'data': [ 'res_config.xml', ], 'js': [ 'static/src/js/auth_signup.js', ], - 'css': [ - 'static/src/css/auth_signup.css', - ], 'qweb': [ 'static/src/xml/auth_signup.xml', ], diff --git a/addons/auth_signup/auth_signup.py b/addons/auth_signup/auth_signup.py deleted file mode 100644 index ebc72b3aeac..00000000000 --- a/addons/auth_signup/auth_signup.py +++ /dev/null @@ -1,39 +0,0 @@ -from openerp.osv import osv, fields - -class res_users(osv.Model): - _inherit = 'res.users' - - _sql_constraints = [ - ('email_uniq', 'UNIQUE (user_email)', 'You can not have two users with the same email!') - ] - -class signup_signup(osv.TransientModel): - _name = 'auth.signup' - - # TODO add captcha - _columns = { - 'name': fields.char('Name', size=64), - 'email': fields.char('Email', size=64), - 'password': fields.char('Password', size=64), - } - - def create(self, cr, uid, values, context=None): - # NOTE here, invalid values raises exceptions to avoid storing - # sensitive data into the database (which then are available to anyone) - - new_user = { - 'name': values['name'], - 'login': values['email'], - 'user_email': values['email'], - 'password': values['password'], - 'active': True, - } - - user_template_id = self.pool.get('ir.config_parameter').get_param(cr, uid, 'auth.signup_template_user_id', 0) - if user_template_id: - self.pool.get('res.users').copy(cr, 1, user_template_id, new_user, context=context) - else: - self.pool.get('res.users').create(cr, 1, new_user, context=context) - - # Dont store anything - return 0 diff --git a/addons/auth_signup/controllers/__init__.py b/addons/auth_signup/controllers/__init__.py new file mode 100644 index 00000000000..e11f9ba81bb --- /dev/null +++ b/addons/auth_signup/controllers/__init__.py @@ -0,0 +1,3 @@ +import main + +# vim:expandtab:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/auth_signup/controllers/main.py b/addons/auth_signup/controllers/main.py new file mode 100644 index 00000000000..7b84c2e2214 --- /dev/null +++ b/addons/auth_signup/controllers/main.py @@ -0,0 +1,37 @@ +import logging + +import werkzeug.urls + +import openerp.modules.registry +import openerp.addons.web.controllers.main +import openerp.addons.web.common.http as openerpweb + +_logger = logging.getLogger(__name__) + +class OpenIDController(openerpweb.Controller): + _cp_path = '/auth_signup' + + @openerpweb.httprequest + def signup(self, req, dbname, name, login, password): + registry = openerp.modules.registry.RegistryManager.get(dbname) + cr = registry.db.cursor() + try: + try: + u = registry.get('res.users') + r = u.auth_signup(cr, 1, name, login, password) + cr.commit() + return openerp.addons.web.controllers.main.login_and_redirect(req, dbname, login, password) + # or + req.authenticate(*r) + url = "/" + except AttributeError: + # auth_signup is not installed + url = "/#action=auth_signup&error=1" + except Exception,e: + # signup error + url = "/#action=auth_signup&error=2" + finally: + cr.close() + return werkzeug.utils.redirect(url) + +# vim:expandtab:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/auth_signup/res_users.py b/addons/auth_signup/res_users.py new file mode 100644 index 00000000000..46f264c6729 --- /dev/null +++ b/addons/auth_signup/res_users.py @@ -0,0 +1,47 @@ +import openerp +from openerp.osv import osv, fields + +class res_users(osv.Model): + _inherit = 'res.users' + + def auth_signup_create(self, cr, uid, new_user, context=None): + user_template_id = self.pool.get('ir.config_parameter').get_param(cr, uid, 'auth.signup_template_user_id', 0) + if user_template_id: + self.pool.get('res.users').copy(cr, 1, user_template_id, new_user, context=context) + else: + self.pool.get('res.users').create(cr, 1, new_user, context=context) + + def auth_signup_check(self, cr, uid, login, key, context=None): + res = self.search(cr, uid, [("login","=",login)]) + if res: + user_id = res[0]['id'] + self.check(cr.dbname, user_id, key) + return user_id + return False + + def auth_signup(self, cr, uid, name, login, password, context=None): + r = (cr.dbname, login, password) + try: + # check for existing user + if not self.auth_signup_check(cr, uid, login, password): + print "NEW USER" + # new user + new_user = { + 'name': name, + 'login': login, + 'user_email': login, + 'password': password, + 'active': True, + } + self.auth_signup_create(cr,uid, new_user) + return r + else: + print "Existing same" + # already existing with same password + return r + except openerp.exceptions.AccessDenied: + print "Existing different" + # already existing with diffrent password + raise + +# diff --git a/addons/auth_signup/static/src/css/auth_signup.css b/addons/auth_signup/static/src/css/auth_signup.css deleted file mode 100644 index 1e8d7d9d8d8..00000000000 --- a/addons/auth_signup/static/src/css/auth_signup.css +++ /dev/null @@ -1,9 +0,0 @@ -.openerp .oe_login .oe_login_pane ul.oe_signup a { - color: #eeeeee; - margin: 0 8px; -} -.openerp .oe_login .oe_login_pane ul.oe_signup a:hover { - text-decoration: underline; -} - - diff --git a/addons/auth_signup/static/src/js/auth_signup.js b/addons/auth_signup/static/src/js/auth_signup.js index 7384fe1e5f5..aba6822739d 100644 --- a/addons/auth_signup/static/src/js/auth_signup.js +++ b/addons/auth_signup/static/src/js/auth_signup.js @@ -1,65 +1,38 @@ openerp.auth_signup = function(instance) { + instance.auth_signup = instance.auth_signup || {}; var _t = instance.web._t; instance.web.Login.include({ start: function() { var self = this; - this.$('a.oe_signup').click(function() { - var db = self.$("form [name=db]").val(); - if (!db) { - self.do_warn(_t("Login"), _t("No database selected!")); - return false; - } - - var cnx = instance.connection; - if (cnx.session_is_valid()) { - self._signup(); - } else { - cnx.session_authenticate(db, 'anonymous', 'anonymous', true).then(function() { - self._signup(); - }).fail(function(error, event) { - console.log(error); - // cannot log as anonymous or auth_signup not installed - self.do_warn(_t('Sign Up'), _.str.sprintf(_t('Signup functionnality is not available for database %s'), db), true); - }); - } + var dbname = self.$("form [name=db]").val(); + self.do_action({ + type: 'ir.actions.client', + tag: 'auth_signup.signup', + params: {'dbname': dbname}, + target: 'new', + name: 'Sign up' + }); return true; }); return this._super(); - }, - - _signup: function() { - this.do_action({ - type: 'ir.actions.client', - tag: 'auth_signup.signup', - target: 'new', - name: 'Sign up' - }); - } }); - instance.auth_signup = instance.auth_signup || {}; instance.auth_signup.Signup = instance.web.Widget.extend({ template: 'auth_signup.signup', - init: function() { - this._super.apply(this, arguments); - this.dataset = new instance.web.DataSet(this, 'auth.signup'); + init: function(parent, params) { + this.params = params; + return this._super(); }, start: function() { var self = this; - this.$('input[type=password]').change(function() { + this.$('input[name=password_confirmation]').keyup(function() { var v = $(this).val(); - var e = !_.isEmpty(v); - if (e) { - e =_.all(self.$('input[type=password]'), function(i) { - return $(i).val() === v; - }); - } var $b = self.$('button'); - if (e) { + if (_.isEmpty(v) || self.$('input[name=password]').val() === v) { $b.removeAttr('disabled'); } else { $b.attr('disabled', 'disabled'); @@ -70,36 +43,19 @@ openerp.auth_signup = function(instance) { if(ev) { ev.preventDefault(); } - var name = self.$('input[name=name]').val(); - var email = self.$('input[name=email]').val(); - var password = self.$('input[name=password]').val(); - - self.dataset.create({ - name: name, - email: email, - password: password - }, function() { - self.do_action({ - type: 'ir.actions.client', - tag: 'login', - params: { - db: instance.connection.db, - login: email, - password: password, - login_successful: function() { - self.do_action('home'); - } - } - }); - }); + var params = { + dbname : self.params.dbname, + name: self.$('input[name=name]').val(), + login: self.$('input[name=email]').val(), + password: self.$('input[name=password]').val(), + }; + var url = "/auth_signup/signup?" + $.param(params); + window.location = url; return false; - }); - return $.when(this._super()); + return this._super(); } - }); instance.web.client_actions.add("auth_signup.signup", "instance.auth_signup.Signup"); - }; diff --git a/addons/auth_signup/static/src/xml/auth_signup.xml b/addons/auth_signup/static/src/xml/auth_signup.xml index 92343d96281..c2eec846d15 100644 --- a/addons/auth_signup/static/src/xml/auth_signup.xml +++ b/addons/auth_signup/static/src/xml/auth_signup.xml @@ -14,10 +14,10 @@
- Name = - Email = - Password = - Confirmation = + Name =
+ Email =
+ Password =
+ Confirmation =