sign up with controller
authorAntony Lesuisse <al@openerp.com>
Sun, 12 Aug 2012 17:06:03 +0000 (19:06 +0200)
committerAntony Lesuisse <al@openerp.com>
Sun, 12 Aug 2012 17:06:03 +0000 (19:06 +0200)
bzr revid: al@openerp.com-20120812170603-mnp54q5xn9htg6vj

addons/auth_signup/__init__.py
addons/auth_signup/__openerp__.py
addons/auth_signup/auth_signup.py [deleted file]
addons/auth_signup/controllers/__init__.py [new file with mode: 0644]
addons/auth_signup/controllers/main.py [new file with mode: 0644]
addons/auth_signup/res_users.py [new file with mode: 0644]
addons/auth_signup/static/src/css/auth_signup.css [deleted file]
addons/auth_signup/static/src/js/auth_signup.js
addons/auth_signup/static/src/xml/auth_signup.xml

index 2e40494..8b5f4d7 100644 (file)
@@ -1,2 +1,3 @@
+import controllers
 import res_config
-import auth_signup
+import res_users
index a869c62..3dbe973 100644 (file)
@@ -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 (file)
index ebc72b3..0000000
+++ /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 (file)
index 0000000..e11f9ba
--- /dev/null
@@ -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 (file)
index 0000000..7b84c2e
--- /dev/null
@@ -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 (file)
index 0000000..46f264c
--- /dev/null
@@ -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 (file)
index 1e8d7d9..0000000
+++ /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;
-}
-
-
index 7384fe1..aba6822 100644 (file)
@@ -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");
 
-
 };
index 92343d9..c2eec84 100644 (file)
 <t t-name="auth_signup.signup">
   <div>
     <form>
-        Name = <input type="text" name="name"/>
-        Email = <input type="email" name="email"/>
-        Password = <input type="password" name="password"/>
-        Confirmation = <input type="password" name="password_confirmation"/>
+        Name = <input type="text" name="name"/><br/>
+        Email = <input type="email" name="email"/><br/>
+        Password = <input type="password" name="password"/><br/>
+        Confirmation = <input type="password" name="password_confirmation"/><br/>
         <button type="submit" disabled="disabled">Signup</button>
     </form>
   </div>