[ADD] auth_oauth_signup: link module to make auth_oauth work with auth_signup
authorRaphael Collet <rco@openerp.com>
Mon, 26 Nov 2012 10:52:42 +0000 (11:52 +0100)
committerRaphael Collet <rco@openerp.com>
Mon, 26 Nov 2012 10:52:42 +0000 (11:52 +0100)
bzr revid: rco@openerp.com-20121126105242-4a1urt7lwnkvchgm

addons/auth_oauth_signup/__init__.py [new file with mode: 0644]
addons/auth_oauth_signup/__openerp__.py [new file with mode: 0644]
addons/auth_oauth_signup/res_users.py [new file with mode: 0644]
addons/auth_oauth_signup/static/src/js/auth_oauth_signup.js [new file with mode: 0644]

diff --git a/addons/auth_oauth_signup/__init__.py b/addons/auth_oauth_signup/__init__.py
new file mode 100644 (file)
index 0000000..bc40c87
--- /dev/null
@@ -0,0 +1,22 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2012-today OpenERP SA (<http://www.openerp.com>)
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU Affero General Public License as
+#    published by the Free Software Foundation, either version 3 of the
+#    License, or (at your option) any later version
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU Affero General Public License for more details
+#
+#    You should have received a copy of the GNU Affero General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>
+#
+##############################################################################
+
+import res_users
diff --git a/addons/auth_oauth_signup/__openerp__.py b/addons/auth_oauth_signup/__openerp__.py
new file mode 100644 (file)
index 0000000..1e57670
--- /dev/null
@@ -0,0 +1,39 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2010-2012 OpenERP SA (<http://openerp.com>).
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU Affero General Public License as
+#    published by the Free Software Foundation, either version 3 of the
+#    License, or (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU Affero General Public License for more details.
+#
+#    You should have received a copy of the GNU Affero General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+##############################################################################
+
+{
+    'name': 'Signup with OAuth2 Authentication',
+    'version': '1.0',
+    'category': 'Hidden',
+    'description': """
+Allow users to sign up through OAuth2 Provider.
+===============================================
+""",
+    'author': 'OpenERP SA',
+    'website': 'http://www.openerp.com',
+    'depends': ['auth_oauth', 'auth_signup'],
+    'data': [],
+    'js': ['static/src/js/auth_oauth_signup.js'],
+    'css': [],
+    'qweb': [],
+    'installable': True,
+    'auto_install': True,
+}
diff --git a/addons/auth_oauth_signup/res_users.py b/addons/auth_oauth_signup/res_users.py
new file mode 100644 (file)
index 0000000..8b56585
--- /dev/null
@@ -0,0 +1,60 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2010-2012 OpenERP SA (<http://openerp.com>).
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU Affero General Public License as
+#    published by the Free Software Foundation, either version 3 of the
+#    License, or (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU Affero General Public License for more details.
+#
+#    You should have received a copy of the GNU Affero General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+##############################################################################
+
+import logging
+
+import urllib
+import urlparse
+import urllib2
+import simplejson
+
+import openerp
+from openerp.osv import osv, fields
+from openerp import SUPERUSER_ID
+
+_logger = logging.getLogger(__name__)
+
+class res_users(osv.Model):
+    _inherit = 'res.users'
+
+    def _auth_oauth_signin(self, cr, uid, provider, validation, params, context=None):
+        # overridden to use signup method if regular oauth signin fails
+        try:
+            login = super(res_users, self)._auth_oauth_signin(cr, uid, provider, validation, params, context=context)
+
+        except openerp.exceptions.AccessDenied:
+            state = simplejson.loads(params['state'])
+            token = state.get('t')
+            oauth_uid = validation['user_id']
+            email = validation.get('email', 'provider_%d_user_%d' % (provider, oauth_uid))
+            name = validation.get('name', email)
+            values = {
+                'name': name,
+                'login': email,
+                'email': email,
+                'oauth_provider_id': provider,
+                'oauth_uid': oauth_uid,
+                'oauth_access_token': params['access_token'],
+                'active': True,
+            }
+            _, login, _ = self.signup(cr, uid, values, token, context=context)
+
+        return login
diff --git a/addons/auth_oauth_signup/static/src/js/auth_oauth_signup.js b/addons/auth_oauth_signup/static/src/js/auth_oauth_signup.js
new file mode 100644 (file)
index 0000000..6715943
--- /dev/null
@@ -0,0 +1,14 @@
+openerp.auth_oauth_signup = function(instance) {
+
+    // override Login._oauth_state to add the signup token in the state
+    instance.web.Login.include({
+        _oauth_state: function(provider) {
+            var state = this._super.apply(this, arguments);
+            if (this.params.token) {
+                state.t = this.params.token;
+            }
+            return state;
+        },
+    });
+
+};