[IMP] Small improvements
[odoo/odoo.git] / addons / auth_oauth_signup / res_users.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2010-2012 OpenERP SA (<http://openerp.com>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 import logging
23 import simplejson
24
25 import openerp
26 from openerp.addons.auth_signup.res_users import SignupError
27 from openerp.osv import osv, fields
28
29 _logger = logging.getLogger(__name__)
30
31 class res_users(osv.Model):
32     _inherit = 'res.users'
33
34     def _auth_oauth_signin(self, cr, uid, provider, validation, params, context=None):
35         # overridden to use signup method if regular oauth signin fails
36         try:
37             login = super(res_users, self)._auth_oauth_signin(cr, uid, provider, validation, params, context=context)
38
39         except openerp.exceptions.AccessDenied, access_denied_exception:
40             if context and context.get('no_user_creation'):
41                 return None
42             state = simplejson.loads(params['state'])
43             token = state.get('t')
44             oauth_uid = validation['user_id']
45             email = validation.get('email', 'provider_%s_user_%s' % (provider, oauth_uid))
46             name = validation.get('name', email)
47             values = {
48                 'name': name,
49                 'login': email,
50                 'email': email,
51                 'oauth_provider_id': provider,
52                 'oauth_uid': oauth_uid,
53                 'oauth_access_token': params['access_token'],
54                 'active': True,
55             }
56             try:
57                 _, login, _ = self.signup(cr, uid, values, token, context=context)       
58             except SignupError:
59                 raise access_denied_exception
60         return login